What is console-stamp?
The console-stamp npm package is used to add timestamps to console output in Node.js applications. It enhances the standard console methods (log, info, warn, error) by prefixing them with a timestamp, making it easier to track when specific log entries were made.
What are console-stamp's main functionalities?
Basic Timestamping
This feature adds a basic timestamp to all console log messages. By requiring the console-stamp package and passing the console object to it, all subsequent console.log calls will include a timestamp.
const consoleStamp = require('console-stamp')(console);
console.log('This is a log message.');
Custom Timestamp Format
This feature allows you to customize the format of the timestamp. By passing an options object with a 'format' property, you can specify the desired timestamp format.
const consoleStamp = require('console-stamp')(console, { format: 'yyyy-mm-dd HH:MM:ss.l' });
console.log('This is a log message with a custom format.');
Custom Label
This feature adds a custom label to the log messages. By setting the 'label' property to true in the options object, you can include a label in the log output.
const consoleStamp = require('console-stamp')(console, { label: true });
console.log('This is a log message with a custom label.');
Custom Prefix
This feature allows you to add a custom prefix to the log messages. By setting the 'prefix' property in the options object, you can include a custom prefix in the log output.
const consoleStamp = require('console-stamp')(console, { prefix: '[LOG]' });
console.log('This is a log message with a custom prefix.');
Other packages similar to console-stamp
winston
Winston is a versatile logging library for Node.js that supports multiple transports (e.g., console, file, HTTP). It provides more advanced logging features compared to console-stamp, such as log levels, custom formats, and log rotation.
bunyan
Bunyan is a simple and fast JSON logging library for Node.js. It provides structured logging with JSON output, making it easier to parse and analyze logs. Unlike console-stamp, Bunyan focuses on structured logging rather than just adding timestamps.
log4js
Log4js is a logging library inspired by the Java log4j library. It supports multiple appenders (e.g., console, file, SMTP) and provides flexible configuration options. Log4js offers more comprehensive logging capabilities compared to console-stamp.
console-stamp
Patch Node.js console methods in order to add timestamp information by pattern.
Usage
Installing
npm install console-stamp
Patching the console
// Patch console.x methods in order to add timestamp information
require("console-stamp")(console, "HH:MM:ss.l");
console.log("Hello World!");
// -> [14:02:48.062] [LOG] Hello World!
var port = 8080;
console.log("Server running at port %d", port);
// -> [16:02:35.325] [LOG] Server running at port 8080
Example
console.time( "MyTimer" );
console.log( "LOG" );
console.info( "INFO" );
console.warn( "WARN" );
console.error( "ERROR" );
console.dir( { foo: "bar" } );
console.trace();
console.timeEnd( "MyTimer" );
console.assert( count < 10, "Count is > 10" );
Result:
[20:04:05.969] [LOG] LOG
[20:04:05.972] [INFO] INFO
[20:04:05.972] [WARN] WARN
[20:04:05.972] [ERROR] ERROR
[20:04:05.972] [DIR] { bar: 'console.dir' }
[20:04:05.975] [ERROR] Trace
at Object.<anonymous> (/Users/starak/code/node-console-stamp/test.js:14:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
[20:04:05.975] [LOG] MyTimer: 6ms
[20:04:05.976] [ASSERT]
AssertionError: Count is > 10
at Console.assert (console.js:102:23)
at Console.con.(anonymous function) [as assert] (/Users/starak/code/node-console-stamp/main.js:35:24)
at Object.<anonymous> (/Users/starak/code/node-console-stamp/test.js:16:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
See more about timestamp patterns at felixges excellent dateformat
Adding Metadata
Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples.
String example
require("console-stamp")(console, "HH:MM:ss.l", '[' + process.pid + ']');
console.log('Metadata applied.');
Result:
[18:10:30.875] [LOG] [7785] Metadata applied.
Function example
var util = require("util");
require("console-stamp")(console, "HH:MM:ss.l", function(){ return '[' + (process.memoryUsage().rss) + ']'; });
console.log('Metadata applied.');
Result:
[18:10:30.875] [LOG] [14503936] Metadata applied.