@digipolis/log

1.1.0 • Public • Published

CI Coverage Status npm version

npm: npmjs.com/package/@digipolis/log

@digipolis/log

log

Log helper which converts default logs to indexable json log.

Table of contents:

Spec

https://github.com/digipolisantwerpdocumentation/logging-requirements

Installing

npm:

$ npm i @digipolis/log

Yarn:

$ yarn add @digipolis/log

Configuration

Params:

Param Description Values
type (optional) Set logging mode log (default) / json / text / silent
override (optional) Set to override given console (default) true / false (default)
level (optional) Set the log level (given level and higher will be logged debug (default) / info / log / warn / error

Examples

Example:
const digipolisLogger = require('@digipolis/log');

const mylogger = digipolisLogger(console, {
  type: 'text', // log(default) | json | text
  override: false, // false(default) | true
});

// human readable for local development
mylogger.log('hello');
/*
type: 'text' -> INFO: 2021-12-01T09:43:19.173Z hello
*/

const mylogger = digipolisLogger(console, {
  type: 'json', // log(default) | json | text
  override: false, // false(default) | true
});

// human readable json local development
mylogger.log('hello');
/*
type: 'json' -> {
  message: 'hello',
  timestamp: '2021-12-01T09:44:20.565Z',
  type: [ 'technical' ],
  level: 'INFO',
  correlationId: ''
}
*/

const mylogger = digipolisLogger(console, {
  type: 'log', // log(default) | json | text
  override: false, // false(default) | true
});
// log for kibana json for production
mylogger.log('hello');
/*
type: 'log' -> {"message":"hello","timestamp":"2021-12-01T09:45:56.515Z","type":["technical"],"level":"INFO","correlationId":""}
*/

const mylogger = digipolisLogger(console, {
  type: 'log', // log(default) | json | text
  override: false, // false(default) | true
  level: "error", // debug(default) | info | log | warn | error
});
// log for kibana json for production only error
mylogger.log('logmessage'); // -> won't be visible
mylogger.error('error');
/*
type: 'log' ->
type: 'error' -> {"message":"error","timestamp":"2021-12-01T09:45:56.515Z","type":["technical"],"level":"ERROR","correlationId":""}
*/
Example (override the global log (this will impact more than just your code)):
const digipolisLogger = require('@digipolis/log');

digipolisLogger((console, {
  type: 'text', // log(default) | json | text
  override: true, // false(default) | true
});

// human readable for local development
console.log('hello');
/*
type: 'text' -> INFO: 2021-12-01T09:43:19.173Z hello
*/

digipolisLogger((console, {
  type: 'json', // log(default) | json | text
  override: true, // false(default) | true
});
// human readable json local development
console.log('hello');
/*
type: 'json' -> {
  message: 'hello',
  timestamp: '2021-12-01T09:44:20.565Z',
  type: [ 'technical' ],
  level: 'INFO',
  correlationId: ''
}
*/

digipolisLogger((console, {
  type: 'log', // log(default) | json | text
  override: true, // false(default) | true
});
// log for kibana json for production
console.log('hello');
/*
type: 'log' -> {"message":"hello","timestamp":"2021-12-01T09:45:56.515Z","type":["technical"],"level":"INFO","correlationId":""}
*/

output examples

// type: json
console.log('hello');
/*
{
  message: 'hello',
  timestamp: '2021-12-01T09:44:20.565Z',
  type: [ 'technical' ],
  level: 'INFO',
  correlationId: ''
}
*/

// type: json
console.error('hello');
/*
{
  message: 'hello',
  timestamp: '2021-12-01T09:44:20.565Z',
  type: [ 'technical' ],
  level: 'ERROR',
  correlationId: ''
}
// type: json
*/

console.error(new Error('Errormessage'));
/*
{
  message: 'Errormessage Error: Errormessage\n' +
    '    at Object.<anonymous> (/Users/oliviervandenmooter/Projects/Digipolis/diglog/example/index.js:17:13)\n' +
    '    at Module._compile (node:internal/modules/cjs/loader:1095:14)\n' +
    '    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)\n' +
    '    at Module.load (node:internal/modules/cjs/loader:975:32)\n' +
    '    at Function.Module._load (node:internal/modules/cjs/loader:816:12)\n' +
    '    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)\n' +
    '    at node:internal/main/run_main_module:17:47',
  timestamp: '2021-12-01T11:00:13.073Z',
  type: [ 'technical' ],
  level: 'ERROR',
  correlationId: ''
}
*/

// structured message
console.log({ message: 'logmessage2', timestamp: 'timestamp123' })
/*
{
  message: 'logmessage2',
  timestamp: 'timestamp123',
  type: [ 'technical' ],
  level: 'INFO',
  correlationId: ''
}

// structured message with extra params
*/
console.log({ message: 'logmessage2', timestamp: 'timestamp123', extra_param1: "extra_value" });

/*
{
  message: 'logmessage2 Extrainfo: {"extra_param1":"extra_value"}',
  timestamp: 'timestamp123',
  type: [ 'technical' ],
  level: 'INFO',
  correlationId: ''
}
*/

Running the tests

Run the tests in this repo:

$ npm run test
$ npm run coverage

Dependencies

none.

Versioning

We use SemVer

for versioning. For the released version check changelog / tags

Contributing

Pull requests are always welcome, however keep the following things in mind:

  • New features (both breaking and non-breaking) should always be discussed with the repo's owner. If possible, please open an issue first to discuss what you would like to change.
  • Fork this repo and issue your fix or new feature via a pull request.
  • Please make sure to update tests as appropriate. Also check possible linting errors and update the CHANGELOG if applicable.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Package Sidebar

Install

npm i @digipolis/log

Weekly Downloads

52

Version

1.1.0

License

MIT

Unpacked Size

16.5 kB

Total Files

10

Last publish

Collaborators

  • trianglejuice
  • jsebrech
  • lievenvg
  • stefanpante
  • vademo
  • jan-bart