@osaas/logging
v1.1.2
Published
OSaaS logging library
Keywords
Readme
@osaas/logging
Tiny and simple logging framework to align the OSaaS platform services and UI. Goal is to be very simple to use and do not taint the code base to much. Logging should be encouraged. Output is a logfmt parse:able log string.
Requirements
Private NPM library that requires access to @osaas organization on npmjs.
Installation / Usage
npm install --save @osaas/loggingimport { Configure, Log } from '@osaas/logging';
Configure({ component: 'mymicroservice' }); // Only need to be done once
function main() {
Log().info('this is logged on the info level');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="this is logged on the info level"
Log().warn('and', 'this', 'is', 'a', 'warning');
// level=warn component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="and this is a warning"
Log().error(new Error('an error message'));
// level=error component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="an error message"
Log().debug('this is not shown unless LOG_DEBUG is set');
//
Log().id('12345').info('decorated with id');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z id=12345 msg="decorated with id"
Log().customer('eyevinn').info('decorated with customer');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z id=12345 msg="decorated with customer"
Log().id('12345').customer('eyevinn').info('decorated with id and customer');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z id=12345 customer=eyevinn msg="decorated with id and customer"
Log().info('providing custom logfmt key value pairs', { 'requestid', '12345' });
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z requestid=12345 msg="providing custom logfmt key value pairs"
}When environment variable LOG_DEBUG is set.
import { Configure, Log } from '@osaas/logging';
Configure({ component: 'mymicroservice' }); // Only need to be done once
function main() {
Log().error(new Error('an error message'));
// When LOG_DEBUG env variable is set it will dump the full stack trace of the error object
// level=error component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="Error: an error message ...
Log().debug('this is not shown unless LOG_DEBUG is set');
// level=debug component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="this is not shown unless LOG_DEBUG is set"Logging for a sub component of the application (module)
import { Configure, Log } from '@osaas/logging';
Configure({ component: 'mymicroservice' });
function moduleA() {
const Log = SubComponent('moduleA');
Log().info('this is a subcomponent');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z subcomponent=moduleA msg="this is a subcomponent"
}
Log().info('this is main component');
// level=info component=mymicroservice ts=2023-06-07T08:44:57.000Z msg="this is main component"