Add missing version.ts and logging.ts files

This commit is contained in:
defiQUG
2026-01-23 16:48:27 -08:00
parent dcc6a1306f
commit 2bbb5a4fa6
10 changed files with 507 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
/**
* Structured logging utilities
* Provides JSON logging with correlation IDs and log levels
*/
class Logger {
correlationId = null;
minLevel = (typeof process !== 'undefined' ? process.env?.LOG_LEVEL : undefined) || 'info';
levels = {
debug: 0,
info: 1,
warn: 2,
error: 3,
fatal: 4,
};
setCorrelationId(id) {
this.correlationId = id;
}
clearCorrelationId() {
this.correlationId = null;
}
setMinLevel(level) {
this.minLevel = level;
}
shouldLog(level) {
return this.levels[level] >= this.levels[this.minLevel];
}
log(level, message, context, error) {
if (!this.shouldLog(level)) {
return;
}
const entry = {
timestamp: new Date().toISOString(),
level,
message,
correlationId: this.correlationId || undefined,
context,
};
if (error) {
entry.error = {
name: error.name,
message: error.message,
stack: error.stack,
};
}
// In production, this would send to a log aggregation service
// For now, output as JSON to console
if (level === 'error' || level === 'fatal') {
console.error(JSON.stringify(entry));
}
else if (level === 'warn') {
console.warn(JSON.stringify(entry));
}
else {
console.log(JSON.stringify(entry));
}
}
debug(message, context) {
this.log('debug', message, context);
}
info(message, context) {
this.log('info', message, context);
}
warn(message, context) {
this.log('warn', message, context);
}
error(message, error, context) {
this.log('error', message, context, error);
}
fatal(message, error, context) {
this.log('fatal', message, context, error);
}
// Transaction-specific logging
logTransaction(transactionId, action, level = 'info', context) {
this.log(level, `Transaction ${action}`, { ...context, transactionId });
}
// Audit logging
logAudit(action, userId, context) {
this.log('info', `Audit: ${action}`, { ...context, userId });
}
}
// Singleton instance
const logger = new Logger();
export function getLogger() {
return logger;
}
/**
* Generate correlation ID
*/
export function generateCorrelationId() {
return `corr-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Create a scoped logger with correlation ID
*/
export function createScopedLogger(correlationId) {
const scopedLogger = new Logger();
scopedLogger.setCorrelationId(correlationId);
return scopedLogger;
}
//# sourceMappingURL=logging.js.map