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
This module enables you to patch the console's methods in Node.js, to add timestamp prefix based on a given string pattern, and more...
Usage
Install
npm install console-stamp
Patching the console
require("console-stamp")(console, [options]);
console
The console itself.
options {Object|String}
From version 2.0 the second parameter is an object with several options. As a backward compatibillity feature this parameter can be a string containing the pattern.
-
options.pattern {String}
A string with date format based on Javascript Date Format
Default: "ddd mmm dd yyyy HH:MM:ss"
-
options.formatter {Function}
A custom date formatter that should return a formmatted date string.
-
options.label {Boolean}
If true it will show the label (LOG | INFO | WARN | ERROR)
Default: true
-
options.include {Array}
An array containing the methods to include in the patch
Default: ["log", "info", "warn", "error", "dir", "assert"]
-
options.exclude {Array}
An array containing the methods to exclude in the patch
Default: [] (none)
-
options.metadata {String/Object/Function}
Types can be String, Object (interpreted with util.inspect), or Function. See the test-metadata.js for examples.
Note that metadata can still be sent as the third parameter (as in vesion 1.6) as a backward compatibillity feature, but this is deprecated.
Default: undefined
-
options.colors {Object}
An object representing a color theme. More info here.
-
options.colors.stamp {String/Array/Function}
Default: []
-
options.colors.label {String/Array/Function}
Default: []
-
options.colors.metadata {String/Array/Function}
Default: []
Note: To combine colors, bgColors and style, set them as an array like this:
...
stamp: ["black", "bgYellow", "underline"]
...
Or chain Chalk functions like this:
...
stamp: require("chalk").red.bgYellow.underline;
...
Note also that by sending the parameter --no-color
when you start your node app, will prevent any colors from console.
$ node my-app.js --no-color
Example
// Patch console.x methods in order to add timestamp information
require( "console-stamp" )( console, { pattern : "dd/mm/yyyy HH:MM:ss.l" } );
console.log("Hello World!");
// -> [26/06/2015 14:02:48.062] [LOG] Hello World!
var port = 8080;
console.log("Server running at port %d", port);
// -> [26/06/2015 16:02:35.325] [LOG] Server running at port 8080
console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );
Result:
[26/06/2015 12:44:31.777] [LOG] This is a console.log message
[26/06/2015 12:44:31.777] [INFO] This is a console.info message
[26/06/2015 12:44:31.779] [WARN] This is a console.warn message
[26/06/2015 12:44:31.779] [ERROR] This is a console.error message
[26/06/2015 12:44:31.779] [DIR] { bar: 'This is a console.dir message' }
and
require( "console-stamp" )( console, {
metadata: function () {
return ("[" + process.memoryUsage().rss + "]");
},
colors: {
stamp: "yellow",
label: "white",
metadata: "green"
}
} );
console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );
Result:
Custom Formatter Example
Custom forrmatter using moment.js
var moment = require('moment');
moment.locale('ja');
require( "console-stamp" )( console, {
formatter:function(){
return moment().format("LLLL");
}
} );
console.log( "This is a console.log message" );
console.info( "This is a console.info message" );
console.warn( "This is a console.warn message" );
console.error( "This is a console.error message" );
console.dir( {bar: "This is a console.dir message"} );
Result:
[2016年5月12日午前11時10分 木曜日] [LOG] This is a console.log message
[2016年5月12日午前11時10分 木曜日] [INFO] This is a console.info message
[2016年5月12日午前11時10分 木曜日] [WARN] This is a console.warn message
[2016年5月12日午前11時10分 木曜日] [ERROR] This is a console.error message
[2016年5月12日午前11時10分 木曜日] [DIR] { bar: 'This is a console.dir message' }
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, {
pattern:"HH:MM:ss.l",
metadata:'[' + process.pid + ']'
});
console.log('Metadata applied.');
Result:
[26/06/2015 12:44:31.779] [LOG] [7785] Metadata applied.
Function example
var util = require("util");
require("console-stamp")(console, {
pattern:"HH:MM:ss.l",
metadata: function(){ return '[' + (process.memoryUsage().rss) + ']'; });
console.log('Metadata applied.');
Result:
[18:10:30.875] [LOG] [14503936] Metadata applied.