tp-logger
Supplement log messages with a Timestamp and caller's Path, with options for more.
Note: v1.0
broke support for Node.js version < 4.0; v1.1
brings the support back, recommended for maximum compatibility.
Install
$ npm install -save tp-logger
Usage
var logger = /* optional options, see Options below */; // Behaves the same as default console, where you can use string formattinglogger;// [2015-12-16T06:49:08+00:00][vanilla.js] lol: 2 hours logger;// [2015-12-16T06:49:08+00:00][vanilla.js] dota logger; // added since 1.0// [2015-12-16T06:49:08+00:00][vanilla.js] cs:go logger;// [2015-12-16T06:49:08+00:00][vanilla.js] Error: helldivers// at Object.<anonymous> (/Users/woozyking/proj/personal/tp-logger/examples/vanilla.js:9:14)// at Module._compile (module.js:425:26)// at Object.Module._extensions..js (module.js:432:10)// at Module.load (module.js:356:32)// at Function.Module._load (module.js:313:12)// at Function.Module.runMain (module.js:457:10)// at startup (node.js:138:18)// at node.js:974:3
If you don't mind your coworkers wondering why node.js console
has suddenly become so awesome (or bad), you can also do:
// NOT Recommendedconsole = /* optional options, see Options below */;// ...
Since 1.0
Since 1.1
the underlying implementation has changed to an ES6 ES5 style class that extends the Node.js native Class: Console
, making it simpler to extend. See Class: TPLogger section for details.
var TPLogger = TPLogger;var logger = /* optional options, see Options below */; console; // trueconsole; // true logger;// [2015-12-16T06:49:08+00:00][/full/path/to/class.js] Log Message logger;// [2015-12-16T06:49:08+00:00][class.js][12345] // your implementation, etc
Since 0.3
If you use express.js, you can also use the middleware provided to log errors passed on by request handlers in a centralize fashion:
var app = ;var loggerMw = ; app; // plug in our logger middleware to log handler passed errorsapp;
Options
See them in action by running this example
var logger = // stream for log() and info(). Not applicable for on the fly `Once` methods stdout: <writable stream instances Default: processstdout> // stream for warn() and error(). Not applicable for on the fly `Once` methods stderr: <writable stream instances Default: processstderr> // whether to use local time or UTC time local: <boolean Default: false> localTs: <Alias of 'local' Since 02> // timestamp format tsFormat: <string See momentjs formatting documentation> // whether to log full path of the caller or just caller filename fullPath: <boolean Default: false Since 02> // whether to log with a supplement of [log|info|err] // enforced to true when stdout == stderr logType: <boolean Default: true when stdout == stderr Since 02> // include process.pid pid: <boolean Default: false Since 10>;
Class: TPLogger
Subclass of native Class: Console
. Since 1.0.
new TPLogger([opt])
Creates a new TPLogger
instance by passing optional options opt
, see Options section for all available options.
logger.meta([opt][, method])
Generates meta info as string. Used as supplement before log messages. Since 1.0. Supports all Options except opt.stdout
and opt.stderr
. The method
argument is only useful when opt.logType
is true
, or both constructor _opt.stdout
and _opt.stderr
point to the same writable stream.
logger.log|info|warn|error(...)
Behaves exactly like native Console
, plus meta
method supplied supplement before intended log messages.
logger.logOnce|infoOnce|warnOnce|errorOnce(opt, ...)
Behaves like TPLogger#log|info|warn|error
, but the first argument must be an object and treated as on the fly options to override constructor options behaviors (opt.stdout
and opt.stderr
are ignored).