Skip to content

Add ability to format debug as JSON output #442

Closed

Description

@gajus

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.

Activity

  1. thebigredgeek commented on Apr 17, 2017

    @thebigredgeek
    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

  2. gajus commented on Apr 18, 2017

    @gajus
    Author

    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.

  3. thebigredgeek commented on Apr 18, 2017

    @thebigredgeek
    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

  4. gajus commented on Apr 18, 2017

    @gajus
    Author

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

    How .. ?

  5. gajus commented on Apr 18, 2017

    @gajus
    Author

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

  6. thebigredgeek commented on Apr 18, 2017

    @thebigredgeek
    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

  7. gajus commented on Apr 18, 2017

    @gajus
    Author

    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.

  8. thebigredgeek commented on Apr 18, 2017

    @thebigredgeek
    Contributor

    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

  9. gajus commented on Apr 18, 2017

    @gajus
    Author

    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?

  10. gajus commented on Apr 18, 2017

    @gajus
    Author

    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.

  11. thebigredgeek commented on Apr 18, 2017

    @thebigredgeek
    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).

  12. gajus commented on Apr 18, 2017

    @gajus
    Author

    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.

  13. thebigredgeek commented on Apr 18, 2017

    @thebigredgeek
    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

  14. Kostanos commented on Dec 3, 2018

    @Kostanos

    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.

  15. Qix- commented on Dec 3, 2018

    @Qix-
    Member

    Please see #582.

  16. Kostanos commented on Dec 3, 2018

    @Kostanos

    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...

  17. Qix- commented on Dec 3, 2018

    @Qix-
    Member

    @Kostanos please post on that ticket, not here.

  18. nervgh commented on May 25, 2020

    @nervgh

    There is pino-debug.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions