Skip to content
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

Add ability to format debug as JSON output #442

Closed
gajus opened this issue Apr 16, 2017 · 18 comments
Closed

Add ability to format debug as JSON output #442

gajus opened this issue Apr 16, 2017 · 18 comments

Comments

@gajus
Copy link

gajus commented Apr 16, 2017

I'd like there to be an option (controllable using environment variables) that would make debug module print JSON output instead of plain text.

This at the moment can be achieved using a module such as debug-to-json (@ping yoshuawuyts). The problem with the pipe solution is that it is not always easy to modify the command used to start the program (e.g. How to append an argument to a container command?).

Maybe I am doing something wrong, but (in a large deployment) logs coming via debug are quite useless ("large deployment" being a Kubernetes cluster with filebeat + ELK used for logging). [I suppose it is possible to reconize debug output in logstash and convert it to JSON?]

Being able to configure debug output with environment variable would greatly alleviate the difficulty of log processing here, e.g.

DEBUG_FORMAT=json

The other thing to note here is that certain log aggregation methods (filebeat) require that logs come in one-per-line. Plain text output does not guarantee this; JSON can.

Furthermore, the JSON format could be used to supplement debug output with meta information such as package name, version. This can be a controllable option via environment variables too.

I'd be happy to raise a PR.

@thebigredgeek
Copy link
Contributor

With v3, you'll be able to override the out function... which will allow you to format the log output however you want. v2 doesn't including functionality like this, though

@gajus
Copy link
Author

gajus commented Apr 18, 2017

Well, overriding the functionality of out is hardly a fix here. If I need to override something, I'd rather use a logging framework such as bunyan or winston. The convenience of debug is that it is a convention driven, runtime configurable logging tool. If I need to override something, then it is just another logging framework.

@thebigredgeek
Copy link
Contributor

Well, a major goal of debug is to be minimal while also extensible. If you have a case where you need to write to a file, or write JSON to stdout, or move data to logstash via HTTP, debug should be able to support that without including the functionality into the core lib

@gajus
Copy link
Author

gajus commented Apr 18, 2017

debug should be able to support that without including the functionality into the core lib

How .. ?

@gajus
Copy link
Author

gajus commented Apr 18, 2017

The mere fact that the output can be multiline makes it impossible parse the logs in any reliable way.

@thebigredgeek
Copy link
Contributor

Overwriting out, as I said before. You'll be able to stick whatever function you want on there, and do whatever you want with the output. It will be passed a string, which you parse into whatever form you want (JSON, etc) and then send wherever you want

@gajus
Copy link
Author

gajus commented Apr 18, 2017

Overwriting out, as I said before. You'll be able to stick whatever function you want on there, and do whatever you want with the output. It will be passed a string, which you parse into whatever form you want (JSON, etc) and then send wherever you want

That requires to have access to the debug instance. It is fine if my application and ALL of the dependencies share the same version of debug. However, if at least one of them does not, there is no way for me to control output format of that dependency from within my application.

@thebigredgeek
Copy link
Contributor

thebigredgeek commented Apr 18, 2017

I mean, by default, you aren't going to see any output if you are dealing with third-party libs. If you care about the output from third-party libs, and want a special format, it doesn't seem like a stretch to require you to install debug as a dep and overwrite the log function

@gajus
Copy link
Author

gajus commented Apr 18, 2017

This is year 2020.

  • I have an application lalaland that uses debug@v4.
  • lalaland dependency awesomestdlib uses debug@3.

How do I format logs of awesomestdlib using the proposed out property override?

@gajus
Copy link
Author

gajus commented Apr 18, 2017

If you really want to depend on overriding the output method, then use a global variable, e.g.

// debug-format.js
global.visionmediaDebugOut = () => {};

Then whatever app that wants to override the out method would need to include this script prior to executing any code, e.g. node -r debug-format.js my-app.js.

Unlike the proposed approach, this approach can be work with multiple versions of debug in a backwards compatible way.

I don't think thats a right approach. It is an overkill. Simply allowing to output content in JSON format using ENV as configuration would be enough.

@thebigredgeek
Copy link
Contributor

Well, using an env variable is good and all, but it requires additional surface area for the lib.... yet another configuration option that has to be well supported on every single platform running debug (node, all major browsers, electron, cordova, etc).

@gajus
Copy link
Author

gajus commented Apr 18, 2017

another configuration option that has to be well supported on every single platform running debug (node, all major browsers, electron, cordova, etc).

Don't understand the argument. JSON.stringify is part of the ECMA standard since 1.7.

@thebigredgeek
Copy link
Contributor

Standardized output to TTY and non-TTY interfaces on a variety of platforms with yet another output format isn't as simple as simply calling JSON.stringify

@Kostanos
Copy link

Kostanos commented Dec 3, 2018

I'm looking for the same. Why the issue is closed?
Would be great to have output as JSON, many log parser works better with JSON format than custom text format..
Example of output could be:

{
  "time": "2018-12-03 15:15:57,300",
  "namespace": "INFO:root",
  "message": {
    "custom": "data"
  }
}

This would fit to most of the log parsers that support JSON format logs.

@Qix-
Copy link
Member

Qix- commented Dec 3, 2018

Please see #582.

@Kostanos
Copy link

Kostanos commented Dec 3, 2018

Thank you @Qix- but could you please show example how output could be converted to JSON?
And how to make it as a standard in all files?

For those who is still need JSON as simple as it is, I'm checking this library: https://www.npmjs.com/package/pino

But would be great to have simple support for JSON in debug library (I have already so many scripts done with "debug")

For example something like this:

const debug = require('debug')('rest:debug:myEndpoint', {output: 'JSON'})

or this:

DEBUG=*;format:JSON node myScript.js

etc...

@Qix-
Copy link
Member

Qix- commented Dec 3, 2018

@Kostanos please post on that ticket, not here.

@nervgh
Copy link

nervgh commented May 25, 2020

There is pino-debug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants