Skip to the content.

console-log-json

All Contributors

title

A universal JSON logger that plugs in to the existing console.log native function.

Contributors

Logan.seongjae(Benefit)
Logan.seongjae(Benefit)

đź’»
Roberto Sebestyen
Roberto Sebestyen

💻 📖 📆 🛡️ 👀
Igor Dreher
Igor Dreher

đź’»
WesSparla
WesSparla

📖 ⚠️
Remi Kristelijn
Remi Kristelijn

đź’»

Pupose and Description

A no fuss simple drop-in replacement for console.log(), console.info(), console.error() to handle anything you throw at it and have the output be formatted to a consistent format in a single JSON line of text, including stack traces (if passing an error object), so that it can be easily parsed by tool such as LogDNA.

Features

Usage

  1. Install

    npm install console-log-json
    
  2. At the entry point of the application include the package and run LoggerAdaptToConsole()

    import { LoggerAdaptToConsole } from "console-log-json";
    LoggerAdaptToConsole();
    

    This will adapt console.log(), console.error(), etc… to take in any string, or object, in any order or any number of them, and it will log a consistently formatted single line JSON to console. For example:

    console.warn('this is a message', {'some-extra-data': 'hello'});
    

    will produce:

    {"level":"warn","message":"this is a message","some-extra-data":"hello","@timestamp":"2019-11-29T21:44:40.463Z"}
    

Environment Variable Options

To suppress some bits of the log to make it less noisy you can set these environment variables:

Examples

1. Logging an error object

const err = new Error('this is a test');
console.log('hello world', err);

Will produce:

{"level":"error","message":"hello world - this is a test","stack":"Error: this is a test    at Context.<anonymous> (console-log-json/test/logger.test.ts:260:17)    at callFn (console-log-json/node_modules/mocha/lib/runnable.js:387:21)    at Test.Runnable.run (console-log-json/node_modules/mocha/lib/runnable.js:379:7)    at Runner.runTest (console-log-json/node_modules/mocha/lib/runner.js:535:10)    at console-log-json/node_modules/mocha/lib/runner.js:653:12    at next (console-log-json/node_modules/mocha/lib/runner.js:447:14)    at console-log-json/node_modules/mocha/lib/runner.js:457:7    at next (console-log-json/node_modules/mocha/lib/runner.js:362:14)    at Immediate._onImmediate (console-log-json/node_modules/mocha/lib/runner.js:425:5)    at processImmediate (internal/timers.js:439:21)","@timestamp":"2019-11-29T21:55:33.443Z"}

2. Including extra information as an object

const extraInfo = {firstName: 'homer', lastName: 'simpson'};
console.log(extraInfo, 'hello world');

Will produce:

{"level":"info","message":"hello world","age":25,"firstName":"homer","lastName":"simpson","location":"mars","@timestamp":"2019-12-01T04:10:38.861Z"}

3. You may include multiple objects, it will deal with them all

const extraInfo1 = {firstName: 'homer', lastName: 'simpson'};
const extraInfo2 = {age: 25, location: 'mars'};
console.log(extraInfo1, 'hello world', extraInfo2);

Will produce:

{"level":"info","message":"hello world","age":25,"firstName":"homer","lastName":"simpson","location":"mars","@timestamp":"2019-12-01T04:10:38.861Z"}

4. Include Static Properties on Bootstrapping

For use cases such as integrating with OpenSearch, it’s beneficial to include static properties during the logger’s bootstrapping phase. This feature enhances log details by adding consistent, context-relevant information across all log messages, improving the logs’ usefulness for debugging and analysis.

Example: Bootstrapping Logger with Custom Static Properties

Here’s how to configure your logger to include custom static properties such as hello: 'world' and greeting: 'universe':

// Bootstrapping the logger with custom static properties
LoggerAdaptToConsole({
  customOptions: {
    hello: 'world',
    greeting: 'universe'
  }
});

// When logging a message, the defined properties will persistently appear in all subsequent console log outputs.
console.log('Example log message');

Given this configuration, any log message produced will automatically include the hello and greeting properties. For instance, a log output might look like this:

{
  "level": "info",
  "message": "Example log message",
  "hello": "world",
  "greeting": "universe",
  "@timestamp": "YYYY-MM-DDTHH:mm:ss.sssZ"
}