better-logs

3.0.4 • Public • Published

Even Better Logs for NodeJS

npm package

Build status Dependency Status Known Vulnerabilities Gitter

We've found this logger to be immensely useful and flexible for us when we develop and work. Hopefully you would find it useful as well!

npm install --save better-logs

Usage

var log = require('better-logs')('section');
 
log.format('awesome', '{{timestamp}} AWESOME: {{message}}\n');
log.awesome('custom format');
 
log.debug('debug stuff');
log.info('test');
log.warn('warning');
log.error('error with stack');

Sections

In your module, you should distinguish between different types of logs.

// server.js
var log = require('better-logs')('server');
 
// download.js
var log = require('better-logs')('download');
 
// etc.

This way, when logs for a certain module get spammy, you can hide them (or show them when you are debugging.) To do this, it's quite simple:

log.hide('server');

Then to re-enable it:

log.show('server');

Groups

This can get kind of tedious if you have lots of files/sections, so you can define a group to refer to several sections.

log.group('http', ['server', 'download', 'upload']);

Now, instead of hiding/showing individual sections, you can do:

log.hide('http');

You can get even finer control:

log.show('http');      // Show all http logs
log.hide('http/warn'); // ... except warnings

// Now:
httpLog.info('This will be shown.');
httpLog.warn('This will be hidden.');

Modes

Having to show and hide modules can get annoying if you tend to switch between looking at different things. So we've defined modes to automatically set hide/show settings for you.

log.mode('silent', {
    showByDefault: false,
    hide: ['http'],
    show: ['other-modules']
})

You can then switch between modes using:

log.mode('silent')

Formats

By default, we've defined a few formats for convenience, but you can go a step further and define your own formats too.

log.display('dateformat', 'HH:MM');
log.format('custom', '{{file}}:{{line}} custom: {{message}} {{timestamp}}');

Then to invoke it:

log.custom('hello');

Reading and writing outputs

All logs are stream.Readable, so you can simply pipe them to whatever you want. Alternatively, you can also write output to any Writable stream:

// Writes all logs to logs.txt (default is process.stdout)
log.output(fs.createWriteStream('logs.txt'));

// Writes all log.errors to ./error.log
log.output('error', './error.log');

// Writes all section logs to myWriteStream
log.output('section', myWriteStream);

// Writes all errors in section to myErrorStream
log.output('section/error', myErrorStream);

Full Documentation

  • hide([section]) - Hides all logs. If a section/group string is provided, it would only hide those logs.
  • show([section]) - Shows all logs. If a section/group string is provided, it would only show those logs.
  • reset() - Resets all visibility back to its default state.
  • modes() - Returns all defined modes
  • mode() - Returns current active mode
  • mode(modeName) - Sets the active mode
  • mode(modeName, options) - Defines modeName mode with options. Note that if options is null or false this will delete the mode.
  • groups() - Returns all defined groups
  • group(groupName) - Returns the sections/groups in groupName
  • group(groupName, members) - Defines groupName to be a group containing members. This is an array of strings representing a section or another group.
  • formats() - Returns all defined formats
  • format(logType, formatter) - Defines a format for logType (string). formatter may be a string or a function that runs when the log is called. The function gets passed along the arguments from the log call.
  • output([section], writable) - Optionally define a group, section or section/log-type string. Whenever logs happen, they will be written to the writable stream. If no section string is defined then it will write all logs to the writable stream.

Contributions welcome!

Credits

This library was initially made by the awesome team of engineers at Diamond.

If you haven't already, make sure you install Diamond!

Package Sidebar

Install

npm i better-logs

Weekly Downloads

6

Version

3.0.4

License

MIT

Unpacked Size

37.2 kB

Total Files

12

Last publish

Collaborators

  • diam
  • leander