Opinionated Logging for Node.js - forces prescriptive, disciplined, structured logging. This is logging based on predefined [Schemas] (SCHEMAS.md) with default fields. Schema names, fields, and message formatting all have defaults but can all be configured.
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"
}
}
- record
- olog (module[, defaults])
- 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)
Type: Object
Log record used for all logging functions. This undergoes the following transformations during logging:
- Generates message field based on the messageFormatter for the given schema.
- Filters and orders fields based on configured fields for the schema (see log.config (options)). Only configured fields are output, and fields are output in the order they are configured.
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('olog')('my-module')
or
// generates component name
const log = require('olog')(module)
or
// applies record defaults
const log = require('olog')(module, {
category: 'Shopping Cart',
transaction: 'UpdateCart'
})
Configure application-wide settings for logging.
Type: Object
Type: String
Name or ID of the application. Set here or with Environment Variable OLOG_APPLICATION
.
Type: String
Name or ID of the deployed environment. Set here or with Environment Variable OLOG_ENVIRONMENT
.
Type: String
one of debug
, info
, warn
, error
Default: info
Environment Variable: OLOG_LEVEL
Name of log level enabled for this application. Set here or with Environment Variable OLOG_LEVEL
.
Type: Stream
Default: process.stdout
Writable stream to which log records are written. Records are output in UTF8 stringified JSON format.
Type: Object
Default:
{
serverDebug: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions']
serverInfo: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions']
serverWarn: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions']
serverError: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'exception']
clientDebug: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'uri', 'userAgent', 'userAuth']
clientInfo: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'uri', 'userAgent', 'userAuth']
clientWarn: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'uri', 'userAgent', 'userAuth']
clientError: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'uri', 'userAgent', 'userAuth', 'exception']
httpApiStart: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'route', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject', 'referer', 'userAgent']
httpApiStop: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'route', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject', 'referer', 'userAgent', 'duration', 'responseCode', 'responseHeaders', 'responseCookies', 'responseBodyString', 'responseBodyMap', 'responseBodyObject']
httpUiStart: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'route', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject', 'referer', 'userAgent']
httpUiStop: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'route', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject', 'referer', 'userAgent', 'duration', 'responseCode', 'responseHeaders', 'responseCookies', 'responseBodyString', 'responseBodyMap', 'responseBodyObject']
httpApiSend: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'api', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject']
httpApiReceive: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'api', 'method', 'uri', 'requestHeaders', 'requestCookies', 'requestBodyString', 'requestBodyMap', 'requestBodyObject', 'resonseCode', 'responseHeaders', 'responseCookies', 'responseBodyString', 'responseBodyMap', 'responseBodyObject', 'duration'],
eventStart: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'originatedAt', 'topic', 'partition', 'key', 'attributes'],
eventStop: ['time', 'message', 'level', 'schema', 'version', 'application', 'environment', 'host', 'pid', 'component', 'transaction', 'trace', 'annotations', 'extensions', 'originatedAt', 'topic', 'partition', 'key', 'attributes', 'duration', 'elapsed']
}
This determines allowed output fields for each schema along with the order of output for each field.
Type: Object
Default:
{
serverDebug: 'SERVER-Debug',
serverInfo: 'SERVER-Info',
serverWarn: 'SERVER-Warn',
serverError: 'SERVER-Error',
clientDebug: 'CLIENT-Debug',
clientInfo: 'CLIENT-Info',
clientWarn: 'CLIENT-Warn',
clientError: 'CLIENT-Error',
httpApiStart: 'HTTP-API-Start',
httpApiStop: 'HTTP-API-Stop',
httpUiStart: 'HTTP-UI-Start',
httpUiStop: 'HTTP-UI-Stop',
httpApiSend: 'HTTP-API-Send',
httpApiReceive: 'HTTP-API-Receive',
eventStart: 'EVENT-Start',
eventStop: 'EVENT-Stop'
}
Logged name of each schema based on function name.
Type: Object
Default:
{
serverDebug: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
serverInfo: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
serverWarn: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
serverError: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}: ${r.exception}` },
clientDebug: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
clientInfo: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
clientWarn: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}` },
clientError: (r) => { return `[${r.schema}] ${r.transaction}: ${r.message}: ${r.exception}` },
httpApiStart: (r) => { return `[${r.schema}] ${r.transaction}: ${r.route} for ${r.trace}` },
httpApiStop: (r) => { return `[${r.schema}] ${r.transaction}: ${r.route}[${r.responseCode}] in ${r.duraton}ms for ${r.trace}` },
httpUiStart: (r) => { return `[${r.schema}] ${r.transaction}: ${r.route} for ${r.trace}` },
httpUiStop: (r) => { return `[${r.schema}] ${r.transaction}: ${r.route}[${r.responseCode}] in ${r.duraton}ms for ${r.trace}` },
httpApiSend: (r) => { return `[${r.schema}] ${r.transaction}: ${r.api} (${r.method}:${r.uri}) for ${r.trace}` },
httpApiReceive: (r) => { return `[${r.schema}] ${r.transaction}: ${r.api} (${r.method}:${r.uri})[${r.responseCode}] in ${r.duraton}ms for ${r.trace}` }
},
Configure message formatters for each schema. This Object contains properties following this syntax:
<schemaFunctionName>: (record) => { return <formatted message> }
.
For example:
{
serverInfo: (record) => { return `${record.transaction} - DETAILS: ${record.message}` },
httpApiStop: (record) => { return `${record.route}[${record.responseCode}]` }
}
Alias for log.serverDebug (record).
Alias for log.serverInfo (record).
Alias for log.serverWarn (record).
Alias for log.serverError (record).
Logs record using serverDebug (SERVER-Debug) schema.
Logs record using serverInfo (SERVER-Info) schema.
Logs record using serverWarn (SERVER-Warn) schema.
Logs record using serverError (SERVER-Error) schema.
Logs record using clientDebug (CLIENT-Debug) schema.
Logs record using clientInfo (CLIENT-Info) schema.
Logs record using clientWarn (CLIENT-Warn) schema.
Logs record using clientError (CLIENT-Error) schema.
Logs record using httpApiStart (HTTP-API-Start) schema.
Logs record using httpApiStop (HTTP-API-Stop) schema.
Logs record using httpUiStart (HTTP-UI-Start) schema.
Logs record using httpUiStop (HTTP-UI-Stop) schema.
Logs record using httpApiSend (HTTP-API-Send) schema.
Logs record using httpApiReceive (HTTP-API-Receive) schema.
Logs record using eventStart (EVENT-Start) schema.
Logs record using eventStop (EVENT-Stop) schema.