JSPM

  • Created
  • Published
  • Downloads 875
  • Score
    100M100P100Q119540F
  • License MIT

better-logging is a drop in replacement for the default logging methods of node.js

Package Exports

  • better-logging

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (better-logging) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

better-logging

Javascript comes by default with different standardized logging types. But as it stands only browsers are taking advantage of these different methods of logging. better-logging aims to improve the default logging experience of any node application, it is designed to be a drop in replacement for the default logging methods.
Since better-logging only decorates the default logging methods you won't lose any functionality provided by other tooling. better-logging is not meant to be the be all and end all of node.js logging. It is just supposed to increase the usefulness of the default logging methods.

Install: npm i better-logging

Help me help you: Buy Me A Coffee


// Default in node.js
console.debug('foo'); //  foo
console.log('foo'); //    foo
console.info('foo'); //   foo
console.warn('foo'); //   foo
console.error('foo'); //  foo

// With better-logging
require('better-logging')(console);

console.debug('foo'); //  [11:46:35] [debug] foo
console.log('foo'); //    [11:46:35] [log] foo
console.info('foo'); //   [11:46:35] [info] foo
console.warn('foo'); //   [11:46:35] [warning] foo
console.error('foo'); //  [11:46:35] [error] foo
console.line('foo'); //   foo

console.loglevel /**
 * debug: 4
 * log: 3
 * info: 2
 * warn: 1
 * error: 0
 * line: 1
 * turn off all logging: -1
 * default: 3
 */
console.color['ColorName']/**
color = {
  Black: '\033[0;30m',
  Blue: '\033[0;34m',
  Green: '\033[0;32m',
  Cyan: '\033[0;36m',
  Red: '\033[0;31m',
  Purple: '\033[0;35m',
  Brown: '\033[0;33m',
  Gray: '\033[0;37m',
  Dark_Gray: '\033[1;30m',
  Light_Blue: '\033[1;34m',
  Light_Green: '\033[1;32m',
  Light_Cyan: '\033[1;36m',
  Light_Red: '\033[1;31m',
  Light_Purple: '\033[1;35m',
  Yellow: '\033[1;33m',
  White: '\033[1;37m',
  RESET: '\033[0m'
}
*/

Better-logging calls the default implementation in the background.

require('better-logging')(console);
console.info('Hello World');
// Is the same as
console.info('[11:46:35] [info] Hello World')

It can sometimes be useful to define your own logging style, for those occasions you can overwrite the default formatting function:

require('better-logging')(console, {
  format: ctx => `${ctx.time24} ${ctx.time12} ${ctx.date} ${ctx.type} ${ctx.unix} ${ctx.STAMP('lel', console.color.Brown)} ${ctx.msg}`
});

console.debug('foo'); //  [11:44:40] [11:44:40 AM] [2/2/2019] [debug] [1549104280572] [lel] foo
console.log('foo'); //    [11:44:40] [11:44:40 AM] [2/2/2019] [log] [1549104280574] [lel] foo
console.info('foo'); //   [11:44:40] [11:44:40 AM] [2/2/2019] [info] [1549104280577] [lel] foo
console.warn('foo'); //   [11:44:40] [11:44:40 AM] [2/2/2019] [warn] [1549104280579] [lel] foo
console.error('foo'); //  [11:44:40] [11:44:40 AM] [2/2/2019] [error] [1549104280580] [lel] foo

It can also sometimes be useful to be able to react to an event being fired.

require('better-logging')(console, {
  events: [{
     onLogEmitted: log => {
        // A log just got emitted!
     },
     onLoglevelChanged: loglevel => {
       // The loglevel got changed
     }
  }] 
});

"Middleware" example

Some times the default loglevels might not fit your needs, in those cases you can redefine the loglevels to anything you like.

require('better-logging')(console, {
  logLevels: {
    debug: 0,
    error: 10,
    info: 10,
    line: 10,
    log: 10,
    warn: 10,
  }
});

console.debug('foo'); //  [11:46:35] [debug] foo
console.log('foo'); //    wont print
console.info('foo'); //   wont print
console.warn('foo'); //   wont print
console.error('foo'); //  wont print
console.line('foo'); //   wont print

In some cases you might need a specific type of argument to be pre-treated prior to logging it. Introducing argProcessor

require('better-logging')(console, {
  argProcessor: arg => {
    // will fire once per arg in (...args) of the original function call
  
    let msg = arg;
    if (typeof arg === 'number') {
      msg = arg * arg;
      // ex: log(1, 2, 3) => '1 4 9'
    }
  
    return String(msg); // Should return a string
  }
});

It's finally time for the most important option of them all... colors!

require('better-logging')(console, {
    typeColors: Color => ({
        debug: Color.Light_Purple,
        info: Color.Light_Purple,
        log: Color.Light_Purple,
        error: Color.Blue,
        warn: Color.Blue,
    }),
    stampColor: Color => Color.Light_Green
});
// The type color decides the color of the word inside the "ctx.type" stamp.
// By default the text "info" in this stamp, [info], is white, but now it can be any color you want (or that your terminal supports) :)
// The Color object passed to the typeColors function is the same object as console.color will be after the decoration.

// The stampColor decides the color of the [ ] and the color of the body of certain stamps, such as time stamp

Express middleware

I've found that i keep writing the same middleware function (for logging incoming requests) over and over again whenever i start a new project, so i decided to include it in the library from the getgo. (Don't be alarmed, its ~30 lines of code, it won't add that much to your bundle if you dont use express).

const betterLogging = require('better-logging');
const app = require('express')();

// Init betterLogging
betterLogging(console);
// Setup middleware
app.use(betterLogging.expressMiddleware(console));

app.listen(8080);

Default config:

app.use(betterLogging.expressMiddleware(console, {
  ip: {
    show: true,
    color: console.color.STAMP_COLOR
  },
  path: {
    show: true,
    color: console.color.RESET
  },
  body: {
    show: false,
    color: console.color.RESET
  },
  header: {
    show: false,
    color: console.color.RESET
  }
}));

Typescript support

// using import
import betterLogging from 'better-logging';
betterLogging(console);
console.log('Hello!') // [11:46:35] [log] Hello!
// using require
require('better-logging').default(console);
console.log('Hello!') // [11:46:35] [log] Hello!

Decorate any object

Support for decoration of arbitrary objects is considered experimental, this is due to problems with typescript support. If you intend to use better-logging purely with javascript or dont care about type support, then everything should work just fine out of the box. However if you intend to use better-logging with typescript then you should be aware that the types for the console object are hardcoded and will show up on the console object even if you chose not to decorate it. This means that console.line('foo') will look ok to typescript, but will fail during runtime.

const better = {};
require('better-logging')(better);
better.debug('foo'); //  [11:46:35] [debug] foo
better.log('foo') //     [11:46:35] [log] foo
better.info('foo'); //   [11:46:35] [info] foo
better.warn('foo'); //   [11:46:35] [warning] foo
better.error('foo'); //  [11:46:35] [error] foo
better.line('foo'); //   foo
better.loglevel = 0;
// When decorating an arbitrary object we need to trick the type system into thinking that better-logging might infact fail to decorate our object.
const better = {}; // due to some "strange" behavior with the typescript type system this has to be CONST.
if (!require('better-logging').default(better)) throw 'This will never happen';
better.log('Hello!') // [11:46:35] [log] Hello!

Custom Instance

First of all, the custom instance was designed to be used internally to make TDD easier to implement. However some advanced users might find the need to overwrite the default behavior of better-logging on a more detailed level than the current api allows. The custom instance will not log anything to the console, but will instead call the corresponding method on the implementation object. For example, calling better.info('Hi!') will call implementationObj.info('[11:46:35] [info] Hi!'). The msg parameter of these functions will be the fully formated string that usually ends up logged to the terminal.

const { CustomInstance } = require('better-logging');
const implementationObj = {
    log: msg => {},
    info: msg => {},
    debug: msg => {},
    error: msg => {},
    warn: msg => {}
};
const customLogging = CustomInstance(implementationObj);

const better = {};
customLogging(better);

See examples/custom-instance.js for a more realistic usage example.

For reference, this is how you would recreate the default instance of better-logging.

const { CustomInstance } = require('better-logging');
const betterLogging = CustomInstance(console);

const better = {};
betterLogging(better);

better.log('Works!');