Opinionated Logging for Node.js - forces prescriptive, disciplined, structured logging.
Install with:
npm install olog --save
To get started require olog passing your module var. This will automatically use your module to include the component name in your logs.
const log = require('olog')(module)
To configure logging for your application, call the config function in the top-level module.
log.config({
application: 'product-service',
environment: 'dev'
})
To log a server informational log, pass in a log record (javascript object) with fields.
let productId = '123'
log.info({
message: `Db Update on Product ${productId}`,
category: 'Catalog'
transaction: 'CreateProduct',
trace: 'e45dc587-3516-489f-9487-391a119889c0'
annotations: {
productId: productId
}
})
Outputs:
{
"time": "2016-11-01T23:00:35.181Z",
"message": "[SERVER-Info] Db Update on Product 123",
"level": "Info",
"schema": "SERVER-Info",
"version": 3,
"host": "i-a38273",
"pid": "28393",
"component": "lib/mymodule",
"category": "Catalog",
"transaction": "CreateProduct",
"trace": "e45dc587-3516-489f-9487-391a119889c0",
"annotations": {
"productId": "123"
}
}
- olog (options)
- log.config (options)
- log.debug (record)
- log.info (record)
- log.warn (record)
- log.error (record)
- log.serverDebug (record)
- log.serverInfo (record)
- log.serverWarn (record)
- log.serverError (record)
- log.clientDebug (record)
- log.clientInfo (record)
- log.clientWarn (record)
- log.clientError (record)
- log.httpApiStart (record)
- log.httpApiStop (record)
- log.httpUiStart (record)
- log.httpUiStop (record)
- log.httpApiSend (record)
- log.httpApiReceive (record)
- log.eventStart (record)
- log.eventStop (record)
Factor function that creates a new logger. It makes it easy to require and create a logger for a module.
Type: String
or Object
If module is a String, it will be component
in log records. If module is an Object, it should be Node's module Object. The the component
will be generated based on its properties.
Type: Object
Allows you to provide module-level defaults for any of the log record properties (i.e. category, transaction, etc).
// component name
const log = require('my-module')
or
// generates component name
const log = require(module)
or
// applies record defaults
const log = require(module, {
category: 'Shopping Cart',
transaction: 'UpdateCart'
})
Configure application-wide settings for logging.
Type: Object
Type: String
Environment Variable: OLOG_APPLICATION
Type: String
Environment Variable: OLOG_ENVIRONMENT
Type: String
Environment Variable: OLOG_LEVEL
Type: Stream
Type: Object
Alias for log.serverDebug (record).
Alias for log.serverInfo (record).
Alias for log.serverWarn (record).
Alias for log.serverError (record).