You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

77 lines
2.0 KiB

/**
* Structured logging system for TimeSafari
*
* Provides consistent logging across the application with:
* - Timestamp tracking
* - Log levels (debug, info, warn, error)
* - Structured data support
* - Component tagging
*
* @author Matthew Raymer <matthew.raymer@anomalistdesign.com>
* @version 1.0.0
* @since 2025-06-01
*/
// Log levels
export enum LogLevel {
DEBUG = 'DEBUG',
INFO = 'INFO',
WARN = 'WARN',
ERROR = 'ERROR'
}
// Log entry interface
interface LogEntry {
timestamp: string;
level: LogLevel;
component: string;
message: string;
data?: unknown;
}
// Format log entry
const formatLogEntry = (entry: LogEntry): string => {
const { timestamp, level, component, message, data } = entry;
const dataStr = data ? ` ${JSON.stringify(data, null, 2)}` : '';
return `[${timestamp}] [${level}] [${component}] ${message}${dataStr}`;
};
// Create logger for a specific component
export const createLogger = (component: string) => {
const log = (level: LogLevel, message: string, data?: unknown) => {
const entry: LogEntry = {
timestamp: new Date().toISOString(),
level,
component,
message,
data
};
const formatted = formatLogEntry(entry);
switch (level) {
case LogLevel.DEBUG:
console.debug(formatted);
break;
case LogLevel.INFO:
console.info(formatted);
break;
case LogLevel.WARN:
console.warn(formatted);
break;
case LogLevel.ERROR:
console.error(formatted);
break;
}
};
return {
debug: (message: string, data?: unknown) => log(LogLevel.DEBUG, message, data),
info: (message: string, data?: unknown) => log(LogLevel.INFO, message, data),
warn: (message: string, data?: unknown) => log(LogLevel.WARN, message, data),
error: (message: string, data?: unknown) => log(LogLevel.ERROR, message, data)
};
};
// Create default logger for SQLite operations
export const logger = createLogger('SQLite');