Search code examples
node.jsbashpipestdout

Write the contents of node debug log to file


I can I write the contents of this output to a file? I'm using the debug module to log messages, I'd like to be able to pipe them to a file. However its not working as expected.

$ DEBUG=* node -e 'var debug = require("debug")("test"); debug("hello world")'
    test hello world +0ms
$ DEBUG=* node -e 'var debug = require("debug")("test"); debug("hello world")' > temp.txt
    test hello world +0ms

Just tried this and received no output as well.

$ { DEBUG=* node -e "var debug = require('debug')('test'); debug('hello world')"; } >temp.txt
    test hello world +0ms

Solution

    1. Make sure you running the latest version of debug.
    2. You need to use the DEBUG_FD=3 env flag as well as 3> to pipe

    Here's an example.

    $ DEBUG_FD=3 DEBUG=foo node -e "require('debug')('foo')('hello')" 3> foo.txt
    

    From the repo's issue request/question: is there a way for debug to log to a file?