JSPM

  • Created
  • Published
  • 0
  • Score
    100M100P100Q44508F
  • License MIT

support for loglevels and correlation_id

Package Exports

  • @inctasoft/simple-log-ts
  • @inctasoft/simple-log-ts/dist/src/log.js

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 (@inctasoft/simple-log-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

SonarCloud results

simple-log-ts

npm install @inctasoft/simple-log-ts

Exposes a Log class with debug, info, warn, error and crit methods.

  • Logs are in JSON format, useful for parsing from log ingesting services
  • Error, Map Set objects are trnsformed into JSON and also printed
process.env.LOGLEVEL active methods notes
DEBUG debug,info,warn,error,crit
INFO info,warn,error,crit
WARN warn,error,crit default, if no LOGLEVEL is present
ERROR error,crit Both crit and error use console.error and accept optional second Error argument
SILENT
(or any other value)
crit Lets you silence all logs, if not using crit method(as it is always active, no matter of LOGLEVEL value)

Examples:

  • Empty config
import { Log } from "@inctasoft/simple-log-ts";

const log = new Log();
log.error("oops something hapened, new Error('some err msg'));

results in:

{"timestamp":"2023-10-11T21:50:47.405Z","level":"ERROR","message":"oops something hapened","correlation":"undefined","[Error]":{"stack":"Error: some err msg\n    at Object......","message":"some err msg"}}```
  • Printing complex objects, and providing correlation_id
import { Log } from "@inctasoft/simple-log-ts";

const log = new Log({ correlation_id: 'some_guid' });
log.warn({
    a: 1, b: 'xyz', my_set: new Set(['foo', 'bar']), nested: {
        my_arr: [
            'elem1',
            new Map([['mapKey', {
                prop1: 1,
                prop2: new Date()
            }]])]
    }
});

results in:

{"timestamp":"2023-10-11T21:43:13.765Z","level":"WARN","message":{"a":1,"b":"xyz","my_set":["foo","bar"],"nested":{"my_arr":["elem1",{"mapKey":{"prop1":1,"prop2":"2023-10-11T21:43:13.765Z"}}]}},"correlation":"some_guid"}
  • If you are interested in which transformed objects were of Map or Set types, provide printMapSetTypes: true
  • If you are not into using correlation_id, provide printCorrelation: false
import { Log } from "@inctasoft/simple-log-ts";

const log = new Log({ printMapSetTypes: true, printCorrelation: false});
log.warn({
    a: 1, b: 'xyz', my_set: new Set(['foo', 'bar']), nested: {
        my_arr: [
            'elem1',
            new Map([['mapKey', {
                prop1: 1,
                prop2: new Date()
            }]])]
    }
});

log statement:

{"timestamp":"2023-10-11T22:04:00.503Z","level":"WARN","message":{"a":1,"b":"xyz","my_set":{"[Set]":["foo","bar"]},"nested":{"my_arr":["elem1",{"[Map]":{"mapKey":{"prop1":1,"prop2":"2023-10-11T22:04:00.503Z"}}}]}}}

CICD

Using the template repository

  • Upon creating a repository from the template the CICD pipeline will fail for the sonarcloud step
  • You would want to first
    • remove other files, change contents of package.json, etc.
    • make sure these secrets exists, have access to your repo and are valid:
      • PAT_TOKEN_GHA_AUTH the token of the account to setup git for automatic version bumps and mergebacks in dev. Needs a repo scope
      • SONAR_TOKEN - sonar cloud token. You will need a https://sonarcloud.io/ account and a corresponding project
      • NPM_TOKEN - NPM token (classic). You will need a https://www.npmjs.com/ account