Skip to content

Customizing output format #582

@Qix-

Description

@Qix-
Member

Ref #573, #565, #553, #456, #453, #402, #578, #526, #525, #488

Right now it's very clear that debug takes a subjective stance on its output format. This is unsuitable for a variety of use-cases, especially the disparity between the isatty(2) == 0 and == 1 cases.

Let's figure out how to solve this.


My initial knee-jerk idea would be to introduce a DEBUG_FORMAT environment variable. It would accept a string with delimiters similar to date-time or printf formats that indicate different elements of a debug log.

This would also remove the need for a plethora of custom environment variables that have been proposed.

We'd have a default format that would then be the same between interactive/non-interactive TTY sessions (fd 2, as always).

This would look something like

DEBUG_FORMAT="%N %m %D"

Where %N is the namespace (colored if interactive terminal), %m is the message and %D is the time diff - the exact format in the screenshot in the readme.

Of course there would be other delimiters available, and the above doesn't necessarily need to be the default, but it would solve pretty much any use-case imaginable regarding the output format in a non-subjective manner.


That's just my personal proposal, but this RFC is after any solution, so if you have a different idea please sound off below.

Activity

added
discussionThis issue is requesting comments and discussion
change-minorThis proposes or provides a change that requires a minor release
on Jun 20, 2018

48 remaining items

the0neWhoKnocks

the0neWhoKnocks commented on Oct 12, 2020

@the0neWhoKnocks

Just curious what sort of progress has been made on this. I see it's on the list for the 5.x milestone. It would be a really nice feature to be able to use.

deleted a comment from Download on Feb 9, 2021
Joshfindit

Joshfindit commented on Mar 21, 2021

@Joshfindit

I'm honestly only in this thread because I want debug to stop automatically returning seconds and instead always return milliseconds.

  ChannelChecker getting channel_id 1656344628 +2ms
  ChannelChecker getting channel_id 891441386 +2ms
  ChannelChecker getting channel_id 2042650534 +1ms
  ChannelChecker getting channel_id 1015526937 +3ms
  ChannelChecker getting channel_id 1649651584 +2s
  ChannelChecker getting channel_id 815734771 +2ms
  ChannelChecker getting channel_id 388112384 +1ms
  ChannelChecker getting channel_id 1290004369 +3ms
  ChannelChecker getting channel_id 1247374628 +1ms
  ChannelChecker getting channel_id 2020239812 +3ms
  ChannelChecker getting channel_id 969553134 +3ms

It's easy to miss that one call took 1000x longer.

Qix-

Qix- commented on Mar 21, 2021

@Qix-
MemberAuthor

@Joshfindit the problem there is that sometimes events don't happen for hours at a time. Millisecond triviality goes out the window at that scale. Hence why we use ms. But I understand if not everyone wants that behavior.

Joshfindit

Joshfindit commented on Mar 21, 2021

@Joshfindit

@Qix- It’s your package, it’s bot for me to tell you what’s right for you or other users, that’s just why I’m here. Custom output will let me accomplish it.

DimitarNestorov

DimitarNestorov commented on Sep 14, 2021

@DimitarNestorov

I'm honestly only in this thread because I want debug to stop automatically returning seconds and instead always return milliseconds.

Same. Here's a solution:

// JavaScript
debug.humanize = ms => `${ms}ms`;
// TypeScript
function humanize(ms: number): string;
function humanize(ms: string): number;
function humanize(ms: number | string): string | number {
  return `${ms}ms`;
}
debug.humanize = humanize;
Qix-

Qix- commented on Sep 14, 2021

@Qix-
MemberAuthor

Yep @DimitarNestorov's way is currently the "official" way of doing it and is entirely acceptable.

ajorpheus

ajorpheus commented on Dec 2, 2022

@ajorpheus

If you stumble across this issue while trying to enable timestamps for GHA log, here is the good bit:

GitHub Actions now provides timestamps by default with "shift + T" in the logs.

dmatora

dmatora commented on Mar 24, 2023

@dmatora

Here's how I prepend [ YYYY/MM/DD - HH:mm:ss ] to all debug output lines:

var moment = require('moment');
var debug = require('debug');
debug.formatArgs = formatArgs;
function formatArgs(args) {
    let name = this.namespace;
    let useColors = this.useColors;
    let dateTime = '[' + moment().format('YYYY/MM/DD - HH:mm:ss') + ']';
    if (useColors) {
        let c = this.color;
        let colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
        let prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
        args[0] = dateTime + prefix + args[0].split('\n').join('\n' + '                       ' + prefix);
        args.push(colorCode + 'm+' + debug.humanize(this.diff) + '\u001b[0m');
    } else {
        args[0] = dateTime + name + ' ' + args[0].split('\n').join('\n' + '                       ' + name);
    }
}

@SCG82 when I try to do that, name and useColors are undefined
doesn't it require debug.formatArgs = formatArgs.bind(debug) or something (which isn't works as well?

Download

Download commented on Apr 20, 2023

@Download
jmrossy

jmrossy commented on Oct 10, 2023

@jmrossy

In case anyone needs to strip the output format down to just the content, this worked for me:

function formatArgs(this: debug.Debugger, args: any[]) {
  args.push(debug.humanize(this.diff));
  args.pop();
}
debug.formatArgs = formatArgs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

change-majorThis proposes or provides a change that requires a major releasediscussionThis issue is requesting comments and discussionfeatureThis proposes or provides a feature or enhancementhelp-wantedThis issue has an actionable itempr-welcomeThis issue has an approved change; a pull request would be appreciated

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @TooTallNate@Download@theDanielJLewis@bodaro@dbo

    Issue actions

      Customizing output format · Issue #582 · debug-js/debug