7

I am using npm debug package to log messages to the console (instead of the regular console.log()). Is there a way to show timestamp of the message with this debug library? For example I'd like to display all log messages in the format:

debug("Server started")

And to get the output in a form:

[16:24:15] Server started

where 16:24:15 is the current time. Ideally I'd like to be able to specify the time format (eg. add milliseconds etc..).

3 Answers 3

9

Unfortunately the default debug formatting implementation is a little bit inflexible in this respect: there is no option to enable timestamps.

Here are three alternatives to enable timestamps:

  1. as mentioned previously: redirect stderr to a non-TTY handle, e.g. file or pipe. That disables colors by default and therefore switches to the format with timestamps
  2. use DEBUG_COLORS=no e.g.
$ DEBUG_COLORS=no DEBUG=* node ./dummy.js
Wed, 27 Feb 2019 12:29:12 GMT test Server started
  1. wrap/override the default implementation of debug.formatArgs(). This will be global for your program, i.e. you can only have one replacement. args is the array of the parameters pass into the debug() call, i.e. you can process all arguments, remove some entries or add entries.

F.ex.

'use strict';

const debug  = require('debug');
const logger = debug('test');

// wrap debug.formatArgs() implementation
const origFormatArgs = debug.formatArgs;
debug.formatArgs = function (args) { // requires access to "this"
    // example: prepend something to arguments[0]
    args[0] = `${new Date().toUTCString()} ${args[0]}`;

    // call original implementation
    origFormatArgs.call(this, args);
};

// override default debug.formatArgs() implementation
debug.formatArgs = function (args) { // requires access to "this"
    const name = this.namespace;
    const useColors = this.useColors;

    if (useColors) {
        // example: prepend something to arguments[0]
        args[0] = `${new Date().toUTCString()} ${name} ${args[0]}`;
    } else {
        // example: append something to arguments[0]
        args[0] = `${name} ${args[0]} ${new Date().toUTCString()}`;
    }
};

logger("Server started");
Sign up to request clarification or add additional context in comments.

Comments

0

I'm very surprised to see [16:24:15] as a date, especially when getDate funtion from the module's source code uses ISO format.

Answering your question you can either fork the repo and customize date format whatever you want, or use more powerful logging tools like winston.

1 Comment

This is not the actual date or time that debug library displays. It's just a way I'd like to see my log messages displayed and was wondering if that is built-in in the debug library or is there some other easy way to do it. I will take a look at the winston package. I am new to nodeJS so I am still not sure which packages are best to use for different purposes I need in the app.
0

As mentioned on the npm debug page, the 'debug' package does support timestamp logging when the output is not TTY (When stdout is not a TTY, Date#toISOString() is used, making it more useful for logging the debug information). If you are using any process manager like forever, pm2 etc. The timestamp for the particular log will be available in the log file. Below attached is a snapshot from the log file I am using for my application.

::1 - - [16/Aug/2018:07:46:38 +0000] "GET /fonts/fontawesome-webfont.woff2?v=4.6.3 HTTP/1.1" 304 -

::1 - - [16/Aug/2018:07:46:39 +0000] "GET /get-locations HTTP/1.1" 304 -

Thu, 16 Aug 2018 07:47:00 GMT book-keeping:database Param for Inventory Missing Books { isMissing: 'Y' }

::1 - - [16/Aug/2018:07:47:02 +0000] "GET /getMissingBooksList?location=0 HTTP/1.1" 200 6786

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.