Configuration parameters in pm2 to display colors in console
Asked Answered
D

3

8

I use pm2 to keep my node.js app alive, I am starting my app with a configuration file that looks like this:

{
    "name"        : "myApp",
    "script"      : "app.js",
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "ignoreWatch" : ["node_modules","public"],
    "watch"       : true,
}

Additionally I am using the package colors to identify different types of messages but all the logs lose their color when I run the command

pm2 logs

My question is: what can I add to my configuration file to be able to see the colors in console?

Demoralize answered 30/6, 2015 at 3:44 Comment(0)
P
10

When I did use a other logger modules, (like a colors, tracer )

(In my case, var logger = require('tracer').colorConsole(); as logger )

{
    "args": [ "--color" ]
}

this "args": "--color" keeps my logger's log color.

Insert that code in your pm2 script,

then you can see your color log by that command: pm2 logs "id" --raw (id is a pm2 id)

{
    "name"        : "myApp",
    "script"      : "app.js",
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",
    "ignoreWatch" : ["node_modules","public"],
    "watch"       : true,
    "args": [
        "--color"
    ]
}

The process is needed to pm2 stop, pm2 delete and new pm2 start using script.

Preacher answered 18/12, 2015 at 21:2 Comment(3)
which is equivalent to pm2 start app.js -- --color if you don't use a config file, as seen @ github.com/Unitech/pm2/issues/1719#issuecomment-151446644Acidity
I think pm2 delete implies pm2 stop, so you only need the latter.Bluejacket
Good question, good answer - exactly what I needed! I found that this works with pm2 reload ecosystem.json but not with pm2 reload <id> for some reason.Ils
H
3

I found I also needed to add this option for debug to my pm2 process.json file

"env": { "DEBUG_COLORS": true }

then can view colored debug logs with both pm2 logs and pm2 logs --raw

Hearn answered 8/6, 2016 at 8:42 Comment(0)
F
2

Try using the --raw switch. Like this —

pm2 logs --raw

Fatuitous answered 30/6, 2015 at 8:20 Comment(2)
Strange. It works for me. What version of pm2 are you on?Fatuitous
I updated to 0.14.3 and I don't get the error anymore, thanks :)Demoralize

© 2022 - 2024 — McMap. All rights reserved.