@evokegroup/logger
v3.0.0
Published
Console logging based on severity.
Downloads
4
Keywords
Readme
@evokegroup/logger
Console logging based on severity.
Class: Logger
constructor(severity, { output, retain, onLog })
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| severity | Logger.Severity, number, string
| Logger.Severity.Disabled
| The maximum severity to output |
| output | boolean
| true
| Output the messages to the console |
| retain | Logger.Severity, number, string
| Logger.Severity.Disabled
| Message severity level to retain for later retrieval via getLogs()
|
| onLog | function
| | A function to call whenever a message is logged regardless of severity |
Example
const Logger = require('@evokegroup/logger');
function doSomethingWithLogs(log) {
// All logged messages are passed to this function as they are logged.
// example: log = {
// severity: 2,
// logged: true,
// date: '2021-02-07T16:43:32.233Z',
// messages: [
// 'Doing something'
// ]
// }
}
const logger = new Logger(Logger.Severity.Info, { output: true, retain: Logger.Severity.Error, onLog: doSomethingWithLogs});
logger.info('Doing something'); // logged to console
logger.debug('Data', { firstName: 'John', lastName: 'Doe' }); // not logged to console
logger.error('An error occurred', new Error('Bad data')); // logged to console
// do other stuff
doSomthingWithTheLog(logger.getLogs()); // Only errors are retained and returned
Properties
| Property | Type | Access | Description |
| -------- | ---- | ------ | ----------- |
| severity | Logger.Severity
| get | The Logger.Severity
level |
Methods
getLogs(severity)
Get the logged messages
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| severity | Logger.Severity
| The retain severity | The minimum severity level. If not given falls back on the retain severity level passed when the Logger was instantiated |
Example
const Logger = require('@evokegroup/logger');
const logger = new Logger(Logger.Severity.Error, { retain: Logger.Severity.Debug });
logger.info('Doing something'); // not logged to console
logger.debug('Data', { firstName: 'John', lastName: 'Doe' }); // not logged to console
logger.error('An error occurred', new Error('Bad data')); // logged
const allRetainedLogs = logger.getLogs();
/*
[{
severity: 2, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Doing something']
}, {
severity: 3, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Data', { firstName: 'John', lastName: 'Doe' }]
}, {
severity: 0, logged: true, date: '2021-02-07T16:43:32.233Z', messages: ['An error occurred', Error: Bad data)]
}]
*/
const noDebug = logger.getLogs(Logger.Severity.Info);
/*
noDebug = [{
severity: 2, logged: false, date: '2021-02-07T16:43:32.233Z', messages: ['Doing something']
}, {
severity: 0, logged: true, date: '2021-02-07T16:43:32.233Z', messages: ['An error occurred', Error: Bad data)]
}]
*/
debug(message[, ...message])
Log messages with a Logger.Severity.Debug
severity level and using console.debug
.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| messages | *
| | The messages to log |
Example
const Logger = require('@evokegroup/logger');
const logger_1 = new Logger(Logger.Severity.Debug);
logger_1.debug('Details', { firstName: 'John', lastName: 'Doe' });
// Expected output:
// Details
// { firstName: 'John', lastName: 'Doe' }
const logger_2 = new Logger(Logger.Severity.Error);
logger_2.debug('Details', { firstName: 'John', lastName: 'Doe' }); // This message will not be logged since the logger's maximum severity level is less than Logger.Severity.Debug
error(message[, ...message])
Log messages with a Logger.Severity.Error
severity level and using console.error
.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| messages | *
| | The messages to log |
info(message[, ...message])
Log messages with a Logger.Severity.Info
severity level and using console.info
.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| messages | *
| | The messages to log |
log(severity, message[, ...message])
Log messages
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| severity | Logger.Severity, number, string
| Logger.Severity.Disabled
| The message severity level |
| messages | *
| | The messages to log |
Example
const Logger = require('@evokegroup/logger');
const logger = new Logger(Logger.Severity.Error);
logger.log(Logger.Severity.Error, 'Error');
// Expected output:
// Error
warn(message[, ...message])
Log messages with a Logger.Severity.Warning
severity level and using console.warn
.
| Parameter | Type | Default | Description |
| --------- | ---- | ------- | ----------- |
| messages | *
| | The messages to log |
Enumeration: Logger.Severity
| Property | Value |
| -------- | ----- |
| Disabled | -1
|
| Error | 0
|
| Warning | 1
|
| Info | 2
|
| Debug | 3
|