added new logging system

This commit is contained in:
2025-12-31 09:51:40 -05:00
parent 0e6a3c4a01
commit 42b96d58a0
2 changed files with 70 additions and 0 deletions

View File

@@ -23,6 +23,9 @@ APPLICATION_VERSION= # Should match release tag
APPLICATION_ENVIRONMENT= # dev / prod
CONFIG_ID= # configures
# Logger
LOG_DEPTH= # normal: 0, verbose: 1
# Glitchtip
GLITCHTIP_DSN=
DISABLE_GLITCHTIP= # true/false

View File

@@ -0,0 +1,67 @@
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
export type LogDepth = 'normal' | 'verbose';
export type LogType = 'http' | 'app';
export interface LogHeader {
timestamp: string;
level: LogLevel;
depth: LogDepth;
type: LogType; // 'http', 'app', 'db', etc.
user_id?: number;
}
export interface LogPayload {
message?: string; // short human-friendly description
data?: Record<string, any>; // type-specific rich data
}
// Environment defaults
const CURRENT_DEPTH: LogDepth = (process.env.LOG_DEPTH as LogDepth) || 'normal';
const DEPTH_ORDER: Record<LogDepth, number> = { normal: 0, verbose: 1 };
function shouldLog(level: LogLevel, depth: LogDepth) {
return DEPTH_ORDER[depth] >= DEPTH_ORDER[CURRENT_DEPTH];
}
function emitLog(header: LogHeader, payload: LogPayload = {}) {
if (!shouldLog(header.level, header.depth)) return;
const logLine = { ...header, ...payload };
console.log(JSON.stringify(logLine));
}
export const logger = {
log(level: LogLevel, type: LogType, message: string, data?: Record<string, any>, depth: LogDepth = 'normal', context?: Partial<LogHeader>) {
const header: LogHeader = {
timestamp: new Date().toISOString(),
level,
depth,
type,
...context,
};
const payload: LogPayload = {
message,
data,
};
emitLog(header, payload);
},
info(type: string, message: string, data?: Record<string, any>, depth: LogDepth = 'normal', context?: Partial<LogHeader>) {
this.log('info', type, message, data, depth, context);
},
debug(type: string, message: string, data?: Record<string, any>, depth: LogDepth = 'normal', context?: Partial<LogHeader>) {
this.log('debug', type, message, data, depth, context);
},
warn(type: string, message: string, data?: Record<string, any>, depth: LogDepth = 'normal', context?: Partial<LogHeader>) {
this.log('warn', type, message, data, depth, context);
},
error(type: string, message: string, data?: Record<string, any>, depth: LogDepth = 'normal', context?: Partial<LogHeader>) {
this.log('error', type, message, data, depth, context);
},
}