All files / utils logger.ts

84% Statements 21/25
66.66% Branches 6/9
81.81% Functions 9/11
84% Lines 21/25

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 488x   8x             8x   10x 2x 9x 21x     8x 21x       10x   2x   9x           8x     21x 21x 21x 21x     10x 2x 9x     8x  
import { getEnvVariable } from './shared';
 
const levels = {
  error: 'ERROR',
  warn: 'WARN',
  info: 'INFO',
  debug: 'DEBUG'
};
 
const colors = {
  red: (str: string) => `\x1b[31m${str}\x1b[0m`,
  yellow: (str: string) => `\x1b[33m${str}\x1b[0m`,
  blue: (str: string) => `\x1b[34m${str}\x1b[0m`,
  green: (str: string) => `\x1b[32m${str}\x1b[0m`,
  gray: (str: string) => `\x1b[90m${str}\x1b[0m`
};
 
const colorize = (level: string, timestamp: string) => {
  switch (level) {
    case 'error':
      return { level: colors.red(levels[level]), timestamp: colors.gray(timestamp) };
    case 'warn':
      return { level: colors.yellow(levels[level]), timestamp: colors.gray(timestamp) };
    case 'info':
      return { level: colors.blue(levels[level]), timestamp: colors.gray(timestamp) };
    case 'debug':
      return { level: colors.green(levels[level]), timestamp: colors.gray(timestamp) };
    default:
      return { level, timestamp };
  }
};
 
const logger = {
  level: getEnvVariable("COMPOSIO_DEBUG", "0") === "1" ? 'debug' : 'info',
  log: (level: string, message: string, meta?: any) => {
    const timestamp = new Date().toLocaleTimeString();
    const { level: coloredLevel, timestamp: coloredTimestamp } = colorize(level, timestamp);
    const metaInfo = meta ? ` - ${JSON.stringify(meta)}` : '';
    console.log(`[${coloredLevel}] ${coloredTimestamp} ${message}${metaInfo}`);
  },
  error: (message: string, meta?: any) => logger.log('error', message, meta),
  warn: (message: string, meta?: any) => logger.log('warn', message, meta),
  info: (message: string, meta?: any) => logger.log('info', message, meta),
  debug: (message: string, meta?: any) => logger.log('debug', message, meta)
};
 
export default logger;