chore: prepare production deploy bundle
This commit is contained in:
@@ -1,21 +1,38 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import dotenv from 'dotenv';
|
||||||
import { generateUlid } from '../utils/ulid';
|
import { generateUlid } from '../utils/ulid';
|
||||||
import { getTraceId } from './trace-store';
|
import { getTraceId } from './trace-store';
|
||||||
|
|
||||||
|
// Load .env so LOG_LEVEL is available even if env.ts hasn't been imported yet
|
||||||
|
dotenv.config({ path: path.resolve(__dirname, '../../../../.env') });
|
||||||
|
|
||||||
const instanceId = generateUlid();
|
const instanceId = generateUlid();
|
||||||
|
|
||||||
|
const LEVELS: Record<string, number> = { DEBUG: 10, INFO: 20, WARN: 30, ERROR: 40 };
|
||||||
|
const LOG_LEVEL = (process.env.LOG_LEVEL || 'INFO').toUpperCase();
|
||||||
|
const MIN_LEVEL = LEVELS[LOG_LEVEL] ?? LEVELS.INFO;
|
||||||
|
|
||||||
|
const LOGGER_FILE_MARKER = `${path.sep}lib${path.sep}logger.`;
|
||||||
|
|
||||||
function getCallerInfo(): { file: string; line: number } {
|
function getCallerInfo(): { file: string; line: number } {
|
||||||
const stack = new Error().stack;
|
const stack = new Error().stack;
|
||||||
if (!stack) return { file: 'unknown', line: 0 };
|
if (!stack) return { file: 'unknown', line: 0 };
|
||||||
|
|
||||||
const lines = stack.split('\n');
|
const lines = stack.split('\n');
|
||||||
// Skip: Error, logger method, actual caller
|
// Skip frames that are inside logger.ts / logger.js; first non-logger frame is the caller.
|
||||||
const callerLine = lines[3] || '';
|
for (let i = 1; i < lines.length; i++) {
|
||||||
const match = callerLine.match(/\((.+):(\d+):\d+\)/) || callerLine.match(/at (.+):(\d+):\d+/);
|
const line = lines[i];
|
||||||
if (match) return { file: match[1], line: parseInt(match[2]) };
|
if (line.includes(LOGGER_FILE_MARKER) || line.includes('/lib/logger.')) continue;
|
||||||
|
|
||||||
|
const match = line.match(/\((.+):(\d+):\d+\)/) || line.match(/at (.+?):(\d+):\d+/);
|
||||||
|
if (match) return { file: match[1], line: parseInt(match[2], 10) };
|
||||||
|
}
|
||||||
return { file: 'unknown', line: 0 };
|
return { file: 'unknown', line: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
function log(level: string, message: string): void {
|
function log(level: string, message: string): void {
|
||||||
|
if ((LEVELS[level] ?? 0) < MIN_LEVEL) return;
|
||||||
|
|
||||||
const caller = getCallerInfo();
|
const caller = getCallerInfo();
|
||||||
const entry = {
|
const entry = {
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
@@ -31,8 +48,9 @@ function log(level: string, message: string): void {
|
|||||||
|
|
||||||
export const logger = {
|
export const logger = {
|
||||||
instanceId,
|
instanceId,
|
||||||
|
level: LOG_LEVEL,
|
||||||
|
debug: (msg: string) => log('DEBUG', msg),
|
||||||
info: (msg: string) => log('INFO', msg),
|
info: (msg: string) => log('INFO', msg),
|
||||||
warn: (msg: string) => log('WARN', msg),
|
warn: (msg: string) => log('WARN', msg),
|
||||||
error: (msg: string) => log('ERROR', msg),
|
error: (msg: string) => log('ERROR', msg),
|
||||||
debug: (msg: string) => log('DEBUG', msg),
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user