token-guardian
Version:
A comprehensive solution for protecting and managing API tokens and secrets
47 lines (39 loc) • 1.33 kB
text/typescript
/**
* Log levels
*/
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
/**
* Simple logger utility
*/
export class Logger {
constructor(private level: LogLevel = 'info') {}
public debug(message: string, meta?: Record<string, unknown>): void {
if (this.shouldLog('debug')) {
console.debug(this.format('DEBUG', message, meta));
}
}
public info(message: string, meta?: Record<string, unknown>): void {
if (this.shouldLog('info')) {
console.info(this.format('INFO', message, meta));
}
}
public warn(message: string, meta?: Record<string, unknown>): void {
if (this.shouldLog('warn')) {
console.warn(this.format('WARN', message, meta));
}
}
public error(message: string, meta?: Record<string, unknown>): void {
if (this.shouldLog('error')) {
console.error(this.format('ERROR', message, meta));
}
}
private shouldLog(level: LogLevel): boolean {
const levels: LogLevel[] = ['debug', 'info', 'warn', 'error'];
return levels.indexOf(level) >= levels.indexOf(this.level);
}
private format(level: string, message: string, meta?: Record<string, unknown>): string {
const timestamp = new Date().toISOString();
const metaStr = meta ? ` ${JSON.stringify(meta)}` : '';
return `[${timestamp}] ${level}: ${message}${metaStr}`;
}
}