# egg-logger
[![NPM version][npm-image]][npm-url]
[![build status][github-action-image]][github-action-url]
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[npm-image]: https://img.shields.io/npm/v/egg-logger.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-logger
[github-action-image]: https://github.com/eggjs/egg-logger/workflows/Node.js%20CI/badge.svg
[github-action-url]: https://github.com/eggjs/egg-logger/actions
[codecov-image]: https://codecov.io/github/eggjs/egg-logger/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/eggjs/egg-logger?branch=master
[snyk-image]: https://snyk.io/test/npm/egg-logger/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-logger
[download-image]: https://img.shields.io/npm/dm/egg-logger.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logger
Egg 的日志工具。

主要由 Logger 和 Transport 两个基类组成。
Transport 是一种写入日志的渠道,可以是终端、文件等等。
Logger 是所有日志的基类,可以进行扩展。一个 Logger 可以添加多个 Transport,只要调用一次就可以将日志写入多个地方。
---
## Install
```bash
$ npm i egg-logger
```
## Usage
创建一个 Logger,添加一个文件的 Transport
```js
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
```js
logger.disable('file');
logger.info('info'); // 不会输出
logger.enable('file');
logger.info('info'); // 开启后会输出
```
### 汇集日志
可以把日志复制一份到指定的日志对象,支持可选的 `options.excludes` 来排除对应的 tranport
```js
logger.duplicate('error', errorLogger, { excludes: [ 'console' ] });
logger.error(new Error('print to errorLogger')); // 会多调用一次 `errorLogger.error`
```
### 重定向日志
可以将日志重定向到指定的日志对象
```js
oneLogger.redirect('debug', debugLogger); // oneLogger 的调试日志将被 debugLogger 接管,输出过去。
```
### 重新加载文件
```js
logger.reload(); // will end the exists write stream and create a new one.
```
### 自定义 Transport
可以自定义一个 Transport 用于输出日志,比如发送到服务器。
```js
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](LICENSE)
## Contributors
|[
fengmk2](https://github.com/fengmk2)
|[
dead-horse](https://github.com/dead-horse)
|[
popomore](https://github.com/popomore)
|[
atian25](https://github.com/atian25)
|[
whxaxes](https://github.com/whxaxes)
|[
Jeff-Tian](https://github.com/Jeff-Tian)
|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[
mansonchor](https://github.com/mansonchor)
|[
andy-ms](https://github.com/andy-ms)
|[
DavidCai1993](https://github.com/DavidCai1993)
|[
luckydrq](https://github.com/luckydrq)
|[
XadillaX](https://github.com/XadillaX)
|[
linrf](https://github.com/linrf)
|
[
duqingyu](https://github.com/duqingyu)
|[
lix059](https://github.com/lix059)
|[
congyuandong](https://github.com/congyuandong)
|[
waitingsong](https://github.com/waitingsong)
This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Tue Mar 22 2022 23:31:56 GMT+0800`.