Skip to content

how to use debug with PM2? #503

Description

@bi-kai

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

Activity

  1. DanielHeckrath commented on Sep 27, 2017

    @DanielHeckrath

    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

  2. bi-kai commented on Sep 28, 2017

    @bi-kai
    Author

    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?

  3. DanielHeckrath commented on Sep 28, 2017

    @DanielHeckrath

    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);
    
  4. bi-kai commented on Oct 28, 2017

    @bi-kai
    Author

    That's greet!

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