import pino from 'pino';

function resolveLevel(): string {
  if (process.env.CW_LOG_LEVEL) return process.env.CW_LOG_LEVEL;
  return process.env.NODE_ENV === 'development' ? 'debug' : 'info';
}

export const logger = pino(
  {
    name: 'cw',
    level: resolveLevel(),
    ...(process.env.CW_LOG_PRETTY === '1' && {
      transport: {
        target: 'pino-pretty',
        options: {
          colorize: true,
          ignore: 'pid,hostname',
          translateTime: 'HH:MM:ss.l',
        },
      },
    }),
  },
  process.env.CW_LOG_PRETTY === '1' ? undefined : pino.destination(2),
);

export function createModuleLogger(module: string) {
  return logger.child({ module });
}
