logger.js 849 B

1234567891011121314151617181920
  1. import winston from 'winston'
  2. import { ProductionLogger, logger as productionLogger } from './ProductionLogger.js'
  3. const { combine, timestamp, printf, colorize } = winston.format
  4. const logFormat = printf(({ level, message, timestamp }) => {
  5. return `${timestamp} [${level}] ${message}`
  6. })
  7. // 保持向后兼容的 Winston logger
  8. const winstonLogger = winston.createLogger({
  9. level: process.env.LOG_LEVEL || 'info',
  10. format: combine(timestamp(), logFormat),
  11. transports: [
  12. new winston.transports.Console({
  13. format: combine(colorize(), timestamp(), logFormat),
  14. }),
  15. ],
  16. })
  17. // 生产环境使用新的 ProductionLogger,开发环境使用 Winston
  18. export const logger = process.env.NODE_ENV === 'production' ? productionLogger : winstonLogger
  19. // 导出新的生产日志器供直接使用
  20. export { ProductionLogger, productionLogger }