JSPM

  • Created
  • Published
  • 0
  • Score
    100M100P100Q43831F
  • 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-ts-log

npm install @inctasoft/simple-log-ts

Exposes a Log class that is to be initialized with a correlation_id string. Once a log instance is created, each log method will also print the correlation_id.

Useful in event driven apps where each event carries info that you would later want to search for, and correlate with other events.

  • if data passed is a primitive it will be wrapped as logData={data} and then, applied is
formatWithOptions({ colors: true, depth: 10, showHidden: false }, '%j', {
                timestamp: new Date().getTime(),
                level: loglevel,
                correlation: this._correlation_id,
                ...logData
            }) 
  • uses LOGLEVEL environment variable to decide which log statements are to be printed.

Example usage:

import { Log } from "./log";

const my_correlation_id = {correlation_id: 'some_guid'} 

const log = new Log(my_correlation_id);

const my_object = { a: 1, b: 'xyz', c: { nested: ['elem1', 'elem2', 3], more_nested: { nested_1: ['elem1_1', 'elem1_2', 5] } } };
const my_string = 'Lorem ipsum';
const my_number = 42;

log.debug(my_object);
log.info(my_object);
log.warn(my_object);
log.error(my_string);
log.crit(my_number);

result:

{"timestamp":1696816253862,"level":"WARN","correlation":"my_correlation_id","a":1,"b":"xyz","c":{"nested":["elem1","elem2",3],"more_nested":{"nested_1":["elem1_1","elem1_2",5]}}}
{"timestamp":1696816253863,"level":"ERROR","correlation":"my_correlation_id","data":"Lorem ipsum"}
{"timestamp":1696816253863,"level":"CRIT","correlation":"my_correlation_id","data":42}
  • Notice how only WARN, ERROR and CRIT log statements are printed. This is because process.env.LOGLEVEL was not set and in this case WARN level is assumed. See log.spec.ts for details.
  • Notice that both CRIT and ERROR levels uses console.error stream. However by setting process.env.LOGLEVEL to CRIT one can filter out other errors, leaving only those logged by the crit method.
  • If you set LOGLEVEL to something different from DEBUG,INFO,WARN,ERROR or CRIT, for example LOGLEVEL=SILENT, CRIT level is still printed
  • For more sophisticated logger in the context of AWS serverless, you may want to consider using https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/

CICD Workflow

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 change contentsof package.json adding the name of your package, dependencies, 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