All files / utils logger.ts

94.73% Statements 18/19
77.77% Branches 7/9
100% Functions 2/2
94.44% Lines 17/18

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 7216x 16x     16x               16x                       16x 32x 32x       16x     16x         263x     263x 263x 153x 153x           263x         16x                             16x  
import { getEnvVariable } from './shared';
import winston from 'winston';
 
// Define log levels with corresponding priorities
const LOG_LEVELS = {
  error: 0, // Highest priority - critical errors
  warn: 1,  // Warning messages
  info: 2,  // General information
  debug: 3  // Debug information
} as const;
 
// Define colors for each log level for better visibility
const LOG_COLORS = {
  error: 'red',     // Critical errors in red
  warn: 'yellow',   // Warnings in yellow
  info: 'blue',     // Info in blue  
  debug: 'green'    // Debug in green
};
 
/**
 * Get the current log level from environment variables.
 * Defaults to 'info' if not set or invalid.
 * @returns {keyof typeof LOG_LEVELS} The current log level
 */
export const getLogLevel = (): keyof typeof LOG_LEVELS => {
  const envLevel = getEnvVariable("COMPOSIO_LOGGING_LEVEL", "info")?.toLowerCase();
  return (envLevel && envLevel in LOG_LEVELS) ? envLevel as keyof typeof LOG_LEVELS : 'info';
};
 
// Configure winston colors
winston.addColors(LOG_COLORS);
 
// Create custom log format
const logFormat = winston.format.combine(
  winston.format.timestamp(),
  winston.format.colorize(),
  winston.format.printf(({ timestamp, level, message, ...metadata }) => {
    // Format timestamp for readability
    const formattedTime = timestamp.slice(5, 22).replace('T', ' ');
    
    // Handle metadata serialization
    let metadataStr = '';
    if (Object.keys(metadata).length) {
      try {
        metadataStr = ` - ${JSON.stringify(metadata)}`;
      } catch {
        metadataStr = ' - [Circular metadata object]';
      }
    }
 
    return `[${level}]: ${formattedTime} - ${message}${metadataStr}`;
  })
);
 
// Create and configure logger instance
const logger = winston.createLogger({
  // This can be overridden by the user by setting the COMPOSIO_LOGGING_LEVEL environment variable
  // Only this or higher priority logs will be shown
  level: getLogLevel(),
  levels: LOG_LEVELS,
  format: logFormat,
  transports: [
    new winston.transports.Console({
      handleExceptions: false,
      handleRejections: false
    })
  ],
  exitOnError: false // Prevent crashes on uncaught exceptions
});
 
export default logger;