-
Notifications
You must be signed in to change notification settings - Fork 995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing output format #582
Comments
Definitely, this is the feature I was looking for right now. I've added some kind of event id injector to debug logs but I need to place it right in the middle of date and namespace (for non-TTY). What would be the impediments to add this feature to debug? |
Huge 👍 to this proposal, basically as-is. Env based configuration is the only one that really makes sense to me for this module. |
This could also be a reason to remove all but That'd make it a major change, though. |
Here's how I prepend 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);
}
} |
Alright, formally accepting this then. No objections for a while. Further, since this would be a major change (since I think we'd want to clear out Then all code would be able to benefit a little bit more, I think. |
How would |
I suggest to add |
Is a version available in a relatively stable state that supports DEBUG_FORMAT ? I would like to add my own prefix to all statements (across the entire process) to make tracking in AWS LogInsights easier. I would envision like this at the very beginning of the process: process.env.DEBUG_FORMAT= |
@SCG82 can your code snippet be used to apply to all instances of debug throughout the application? For example, I would run that code once in my index.js and prepend all debug's output with a reference ID (to be used in log crawlers later). |
I have all my debug variables in a separate module, const debug = require('./debug-vars.js');
debug.warning('something went wrong...'); debug-vars.js: const debug_module = require('debug');
const debug = {
debug: debug_module('myapp:debug'),
error: debug_module('myapp:error'),
info: debug_module('myapp:info'),
warning: debug_module('myapp:warning')
};
debug_module.formatArgs = formatArgs;
function formatArgs(args) {
const name = this.namespace;
const useColors = true;
const now = new Date();
const offsetMs = now.getTimezoneOffset() * 60 * 1000;
const dateLocal = new Date(now.getTime() - offsetMs);
const space = ' ';
const str = dateLocal.toISOString().slice(0, 19).replace(/-/g, '/').replace('T', ' - ');
const dateTime = '[' + str + ']';
let prefix = '';
if (useColors) {
const c = this.color;
const colorCode = '\u001b[3' + (c < 8 ? c : '8;5;' + c);
prefix = ' ' + colorCode + ';1m' + name + ' ' + '\u001b[0m';
args[0] = dateTime + prefix + args[0].split('\n').join('\n' + space + prefix);
args.push(colorCode + 'm+' + debug_module.humanize(this.diff) + '\u001b[0m');
} else {
prefix = ' ' + name + ' ';
args[0] = dateTime + prefix + args[0].split('\n').join('\n' + space + prefix);
args.push(debug_module.humanize(this.diff));
}
}
module.exports = debug; |
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. |
I'm honestly only in this thread because I want
It's easy to miss that one call took 1000x longer. |
@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 |
@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. |
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; |
Yep @DimitarNestorov's way is currently the "official" way of doing it and is entirely acceptable. |
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. |
* origin/format: fix most issues from the linter add `%d` `outputFormatter` to show exact ms diff debug-js#626 implement metaformatters (color, JSON.stringify) move args handling to outputFormatters.m() properly match current version's formatting fix %n to correctly show namespace move formatting mostly into common.js start implementing output formatting debug-js#582
@SCG82 when I try to do that, name and useColors are undefined |
This comment was marked as spam.
This comment was marked as spam.
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; |
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 theisatty(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
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.
The text was updated successfully, but these errors were encountered: