Chill Logger
Relax dude, your logs are covered
Working with Docker & ECS presents some interesting challenges & some interesting features, including application logs through Docker & Fluentd. However this requires a specific format to be printed out to stdout, so this module prints logs in a specific format for Fluentd to consume.
In development, your logs are formatted slightly to add color (optionally) and to add clearer spacing. In production they are minified (well, just not padded out) and line-by-line they will be sent to Fluentd courtesy of the Docker logging driver.
Installation
$ npm install --save chill-logger
If you would like colors when looking at logs during development, also include colors too:
$ npm install --save-dev colors
Without colors, your logs will be formatted into the same layout but there won't be any colors 😢
Usage
The easiest way to use this is to define a file for your logger:
const logger = name: 'my-awesome-project' level: processenvLOG_LEVEL || 'debug' // Allows you to override it at runtime;
More "advanced" usage could look like:
// In logger.jsconst logger = ; moduleexports = ; moduleexports { moduleexports;}; // In another fileconst logger = logger;
API
const log = ;// By default, the name for the logger is "log"// And by default the log level is "debug", to allow everything through log;// This will write the log line as "log.debug" log;// This will write the log line as "log.info" log;// This will write the log line as "log.warn" log;// This will write the log line as "log.error" log;// This will write the object as "log.info" // You can optionally create types on demand:log;// This will write the object as "log.location" // Or you can create functions to create new types, by passing arguments to the _send method:log { log;};// And then use it straight away!log;
Errors
This logger will format errors into plain objects by passing them through the formatErr
function that's provided.
You can override this method if you want to include additional properties:
const logger = ;const os = ; moduleexports = ; logger { return code: errcode || null name: errname message: errmessage instance_id: oshostname query: errquery || null stack: errstack ;};
Middleware
chill-logger offers a way to create a HTTP request logger middleware function using your logger to output the request.
const chill = ;const express = ; const app = ;const logger = ; app; app;
Requests get logged into .req
at the info
level (so if you set the log-level to warn
or above then you won't see
any requests being logged - useful for integration tests!) and the request & response are transformed into plain objects
describing the many properties of a request.
There are a few options to let you customise the logger, so you can get/set/override constious properties:
chill
Usually, if you want to add information to the log after the request has completed, and logins/sessions/events have
occurred, then you'd want to use format
over req
or res
:
chill
Please note: req
executes as the request hits this middleware, whereas res
and format
execute after the response
has been sent by the HTTP server. Choose your logic wisely!
Silence all
If you are looking to test code using this module, and you want to silence the output, set the level
to ignore-all
and all output will be silenced.
const logger = ; moduleexports = ;
$ LOG_LEVEL=ignore-all node test/runner.js
One more thing...
Feel free to open an issue, and this is an open-source project so pull-requests are welcome!