Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eggjs/egg-logger
Egg logger
https://github.com/eggjs/egg-logger
egg egg-logger logger
Last synced: about 1 month ago
JSON representation
Egg logger
- Host: GitHub
- URL: https://github.com/eggjs/egg-logger
- Owner: eggjs
- License: mit
- Created: 2016-06-18T07:01:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-05-08T03:30:16.000Z (7 months ago)
- Last Synced: 2024-10-29T21:05:53.510Z (about 1 month ago)
- Topics: egg, egg-logger, logger
- Language: JavaScript
- Homepage:
- Size: 236 KB
- Stars: 149
- Watchers: 26
- Forks: 45
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-logger - 日志。 ![](https://img.shields.io/github/stars/eggjs/egg-logger.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-logger.svg?style=flat-square) (仓库 / [内置插件](https://eggjs.org/zh-cn/basics/plugin.html#%E6%8F%92%E4%BB%B6%E5%88%97%E8%A1%A8))
README
# egg-logger
[![NPM version][npm-image]][npm-url]
[![CI](https://github.com/eggjs/egg-logger/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/egg-logger/actions/workflows/nodejs.yml)
[![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
[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-loggerEgg logger.
![diagram](diagram.png)
Including two base class, `Logger` and `Transport`:
- Transport: Save log to file, stdout/stderr and network.
- Logger: A logger can contains multi transports.---
## Install
```bash
$ npm i egg-logger
```## Usage
Create a `Logger` and add a file `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'); // only output to stdout
logger.info('info foo');
logger.warn('warn foo');
logger.error(new Error('error foo'));
```### Enable / Disable Transport
```js
logger.disable('file');
logger.info('info'); // output nothing
logger.enable('file');
logger.info('info'); // output 'info' string
```### Duplicate
Duplicate error log to other logger.
Accept an `options.excludes` to special whether excludes some tranports.
```js
logger.duplicate('error', errorLogger, { excludes: [ 'console' ]});
logger.error(new Error('print to errorLogger')); // will additional call `errorLogger.error`
```### Redirect
Redirect special level log to other logger.
```js
oneLogger.redirect('debug', debugLogger); // all debug level logs of `oneLogger` will delegate to debugLogger
```### Reload
```js
logger.reload(); // will close the exists write stream and create a new one.
```### Custom Transport
You can make your own `Transport` for logging,e.g.: send log to your logging server.
```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 logger level
set environment 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)
|[
semantic-release-bot](https://github.com/semantic-release-bot)
|[
mansonchor](https://github.com/mansonchor)
|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[
whxaxes](https://github.com/whxaxes)
|[
Jeff-Tian](https://github.com/Jeff-Tian)
|[
waitingsong](https://github.com/waitingsong)
|[
sjfkai](https://github.com/sjfkai)
|[
congyuandong](https://github.com/congyuandong)
|[
lix059](https://github.com/lix059)
|
[
killagu](https://github.com/killagu)
|[
duqingyu](https://github.com/duqingyu)
|[
AmazingCaddy](https://github.com/AmazingCaddy)
|[
linrf](https://github.com/linrf)
|[
XadillaX](https://github.com/XadillaX)
|[
luckydrq](https://github.com/luckydrq)This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Wed May 08 2024 09:39:22 GMT+0800`.