@lumentai/logger v1.7.0
Logging Framework
Custom framework that can be used locally or with external logging providers.
Installation
Install using npm:
npm install @lumentai/logger --save
In Node.js
// import the logging library
import { Logger } from '@lumentai/logger';
// instantiate the logger
const logger = new Logger({ name: 'THE_LOGGER_NAME', level: 'trace' });
// instantiate the logger with a contact
const logger = new Logger(
{ name: 'THE_LOGGER_NAME', level: 'trace' },
{correlationId: 'aLogCorrelationId', user: {id: 'aUserId', name: 'aUserName'}, organizationId: 'anOrgId'}
);
// logging
logger.fatal('Some fatal message.', optionalMessagePayload);
logger.error('Some error message.', optionalMessagePayload);
logger.info('Some info message.', optionalMessagePayload);
logger.debug('Some debug message.', optionalMessagePayload);
logger.trace('Some trace message.', optionalMessagePayload);
// retrieve current logging context
const context = logger.context;
// check if a log level is enabled
if(logger.isInfoEnabled) {
// only goes here if INFO is enabled
}
...
await Logger.shutdown(); // make sure all logs are flushed before exiting the process
Log levels
Set log level on a logger instance
When instantiating the logger, simply pass it as a constructor argument.
const logger = new Logger({ name: 'THE_LOGGER_NAME', level: 'trace' });
Set default log level
To set the default log level, set the environment variable LOGLEVEL
.
Logger output
By default, the logger will output to console. It is possible to redirect logs to other implementations. See below to make it work with another implementation:
Datadog
Set the constructor argument with Datadog information:
const logger = new Logger({ name: 'THE_LOGGER_NAME', level: 'trace', transport: {type: 'datadog', env: 'dev', apiKey: '...' } });
Uncaught exceptions
The library will always log uncaught exceptions. By default, the process will remain alive after such error occurs. It is possible to exit the process automatically after an unhandled exception. The environment variable EXIT_ON_UNCAUGHT_EXCEPTION
should be set to true
for such case.