Skip to content

ghostoy/logger

 
 

Repository files navigation

@midwayjs/logger

PRs Welcome

@midwayjs/logger is a log module extended based on winston, which is suitable for log output under a single process, and supports multiple formats and customizations.

Install

$ npm install @midwayjs/logger --save

Create Logger

import { loggers } from '@midwayjs/logger';

const logger = loggers.createLogger('logger', {
  // some logger options
})

Create console, file and error Logger.(default logger, include console, file and error 3 transports)

const logger = loggers.createLogger('logger', {
  dir: '...',
  fileLogName: 'app.log',
  errorLogName: 'error.log',
})

Create console logger(just disable file and error transports)

const logger = loggers.createLogger('consoleLogger', {
  enableFile: false,
  enableError: false,
})

Logger Output Method

logger.debug('debug info');
logger.info('启动耗时 %d ms', Date.now() - start);
logger.warn('warning!');
logger.error(new Error('my error'));

Logger Level

log level is divided into the following categories, and the log level decreases sequentially (the larger the number, the lower the level):

const levels = {
  none: 0,
  error: 1,
  trace: 2,
  warn: 3,
  info: 4,
  verbose: 5,
  debug: 6,
  silly: 7,
  all: 8,
}

Set level for all transports

const logger = loggers.createLogger('logger', {
  dir: '...',
  level: 'warn',
  fileLogName: 'app.log',
  errorLogName: 'error.log',
});

// not output
logger.debug('debug info');

// not output
logger.info('debug info');

Format

Change file and error logger format.

const logger = loggers.createLogger('logger', {
  dir: '...',
  level: 'warn',
  fileLogName: 'app.log',
  errorLogName: 'error.log',
  format: info => {
    return `${info.timestamp} ${info.message}`;
  }
});

info is a winston metadata we called 'MidwayTransformableInfo' and include some default value;

Logger Options

find more options in interface.

License

MIT

Packages

No packages published

Languages

  • TypeScript 99.4%
  • JavaScript 0.6%