node-metalog v1.0.2
Node Metalog
This is a logging utility for Node that wraps console logging methods to automatically log metadata such as the file, function and line number where the log call originated from.
Installation
To install the package using npm:
npm install node-metalog
Integration
To integrate the logger with your package/application:
// Imagine this file is located here: /var/task/tool/index.js
const log = require('node-metalog')
function logInfo () {
log.info('An informational log message')
}
// Based on the default configuration, the above method will log a message similar to:
// "2018-02-07T08:00:10-00:00 /var/task/tool/index.js logInfo 24 An informational log message"
function logWarn () {
log.warning('A warning log message')
}
// Based on the default configuration, the above method will log a message similar to:
// "2018-02-07T08:00:10-00:00 /var/task/tool/index.js logWarn 30 A warning log message"
function logError (err) {
log.info('An error', err)
}
// Based on the default configuration, the above method will log a message similar to:
// "2018-02-07T08:00:10-00:00 /var/task/tool/index.js logError 36 An error"
// Error detail...
Configuration
You can override the configuration of the logger by creating a configuration file named node-metalog.json in the root of your application as follows:
{
"LogFormat": "${time} ${file} ${function} ${line} ${type} ${message}",
"TokenPrefix": "${",
"TokenSuffix": "}",
"DateTimeFormat": ""
}
LogFormat
You can change the format of your log messages by modifying the string and moving/removing the tokens. Currently the tokens that are supported are:
- time: this will output the date/time of the log call, you can specify the foramt using the DateTimeFormat configuration setting (see below).
- file: this will output the resolved filename from where the log call originated.
- function: this will output the function name from where the log call originated.
- line: this will output the line number from where the log call originated.
- type: this will output the type of log call, i.e. INFO, WARN, ERROR.
- message: this will output the message you pass to the log call.
TokenPrefix
You can change the prefix used for the tokens in the log message template.
TokenSuffix
You can change the suffix used for tokens in the log message template.
DateTimeFormat
You can specify the format for the time segment of the log, moment is used to generate and format time so you can provide a format string as per the moment documentation. If you don't specify a format string in your configuration, the default moment format is used (ISO 8601).
See Node Package Configuration Loader for further info on how configuration is managed.
Examples
Changing the default log format
This example overrides the default log format by adding a node-metalog.json to the application's root directory and modifies the format of the log message:
Configuration (node-metalog.json):
{
"LogFormat": "${time} ${file}::${function}()[${line}] ${type}: ${message}"
}
Code (index.js):
const log = require('node-metalog')
log.info('My info log message')
log.warn('My warning log message')
log.error('My error log message')
Log output:
2018-02-07T08:00:10-00:00 /var/task/tool/index.js::myFunction()[2] INFO: My info log message
2018-02-07T08:00:10-00:01 /var/task/tool/index.js::myFunction()[3] WARN: My warning log message
2018-02-07T08:00:10-00:02 /var/task/tool/index.js::myFunction()[4] ERROR: My error log message
Changing the date/time format
This example overrides the default date/time format used in the log entries by adding a node-metalog.json to the application's root directory and specifying the format to use for the date/time:
Configuration (node-metalog.json):
{
"LogFormat": "${time} ${file}::${function}()[${line}] ${type}: ${message}",
"DateTimeFormat": "DD-MM-YYYY-HH-mm-SS",
}
Code (index.js):
const log = require('node-metalog')
log.info('My info log message')
log.warn('My warning log message')
log.error('My error log message')
Log output:
07-02-2018-08-10-00 /var/task/tool/index.js::myFunction()[2] INFO: My info log message
07-02-2018-08-10-01 /var/task/tool/index.js::myFunction()[3] WARN: My warning log message
07-02-2018-08-10-02 /var/task/tool/index.js::myFunction()[4] ERROR: My error log message
Reference: Date/time formats
Changing the log message token prefx/suffix
This example overrides the default prefix and suffix used for the tokens in the log message template by adding a node-metalog.json to the application's root directory and changing the token prefix and suffix:
Configuration (node-metalog.json):
{
"LogFormat": "##time## ##file##::##function##()[##line##] ##type##: ##message##",
"TokenPrefix": "##",
"TokenSuffix": "##",
}
Code (index.js):
const log = require('node-metalog')
log.info('My info log message')
log.warn('My warning log message')
log.error('My error log message')
Log output:
2018-02-07T08:00:10-00:00 /var/task/tool/index.js::myFunction()[2] INFO: My info log message
2018-02-07T08:00:10-00:01 /var/task/tool/index.js::myFunction()[3] WARN: My warning log message
2018-02-07T08:00:10-00:02 /var/task/tool/index.js::myFunction()[4] ERROR: My error log message
Removing tokens from the log format
This example will run in AWS Lambda, which already logs a timestamp, so we remove the time token from the default log message template by adding a a node-metalog.json to the application's root directory and remove the time token in the log message template:
Configuration (node-metalog.json):
{
"LogFormat": "${file}::${function}()[${line}] ${type}: ${message}"
}
Code (index.js):
const log = require('node-metalog')
log.info('My info log message')
log.warn('My warning log message')
log.error('My error log message')
Log output:
/var/task/tool/index.js::myFunction()[2] INFO: My info log message
/var/task/tool/index.js::myFunction()[3] WARN: My warning log message
/var/task/tool/index.js::myFunction()[4] ERROR: My error log message