Pino Lombok(ish)
Lombok(ish) wrapper which allows for simple pino logger creation as class member (or module variable) Also Implements NestJS interface.
Table of Contents
- Installation
- Usage
- HTTP Middleware
- Level Changer
- Marking Sensitive Logs
- Default Configurations
- Acknowledgments
- License
Installation
npm i -s pino-lombokish
npm i -D @types/pino @types/pino-http pino-pretty
Note pino-pretty is disabled when NODE_ENV==='production'. If Users want to use pretty print in production then
- install pino-pretty as production dependency (
npm i -s pino-pretty
)- override logger config during init or set NODE_ENV !== 'production'
Important!
experimentalDecorators
,emitDecoratorMetadata
,strictPropertyInitialization
compilation options must be enabled in yourtsconfig.json
file.
Usage
Important! Execute "forRoot()" AT THE VERY BEGINNING OF YOUR APP (EVEN BEFORE OTHER IMPORTS)
// main.ts; LoggerFactory.forRoot;
Inside ts/js file
// file.ts; ;logger.info'Will log "className": "file.ts" in Your Log';
With Provided class name
// file.ts; ;logger.info'Will log "className": "foo" in Your Log';
As class member - Lombok(ish) style
// foo.ts;
HTTP Middleware.
Creates Pino Http NodeJS/express middleware
;;;LoggerFactory.forRootconfig; // NOTE: Import other modules using Logger after calling LoggerFactory.forRoot();;;app.useLoggerFactory.createHttpLoggerMiddlewareconfig;app.listen3000;
Note HTTP Middleware class name is hard coded to 'http'
By Default HTTP Logger Logs any request containing "/health" as TRACE
(To Get rid of Kubernetes Health check logs)
By Default HTTP Logger will redact
- HTTP Headers (via pino redact option)
- Authorization
- x-original-uri
- x-original-url
- sec-websocket-protocol
- HTTP URL Parameters (via pino http serializers option)
- access_token
HTTP Configuration can be overriden by user. Simply call function with own provided config
;;app.useLoggerFactory.createHttpLoggerMiddlewarepinoConfig, pinoHttpOptions;
Level Changer
pino-lombokish package implements Level Changer functionality which allows users to change logger level at runtime.
Configuration
main.ts
;LoggerFactory.forRoot;
levels.json
NOTE
- If provided file does not exist Level Changer will check for its existence every 10s.
- Once the file is created Level Changer will subscribe to its changes
- When the file is removed Level Changer will start checking for its existence every 10s (again)
Marking Sensitive Logs (optional)
pino-lombokish package implements sensitive logging bindings.
This feature is not meant to redact logs.
- It is meant for log forwarders in PaaS systems (i.e splunk-forwader or fluentd).
- Log forwarders send logs to aggregators (i.e kibana)
- Log forwarders can be configured to look for specific properties in logs
- If property is present then send to specific aggregators only (i.e. aggregators which store sensitive logs only)
- If property is not present then send to all aggregators
Default Configuration
LoggerFactory.forRoot ;logger.sensitive.info'Will add { isSensitive: true } to log'
User Provided Configuration
LoggerFactory.forRoot ;logger.sensitive.info'Will add { sensitiveLog: "yes" } to log'
Default Configurations
Logger
;
HTTP Middleware
; ;
Sensitive Config
;
Acknowledgements
License
The MIT License Copyright (C) 2019 Motorola Solutions, Inc All rights reserved