Unitech / pm2

Node.js Production Process Manager with a built-in Load Balancer.
https://pm2.keymetrics.io/docs/usage/quick-start/
Other
41.5k stars 2.62k forks source link

pm2 integration with logstash #437

Closed jaromirmuller closed 7 years ago

jaromirmuller commented 10 years ago

Hello, we're using intensively logstash+elasticsearch (http://logstash.net/) for managing our logs. Is there any way how to integrate pm2 with logstash?

As I understand pm2 allows to log into file and also have real-time view in console with pm2 logs command. It's useful for development but I'm thinking about usage pm2 in production and ability to browse our logs via kibana interface is much more user friendly.

Is such integration new feature or could I use any module?

Unitech commented 10 years ago

All the logs are written by default into ~/.pm2/logs can you do something to use these files ? And have a look to the section log management https://github.com/Unitech/pm2#a9, there is also the --merge-logsoptions that can be helpful.

Tell us if you found something to share ! Thanks

michaelBenin commented 9 years ago

Here is a relevant link to formatting logs for logstash in nginx: https://blog.pkhamre.com/logging-to-logstash-json-format-in-nginx/

Any stdout from the app running would go to pm2's logs, make sure that format is in json that abides to logstash. I agree as this would be helpful if it came out of the box with pm2. Currently it doesn't format it to json.

Unitech commented 9 years ago

Would it be interesting to output the logs of application managed by PM2 in JSON?

michaelBenin commented 9 years ago

Is it possible to set the format like nginx does?

atomantic commented 9 years ago

Yes, JSON format output would be good. I'm also looking for logstash support. Can we get this reopened for discussion?

I'd like to be able to configure it via process.json like:

"log_format": "json"
michaelBenin commented 9 years ago

Ping.

solshark commented 8 years ago

Ping.

jlsjonas commented 8 years ago

pong?

On a serious note... generally supporting JSON-formatted logs would improve programmability a lot, so I would also like to recommend supporting an option to log to json instead of plaintext, for improved machine parseability.

Would a PR regarding this be accepted? If so I could look into it when I have time for it (soon).

soyuka commented 8 years ago

What do you mean by "JSON-formatted logs"? If you handle logs from your application, it works well.

Take a simple example:

// log.js
setInterval(function() { console.log(JSON.stringify({hello: 'world'}) })
pm2 start log.js
pm2 stop log.js
jq . ~/.pm2/logs/log-out-0.log
// Outputs json

If you want to pipe out JSON from pm2 logs add the --raw option so that pm2 doesn't add anything.

I don't see how PM2 could improve the logging workflow by implementing JSON output. BTW, should we respect a particular schema? How do we handle user context? This seems big work for no real improvement to me but I'm sure you'll have interesting arguments :).

jlsjonas commented 8 years ago

Looks like I didn't press save (correctly?) last time... in short:

bragatto commented 8 years ago

if @jlsjonas is willing to implement this, by all means, please let him. That would be an excellent way to import logs into an ELK stack more efficiently. +1 for this request, and my compliments for the initiative.

nidhhoggr commented 8 years ago

@jlsjonas Any luck with implementation. Anyone?

chrishelgert commented 8 years ago

any news for logfile in json-format?

jlsjonas commented 8 years ago

Sorry have been preoccupied, I should be able to find some time to implement the changes next week, I'll let you know ;)

Unitech commented 8 years ago

The solutions to implement this would be:

I guess the first solution would provide an easier way to integrate with logstash, as you do not need the log to be splitted by application name/id/error-out,

Maybe we can directly send the log to logstash? It looks like there are some modules for that (https://npms.io/search?term=logstash)

Unitech commented 8 years ago

You can refer to the pm2-syslog module that output app logs into /var/log/syslog

https://github.com/pm2-hive/pm2-syslog/blob/master/app.js#L1

Quite straightforward, 24 lines long!

Unitech commented 8 years ago
$ npm install Unitech/development -g
$ pm2 update

Now the pm2 logs can take the --json argument to ouptut json, hope you find this helpful for logstash integration

jlsjonas commented 8 years ago

Maybe we can directly send the log to logstash? It looks like there are some modules for that (https://npms.io/search?term=logstash)

That should be perfectly possible yes, it is how I've been handling my internal communications to ELK so far. Given the --json argument to pm2 logs a pipe should be able to handle most usecases, albeit possibly in a hackish way. I might still make a submodule for it if we decide on using ELK again for our next project. The main project we used ELK for became obsolete so this doesn't have a high priority to me anymore (+ pm2 logs --json gives a workaround).

However I would recommend keeping the issue open for now, since the work that remains should be pretty trivial; in case anyone else wants to tie their pm2 processes to ELK

Unitech commented 7 years ago
$ pm2 logs --json

Available since PM2 2.x