📃 @benchmate/logger
logging library with support for structured logs (for cloud services) and local logging with colors
Installation
First install the library:
yarn add @benchmate/logger
Usage
ES6 imports
Create a new log.js
file, as follows:
import createLogger from '@benchmate/logger'
const makeLogger = service => createLogger('APPNAME', service)
export default makeLogger
Finally, use the logger:
import makeLogger from './log'
const { debug, info, warn, error, crit, exception } = makeLogger('SERVICENAME')
debug('Debug or trace information.')
info('Routine information, such as ongoing status or performance.')
warn('Warning events might cause problems.')
error('Error events are likely to cause problems.')
exception(new Error('something went wrong!'))
crit('Critical events cause more severe problems or outages.')
exception(new Error('fatal error'), 'crit')
node.js
Create a new log.js
file, as follows:
const createLogger = require('@benchmate/logger')
module.exports = service => createLogger('APPNAME', service)
Finally, use the logger:
const { debug, info, warn, error, crit, exception } = require('./log')('SERVICENAME')
debug('Debug or trace information.')
info('Routine information, such as ongoing status or performance.')
warn('Warning events might cause problems.')
error('Error events are likely to cause problems.')
exception(new Error('something went wrong!'))
crit('Critical events cause more severe problems or outages.')
exception(new Error('fatal error'), 'crit')
Log levels
We use a subset of the official syslog
log levels:
- crit: Critical events cause more severe problems or outages.
- error: Error events are likely to cause problems.
- warn: Warning events might cause problems.
- info: Routine information, such as ongoing status or performance.
- debug: Debug or trace information.
Configuration
The logging library is configured using the following environment variables:
-
LOG_LEVEL
(default:info
) - everything higher than this level is logged -
LOG_DISABLE_JSON
(default:false
) - disable structured JSON logs in production -
NODE_ENV
(default:development
) - set toproduction
to enable structured JSON logs or simpler logs (without colors)
Demo
Run yarn demo
to see a demo of the logger: