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

how to use debug with PM2? #503

Closed
bi-kai opened this issue Sep 14, 2017 · 4 comments
Closed

how to use debug with PM2? #503

bi-kai opened this issue Sep 14, 2017 · 4 comments

Comments

@bi-kai
Copy link

bi-kai commented Sep 14, 2017

how to use debug with PM2?
DEBUG=* pm2 start app.js
There is no debug logs.

@DanielHeckrath
Copy link

Isn't it possible to use the ecosystem.config.js to configure different environment variables when starting a process with PM2?
See https://github.com/Unitech/pm2/wiki/Applications-Declaration

Should be possible to include DEBUG=* in the env section of your desired process

@bi-kai
Copy link
Author

bi-kai commented Sep 28, 2017

That works, and there is another question, the debug logs are all in PM2's error log, not in out.log, do you know why?

@DanielHeckrath
Copy link

DanielHeckrath commented Sep 28, 2017

When you use debug on nodejs it writes to stderr by default (see node.js)

It is possible though to redirect the output of a logger to another stream https://github.com/visionmedia/debug/blob/master/examples/node/stdout.js

The pattern I'm usually using when working with debug is this:

const logFactory = require('debug');

module.exports = function log(namespace) {
    const debug = logFactory(namespace);

    const error = logFactory(namespace);
    error.log = function(...args) {
        console.error(...args);
    };

    return {
        debug,
        error,
    };
};

then I just require my new log factory in my modules like this

const log = require('log')('tag');

log.debug('this logs to stdout');
log.error('this logs to stderr');

Note that you have to rewrite the error.log part to something like this if ES6 spread syntax is not available in your environment

error.log = console.error.bind(console);

@bi-kai
Copy link
Author

bi-kai commented Oct 28, 2017

That's greet!

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

2 participants