All files / common logger.ts

100% Statements 10/10
85.71% Branches 6/7
100% Functions 3/3
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 313x     3x     3x             40x 40x     40x             3x 9x 3x   9x    
import winston from 'winston';
 
// Singleton logger instance
let loggerInstance: winston.Logger | null = null;
 
function createLogger(): winston.Logger {
  return winston.createLogger({
    level: process.env.LOG_LEVEL || 'info',
    format: winston.format.combine(
      winston.format.timestamp(),
      winston.format.errors({ stack: true }),
      winston.format.splat(),
      winston.format.printf(({ timestamp, level, message, label, ...meta }) => {
        const labelStr = label ? `[${String(label)}] ` : '';
        const metaStr = Object.keys(meta).length
          ? ` ${JSON.stringify(meta)}`
          : '';
        return `${String(timestamp)} ${labelStr}${String(level)}: ${String(message)}${metaStr}`;
      })
    ),
    transports: [new winston.transports.Console()],
  });
}
 
export function getLogger(label?: string): winston.Logger {
  if (!loggerInstance) {
    loggerInstance = createLogger();
  }
  return loggerInstance.child({ label });
}