Add missing version.ts and logging.ts files
This commit is contained in:
100
packages/utils/src/logging.js
Normal file
100
packages/utils/src/logging.js
Normal 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
|
||||
Reference in New Issue
Block a user