Detalhes do pacote

egg-logger

eggjs169.4kMIT3.6.1

egg logger

readme (leia-me)

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

Contributors

Made with contributors-img.

changelog (log de mudanças)

Changelog

3.6.1 (2024-12-22)

Bug Fixes

  • Transport log should return string type (#99) (fe6b496)

3.6.0 (2024-11-30)

Features

  • print AggregateError errors stack (#98) (03c38bd)

3.5.0 (2024-05-08)

Features

3.4.2 (2023-12-31)

Bug Fixes

  • should add paddingMessage on ConsoleLogger (#93) (eb73f50)

3.4.1 (2023-10-16)

Bug Fixes

  • should use paddingMessageFormatter on ConsoleTransport options (#92) (32ec755)

3.4.0 (2023-09-05)

Features

3.3.1 (2023-06-05)

Bug Fixes

  • allow EggConsoleLogger init without options (#90) (e4ff375)

3.3.0 (2023-03-03)

Features

3.2.1 (2023-01-20)

Bug Fixes

  • ctx logger paddingMessage should be a getter (#86) (5417064)

3.2.0 (2023-01-19)

Features

3.1.1 (2023-01-13)

Bug Fixes

  • use util.debuglog instead of debug module (#84) (3a5124e)

3.1.0 (2023-01-12)

Features

  • allow custom format paddingMessage (#83) (2ac591a)

3.0.1 / 2022-12-10

fixes

others

3.0.0 / 2022-12-10

features

others

2.9.1 / 2022-11-15

fixes

2.9.0 / 2022-07-27

features

2.8.0 / 2022-03-22

features

2.7.1 / 2021-12-30

fixes

2.7.0 / 2021-12-05

features

2.6.2 / 2021-08-02

fixes

2.6.1 / 2021-04-07

fixes

2.6.0 / 2021-03-23

features

others

2.5.0 / 2021-02-11

features

others

2.4.2 / 2020-04-14

others

2.4.1 / 2019-03-19

fixes

2.4.0 / 2019-03-14

features

2.3.2 / 2019-02-28

fixes

  • [f51216b] - fix: crash when format a getter-only error stack (#50) (Khaidi Chu <i@2333.moe>)

others

2.3.1 / 2019-01-03

  • fix: should not duplicate to console (#43)
  • chore: modify engine version (#42)

2.3.0 / 2019-01-01

features

others

2.2.1 / 2018-12-26

  • fix: logger should extend Map (#39)

2.2.0 / 2018-12-17

features

  • [d578857] - feat: support concentrateError and set default to duplicate (#38) (TZ | 天猪 <atian25@qq.com>)

2.1.0 / 2018-12-07

features

2.0.3 / 2018-11-19

others

2.0.2 / 2018-10-18

fixes

2.0.1 / 2018-10-09

others

2.0.0 / 2018-10-08

fixes

others

1.7.1 / 2018-07-09

fixes

1.7.0 / 2018-06-21

features

1.6.2 / 2018-04-08

fixes

1.6.1 / 2017-12-27

fixes

1.6.0 / 2017-04-28

  • feat: add level getter to Logger and Transport (#19)

1.5.0 / 2016-12-08

  • improve: don't convert utf8 string to buffer (#15)

1.4.1 / 2016-11-11

  • refactor: use .close instead of .end (#12)
  • fix: print to stderr when stream closed (#11)

1.4.0 / 2016-11-02

  • feat: write support util.format (#10)

1.3.0 / 2016-09-21

  • feat: add .unredirect() for logger (#9)
  • use Infinity to improve performance and semantics (#7)

1.2.0 / 2016-08-11

  • feat: remove fileTransport from consoleLogger (#6)

1.1.1 / 2016-08-10

  • fix: add missing write function on context logger (#5)

1.1.0 / 2016-07-27

  • feat: display all error properties (#4)

1.0.1 / 2016-07-09

  • first version