zero-to-one-logger
v1.0.0
Published
[![NPM version][npm-image]][npm-url] [![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [![David deps][david-image]][david-url] [![Known Vulnerabilities][snyk-image]][snyk-url] [![npm download][download-image]][down
Downloads
1
Readme
egg-logger
Egg logger.
Including two base class, Logger
and Transport
:
- Transport: Save log to file, stdout/stderr and network.
- Logger: A logger can contains multi transports.
Install
$ npm i egg-logger
Usage
Create a Logger
and add a file 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'); // only output to stdout
logger.info('info foo');
logger.warn('warn foo');
logger.error(new Error('error foo'));
Enable / Disable Transport
logger.disable('file');
logger.info('info'); // output nothing
logger.enable('file');
logger.info('info'); // output 'info' string
Redirect
Redirect error log to other logger.
logger.redirect('error', errorLogger);
logger.error(new Error('print to errorLogger')); // equal to `errorLogger.error`
Reload
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.
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');