@opuscapita/logger
This module provides simple common logging. For further details of how to use this module, please have a look at the wiki.
Currently this module is released manually
Known issues
Versions 1.3.x had an issue where logging logic was reversed (more severe logs not logged while less severe logged).
Versions
See https://github.com/OpusCapita/logger/releases
Minimum setup
Install logger to your project:
npm install @opuscapita/logger
After that you can directly use the logger component.
const Logger = require('@opuscapita/logger');
const logger = new Logger({context:{name:"My preferably unique logger name"});
logger.info('Hello world!');
logger.trace('Very detailed logs', {details: 'Variable data'});
logger.debug('Detailed logs, normally off', {details: 'Variable data'});
logger.info('Information', {details: 'This is normally logged on production - try to use it only when needed'});
logger.warn('Something wrong but handled', {error: errorThrown, details: 'It is something that is worth looking into but not an issue that require immediate work'});
logger.error('Something bad happened - someone MUST look at it', {details: 'There is a transaction broken or something similar - customer is impacted and someone needs to solve the issue'});
logger.fatal('System is down', {details: 'Use it is major outage happened (and likely system is not reliable)'});
Default configuration
The default configuration object provides hints about what the module's standard behavior is like and which configuration options are available. For further details about the API, please have a look at the wiki.
{
defaultLogLevel : Logger.LogLevel.Info,
minLogLevel : Logger.LogLevel.Info,
outputStreams : {
[Logger.LogLevel.Trace] : process.stdout,
[Logger.LogLevel.Debug] : process.stdout,
[Logger.LogLevel.Info] : process.stdout,
[Logger.LogLevel.Warning] : process.stdout,
[Logger.LogLevel.Error] : process.stderr,
[Logger.LogLevel.Fatal] : process.stderr
},
context : {
name: null,
serviceName : Logger.serviceName,
serviceInstanceId : 0,
correlationId : null,
userId : null,
requestUri : null
}
}
Note that some settings would become obsolete in the future - logging configuration should be set by the environment, not by code. And the environment is preferably created by a code ;)
- defaultLogLevel and minLogLevel should dynamically change (user will be able to change configuration during runtime in the future)
- outputStreams could become more abstract - one can print to something else than a terminal/file but the concept generally is not planned to change
- context is going to move into local storage instead - logger shouldn't be contextified, session (call chain) should be
Configuration
- You should create a file
logger.json
in your project directory. - You can have file
etc/logger.json
that takes precedence before logger.json
if found. - You can set environment variable LOGGER_CONFIG_FILE to a path that takes precedence before the two above.
The first file found - it would be the only used and monitored for changes.
See https://github.com/OpusCapita/logger/wiki/Configuration
An example self-explaining logger.json with explanation
{
"serviceName": {
"my service name": "Debug"
},
"name": {
"my preferably unique logger name": "Trace",
"some logger that I am not interested in": "Warning"
},
"requestUri": {
"/api/health/check": "Fatal",
"/api/buggy/endpoint": "Trace"
},
"userId": {
"superduperadmin": "Trace"
},
"default": {
"minLogLevel": "Info",
"defaultLogLevel": "Info",
}
"--alternative default configuration entries you might consider--": {
"formatter": "terminal",
"formatter": "ci",
"formatter": "ci-wide",
"formatter": "color"
}
}