justlog
justlog , focus on logging
Features:
- coloured console logging
- file appender, with log rotate based on time
- configurable log message/patterns
- different log levels for different log categories (info debug warn error )
- can use as a connect middleware for access-log
Installation
npm install justlog
Usage
Getting start
// simple.jsvar justlog = ; var log = ; l;l;l;l;
$ node simple.js
the stdout/stderr is as below
19:01:41 INFO ajs:4 simple info19:01:41 DEBUG ajs:5 "a":1"b":219:01:41 WARN ajs:6 123419:01:41 ERROR ajs:7 name:somename number:123
and also, you can got a log file on logs/simple-%Y-%M-%D.log
2013-01-06 19:01:41 WARN ajs:6 12342013-01-06 19:01:41 ERROR ajs:7 name:somename number:123
Change log levels
var log = ;
available levels
- justlog.INFO
- justlog.DEBUG
- justlog.WARN
- justlog.ERROR
- justlog.ALL include all 4 level above
- justlog.EXCEPTION include WARN and ERROR
Log only one way
var stdLog = ; // not write log file, only use stdiovar fileLog = ; // not use stdio, only write a log file});
log error & warn messages into stdout
var log = ;
custom log file path & log file rotate
// write logs into filename.log and never rotatevar log1 = ; // write logs into filename-%Y-%M-%D.log, and rotate every daysvar log2 = ;
time format use moment string-format
rotate will be trigger when log filename changed.
custom log line format
var log = ;log;
All options
file : level : error | warn // levels for filelog pattern : 'file' // filelog pattern path : '[logs/%main_script_name]YYYY-MM-DD[.log]' // log file path mode : '0664' // log file mode dir_mode : '2775' // if log dir is not exists, log dir mode when create _watcher_timeout : 5007 // log file renamed watch timeout, DO NOT CHANGE THIS IF YOU REALLY KNOWN WHAT YOU DO. stdio : level : error | warn | debug | info // levels for stdio pattern : 'color' // stdio pattern stdout : processstdout // stdout stream (for info & debug log) stderr : processstderr // stderr stream (for warn & error log)
Middleware
a connect middleware for apache-like accesslog.
middleware's has same options as normally justlog object, but has different default value
var app = ;app;
Middleware default options
file: path : '[logs/%main_script_name-access-]YYYY-MM-DD[.log]' pattern : 'accesslog-rt' stdio : pattern : 'accesslog-color'
Buffer Log
you can buffer your log if you have big visits.
var log = ;
or
var log = justlog;
default options
duration : 1000 bufferLength : 0 // file: ... // stdio : ...
close buffer log
justlog;
About log pattern
Justlog has a powerful log line pattern support. You can use variables, objects and functions in you patterns. And you can define ansi color output patterns easily.
Syntax
{var[ args...][@colors...]}
- "{variable_name}": show varaible's value.
eg.
{remote-address}
- "{object_name.prop_name}": show property value.
eg.
{headers.accepted-encoding}
- "{function_name "const1", "const2"}": show function's return value with const arguments.
eg.
{now "YYYY-MM-DD"}
- "{function_name variable_name, object_name.prop_name}": show function's return value with variable arguments.
eg.
{color.status status-code}
- "{something@color1,color2}": set output ansi color.
eg.
{url@blue,underline}
Predefine variables
- msg : (all log arguments).toString()
- level : log level text align ("INFO ", "DEBUG", "WARN ", "ERROR")
- levelTrim : log level text without align ("INFO", "DEBUG", "WARN", "ERROR")
- file : log triggered file path
- lineno : log triggered code line number
- stack : alias for "file:lineno"
- stackColored : alias for colored stack
- time : time format as "HH:mm:ss"
- date : time format as "YYYY-MM-DD"
- fulltime : time format as "YYYY-MM-DD HH:mm:ss"
- numbertime : time format as "YYYYMMDDHHmmss"
- mstimestamp : unix timestamp (with milliseconds)
- timestamp : unix timestamp (with seconds)
Predefined functions
- now : now formater function. eg.
{now "YYYY-MM-DD"}
- color.status : add color for http status code. eg.
{color.status status}
- color.method : add color for http request method. eg.
{color.method method}
- color.event : add color for event type. eg.
{color.event event}
- color.level : add color for log level. eg.
{color.level level}
Predefined patterns
- simple-color: log message and colored level text
- simple-nocolor: like simple without color
- color: tracestack, time, log message and colored level text
- nocolor: like color without color
- event-color: time, log message and colored event
- event-nocolor: like event-color without color
- file : fulltime, tracestack, log message and level text
- accesslog: apache access-log
- accesslog-rt: like access-log with response-time on the end (with microsecond)
- accesslog-color: like accesslog-rt with ansi colored
License
justlog
is published under BSD license.