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
simple-ts-log
npm install @inctasoft/simple-log-ts
Exposes a Log
class that could be initialized with a correlation_id
string. Each log method will always print the correlation_id
, even if it is not provided (in this case prints UNKNOWN
). Useful when you explore logs for events, that should have it, but for some reason it is missing.
If you do not want to use correlation_id
, initialize the log by new Log({skipCorrelation: true})
.
process.env.LOGLEVEL
controls which log methods are active.['DEBUG', 'INFO', 'WARN', 'ERROR', 'CRIT']
- default level is
WARN
(ifprocess.env.LOGLEVEL
is not present) log.crit
always prints, even if you setprocess.env.LOGLEVEL
to something different from known levels
- The Log will transform
Map
andSet
objects so that they are also printed - The Log can optionally print types of objects being logged (by passing
printTypes: true
in the constructor).
Example usage:
import { Log } from "@inctasoft/simple-log-ts";
const log = new Log({ correlation_id: 'my_correlation_id' });
const my_object = {
a: 1, b: 'xyz', c: {
nested: ['elem1', new Map([['mapKey', { prop1: 1, prop2: new Date() }]]), 3],
more_nested: { nested_1: ['elem1_1', new Set(['a', 'b', new Map([['foo', 'bar']])]), 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":"2023-10-11T10:40:34.415Z","level":"WARN","correlation":"my_correlation_id","message":{"a":1,"b":"xyz","c":{"nested":["elem1",{"mapKey":{"prop1":1,"prop2":"2023-10-11T10:40:34.415Z"}},3],"more_nested":{"nested_1":["elem1_1",["a","b",{"foo":"bar"}],5]}}}}
{"timestamp":"2023-10-11T10:40:34.415Z","level":"ERROR","correlation":"my_correlation_id","message":"Lorem ipsum"}
{"timestamp":"2023-10-11T10:40:34.415Z","level":"CRIT","correlation":"my_correlation_id","message":42}
- Notice that only 'WARN', 'ERROR' and 'CRIT' log statements are printed. This is because
process.env.LOGLEVEL
was not set. Seelog.spec.ts
for details. - Notice that both
CRIT
andERROR
levels uses console.error stream. However by settingprocess.env.LOGLEVEL
toCRIT
one can filter out other errors, leaving only those logged by thecrit
method. OR, you can completley silence the log if you never uselog.crit
, and setLOGLEVEL
to anything different than 'DEBUG','INFO','WARN', or 'ERROR'. - 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
- PRs to
dev
,main
,release/**
,hotfix/**
will trigger github workflow to Build, Test, Sonarcloud scan - On push to
main
,release/**
orhotfix/**
, commits are pulled back indev
branch - On push to
main
:- package version is bumped depending on commit messages
- see https://github.com/phips28/gh-action-bump-version#workflow on commit messages
- (version bump commit will be automerged in
dev
from 2.)
- new tag is being created with the new version
- npm package with the new version is pushed to https://registry.npmjs.org/
- npm package with the new version is pushed to https://npm.pkg.github.com/
- (TODO)
gh release
is created ! https://medium.com/giant-machines/releases-the-easy-way-3ec1c2c3502b
- package version is bumped depending on commit messages
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 arepo
scopeSONAR_TOKEN
- sonar cloud token. You will need a https://sonarcloud.io/ account and a corresponding projectNPM_TOKEN
- NPM token (classic). You will need a https://www.npmjs.com/ account