Cedar
Cedar is a Node.js log, designed to be fast, extensible, and super useful.
Powerful features
- Modifiable logging prefixes, formatters, and stringify functions.
- Logs which are functions, allowing shorthand calls.
- A
console
transport with color unicode symbols and helpful code snippets inside stack traces. - A
file
transport that can rotate by day, hour or minute. - A
multi
transport, which supportscluster
, sending worker logs to the master process.
Quick Start
Add cedar
to your dependencies.
npm install --save cedar
Create a log, and use it.
var log = transport: 'console' // Default. level: 'trace' // Default. transport: 'file' level: 'info' path: 'log/${YYYY}/${MM}/${DD}/app_${HOST}.log' ; ; // Shorthand.log;log;log;log log;log;log;log;log;log;log;
Convention & Configuration
Each Cedar transport has properties with defaults that can be overridden using a config object. They also have getters and setters, allowing you to change them later.
For example:
// Set the log `level` in a configuration object.var log = level: 'warn'; // "warn", "error" and "fatal". ; // Ignore.log; // Log "error". // Assign the `level`, thereby invoking its setter.loglevel = 'debug'; ; // Log "log".log; // Log "error".log; // Ignore.
string
log.level Configures the minimum level of logging that is shown (default: trace
).
var log = ;loglevel = 'debug';
Setting to a level from this list will enable logs of that level and all
of the levels after it: debug
, trace
, log
, info
, warn
, error
and fatal
. Setting the level to nothing
will stop all logs.
object
log.prefixes Customize prefixes for the color log messages.
; var log = ;logprefixes = trace: 'TRACE: 'cyan debug: 'DEBUG: 'magenta log: 'LOG: 'grey info: 'INFO: 'green warn: 'WARN: 'yellow error: 'ERROR: 'red fatal: 'FATAL: 'red;
string
log.space Configures the spacing that stringify uses.
var log = ;logspace = ' ';
The default is two spaces.
function
log.format Customize the message format, given 3 arguments.
var log = ;log { return prefix + message + ' from log.' + type + '!';});log; // "INFO Hello from log.info!"
Transports
Cedar currently supports 4 main transports: "base", "console", "file" and "multi". Each transport takes an optional configuration object.
Base
The base transport writes to a stream, and other transports extend it.
var fs = ;var writeStream = fs;var base = 'base' stream: writeStream;base;
Console
The console
log writes to process.stdout
with pretty colors.
Console is the default transport, so the following are equivalent:
log = ;log = 'color';
File
The file
log writes JSON messages to a file. In addition, it acts as a
simple event emitter so you can receive notifications when file rotation
events occur.
var file = 'file' path: 'log/app_${YYYY}-${MM}-${DD}_${HH}:${NN}_${HOST}.log';file; var console = ; file; file;
Multi
The multi
log writes to multiple logs at once. Its configuration object
is an array of configurations with transports specified by a transport
property.
var log = transport: 'console' transport: 'file' level: 'info' path: 'log/app_${YYYY}-${MM}-${DD}_${HOST}.log' transport: 'file' level: 'error' path: 'log/${YYYY}/${MM}/${DD}/error_${HOST}.log';