Generic Logger
Idea is to give best of Winston, Bunyan and npmlog
Eactly like npmlog , but giving a snippet here
// Use 1 logger throughout application
var log = require('lgr');
// App invoked with -v ? set verbose level
log.setLevel('verbose');
log.info('gateway', 'Check', null);
For each log level name and priority are mandatory arguments. Mandatory args
- name
- priority
Optional args
- Style ( default info style )
- Stream ( default process.stdout)
- LogFormat ( default )
- tsFormat (default YYYY-MM-DD HH:mm:ss)
log.setLevel('verbose'); // set level
log.getLevel();
// -> now returns 'verbose'
log.getLevels(); // You can query the levels you can `setLevel` to
/*
Returns:
{ silly: -Infinity,
verbose: 1000,
info: 2000,
http: 3000,
warn: 4000,
error: 5000,
silent: Infinity,
critical: 6000 }
*/
log.addLevel('wall', 3500); // Add custom levels
log.addLevel('hell', 6000, { fg : 'red', 'bg' : 'yellow' }, 'hell!', '<%= prefix %> <%= ts %> <%= ram %> [<%= uptime %>] [<%= count %>] <%= __FILE__ %>:<%= __FUNC__ %>:<%= __LINE__ %>:<%= __COLM__ %> <%= msg %>', process.stderr);
// Edit level setting : done by same function call
// log.editLevel(levelName, property, value) , properties are ['weight', 'style', 'dispPrefix', 'logFormat', 'stream']
log.editLevel('info', 'stream', someStream)
Variables
- prefix : info, error , etc. from log level
- msg : The message that was passed to log
- ram : will give memory usage from process.memoryusage
- ts : current timestamp
- uptime : uptime of process
- pid : process id
- count : Log count , global for this process
- hostname : system hostname
- FUNC : will invoke stacktrace and last visited function of code
- FILE : will invoke stacktrace and last visited file at this log
- LINE : will invoke stacktrace and last visited line at this log
- COLM : will invoke stacktrace and show column number.
// and "__FUNC__", "__FILE__", "__LINE__", "__COLM__". C Forever. :D
log.setLogFormat('<%= ts %> [<%= uptime %>] ');
//you can also set log formats for specific log levels
log.setLogFormat('info','<%= ts %>');
- Only Prefix colors can be user controlled.
- Different color for system generated information.
- The user specified data is in white.
To change timeformat for each level please change by editLevel
To change timeformat for all levels throughout
log.updateTsFormat('YYYY-MM-DD HH:MM:SS.sss');
NOTE : updateTsFormat
actually gets all levels and overwrite the timestamp for each level.
- Priority based log levels.
- Streams can be tied to each level making it easier to redirect log anywhere.
- User defined formatted logs with os specific and stack information.
- Decision to not incorporate file saving, logs in rabbitmq and to use streams instead comes from the learning of winston which incorporate transport system. this is designed to be leightweight and users must implement their own stream to make use of log outouts.
Just run 'npm test'