egg-logger
egg logger
Last updated 6 months ago by fengmk2 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install egg-logger 
SYNC missed versions from official npm registry.

egg-logger

NPM version CI Test coverage Known Vulnerabilities npm download

Egg 的日志工具。

主要由 Logger 和 Transport 两个基类组成。

Transport 是一种写入日志的渠道,可以是终端、文件等等。

Logger 是所有日志的基类,可以进行扩展。一个 Logger 可以添加多个 Transport,只要调用一次就可以将日志写入多个地方。


Install

$ npm i egg-logger

Usage

创建一个 Logger,添加一个文件的 Transport

const Logger = require('egg-logger').Logger;
const FileTransport = require('egg-logger').FileTransport;
const ConsoleTransport = require('egg-logger').ConsoleTransport;

const logger = new Logger();
logger.set('file', new FileTransport({
  file: '/path/to/file',
  level: 'INFO',
}));
logger.set('console', new ConsoleTransport({
  level: 'DEBUG',
}));
logger.debug('debug foo'); // 不会输出到文件,只输出到终端
logger.info('info foo');
logger.warn('warn foo');
logger.error(new Error('error foo'));

开启/关闭 Transport

logger.disable('file');
logger.info('info'); // 不会输出
logger.enable('file');
logger.info('info'); // 开启后会输出

汇集日志

可以把日志复制一份到指定的日志对象,支持可选的 options.excludes 来排除对应的 tranport

logger.duplicate('error', errorLogger, { excludes: [ 'console' ] });
logger.error(new Error('print to errorLogger')); // 会多调用一次 `errorLogger.error`

重定向日志

可以将日志重定向到指定的日志对象

oneLogger.redirect('debug', debugLogger); // oneLogger 的调试日志将被 debugLogger 接管,输出过去。

重新加载文件

logger.reload(); // will end the exists write stream and create a new one.

自定义 Transport

可以自定义一个 Transport 用于输出日志,比如发送到服务器。

const urllib = require('urllib');
const Transport = require('egg-logger').Transport;

class UrllibTransport extends Transport {

  log(level, args, meta) {
    const msg = super.log(level, args, meta);
    return urllib.request('url?msg=' + msg);
  }
}

const logger = new Logger();
logger.set('remote', new UrllibTransport({
  level: 'DEBUG',
}));
logger.info('info');

console 默认打印级别设置

设置环境变量 NODE_CONSOLE_LOGGRE_LEVEL = 'INFO' | 'WARN' | 'ERROR'

License

MIT

Contributors


fengmk2


dead-horse


popomore


atian25


semantic-release-bot


mansonchor


whxaxes


Jeff-Tian


waitingsong


sjfkai


congyuandong


lix059


killagu


duqingyu


AmazingCaddy


linrf


XadillaX


luckydrq

This project follows the git-contributor spec, auto updated at Wed May 08 2024 09:39:22 GMT+0800.

Current Tags

  • 3.5.0                                ...           latest (6 months ago)
  • 1.8.0                                ...           latest-1 (6 years ago)

50 Versions

  • 3.5.0                                ...           6 months ago
  • 3.4.2                                ...           10 months ago
  • 3.4.1                                ...           a year ago
  • 3.4.0                                ...           a year ago
  • 3.3.1                                ...           a year ago
  • 3.3.0                                ...           2 years ago
  • 3.2.1                                ...           2 years ago
  • 3.2.0                                ...           2 years ago
  • 3.1.1                                ...           2 years ago
  • 3.1.0                                ...           2 years ago
  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.9.1                                ...           2 years ago
  • 2.9.0                                ...           2 years ago
  • 2.8.0                                ...           3 years ago
  • 2.7.1                                ...           3 years ago
  • 2.7.0                                ...           3 years ago
  • 2.6.2                                ...           3 years ago
  • 2.6.1                                ...           4 years ago
  • 2.6.0                                ...           4 years ago
  • 2.5.0                                ...           4 years ago
  • 2.4.2                                ...           5 years ago
  • 1.8.0                                ...           6 years ago
  • 2.4.1                                ...           6 years ago
  • 2.4.0                                ...           6 years ago
  • 2.3.2                                ...           6 years ago
  • 2.3.1                                ...           6 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.1                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.3                                ...           6 years ago
  • 2.0.2                                ...           6 years ago
  • 2.0.1                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.7.1                                ...           6 years ago
  • 1.7.0                                ...           6 years ago
  • 1.6.2                                ...           7 years ago
  • 1.6.1                                ...           7 years ago
  • 1.6.0                                ...           8 years ago
  • 1.5.0                                ...           8 years ago
  • 1.4.1                                ...           8 years ago
  • 1.4.0                                ...           8 years ago
  • 1.3.0                                ...           8 years ago
  • 1.2.0                                ...           8 years ago
  • 1.1.1                                ...           8 years ago
  • 1.1.0                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Downloads
Total 1
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (6)
Dev Dependencies (14)

© 2010 - cnpmjs.org x YWFE | Home | YWFE