mirror of
https://dev.azure.com/tstanciu94/PhantomMind/_git/Bitip
synced 2025-10-13 01:52:19 +03:00
112 lines
2.8 KiB
TypeScript
112 lines
2.8 KiB
TypeScript
import { Logger as SeqLogger, SeqLoggerConfig } from 'seq-logging';
|
|
import { LogLevel, LOG_LEVELS, LOG_LEVEL_PRIORITY } from '../types/index.js';
|
|
import config from './config.js';
|
|
|
|
class Logger {
|
|
private seqLogger?: SeqLogger;
|
|
private minLevel: LogLevel;
|
|
|
|
constructor() {
|
|
this.minLevel = config.logLevel;
|
|
|
|
if (config.seqUrl) {
|
|
const seqConfig: SeqLoggerConfig = {
|
|
serverUrl: config.seqUrl,
|
|
apiKey: config.seqApiKey,
|
|
onError: (error: Error) => {
|
|
console.error('Seq logging error:', error);
|
|
},
|
|
};
|
|
this.seqLogger = new SeqLogger(seqConfig);
|
|
}
|
|
|
|
// Log initial configuration
|
|
console.log(
|
|
`[INFO] Logger initialized with minimum level: ${this.minLevel.toUpperCase()}`
|
|
);
|
|
}
|
|
|
|
private shouldLog(level: LogLevel): boolean {
|
|
return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[this.minLevel];
|
|
}
|
|
|
|
info(message: string, properties?: Record<string, unknown>): void {
|
|
if (!this.shouldLog(LOG_LEVELS.INFO)) return;
|
|
|
|
console.log(`[INFO] ${message}`, properties || '');
|
|
if (this.seqLogger) {
|
|
this.seqLogger.emit({
|
|
timestamp: new Date(),
|
|
level: 'Information',
|
|
messageTemplate: message,
|
|
properties: properties || {},
|
|
});
|
|
}
|
|
}
|
|
|
|
warn(message: string, properties?: Record<string, unknown>): void {
|
|
if (!this.shouldLog(LOG_LEVELS.WARNING)) return;
|
|
|
|
console.warn(`[WARN] ${message}`, properties || '');
|
|
if (this.seqLogger) {
|
|
this.seqLogger.emit({
|
|
timestamp: new Date(),
|
|
level: 'Warning',
|
|
messageTemplate: message,
|
|
properties: properties || {},
|
|
});
|
|
}
|
|
}
|
|
|
|
error(
|
|
message: string,
|
|
error?: Error,
|
|
properties?: Record<string, unknown>
|
|
): void {
|
|
if (!this.shouldLog(LOG_LEVELS.ERROR)) return;
|
|
|
|
console.error(`[ERROR] ${message}`, error || '', properties || '');
|
|
if (this.seqLogger) {
|
|
this.seqLogger.emit({
|
|
timestamp: new Date(),
|
|
level: 'Error',
|
|
messageTemplate: message,
|
|
properties: {
|
|
error: error?.message,
|
|
stack: error?.stack,
|
|
...properties,
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
debug(message: string, properties?: Record<string, unknown>): void {
|
|
if (!this.shouldLog(LOG_LEVELS.DEBUG)) return;
|
|
|
|
console.debug(`[DEBUG] ${message}`, properties || '');
|
|
if (this.seqLogger) {
|
|
this.seqLogger.emit({
|
|
timestamp: new Date(),
|
|
level: 'Debug',
|
|
messageTemplate: message,
|
|
properties: properties || {},
|
|
});
|
|
}
|
|
}
|
|
|
|
async flush(): Promise<void> {
|
|
if (this.seqLogger) {
|
|
await this.seqLogger.flush();
|
|
}
|
|
}
|
|
|
|
async close(): Promise<void> {
|
|
if (this.seqLogger) {
|
|
await this.seqLogger.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
export const logger = new Logger();
|
|
export default logger;
|