Logging with PM2

Will Button
InstructorWill Button
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

This lesson shows you how to redirect pm2 logs to a specific folder, such as the Linux standard /var/log folder.

[00:02] By default PM2 will write its logs to the .pm2 folder of whatever directory PM2 happens to be running in. Eventually we want to turn this system over to our sysadmins and put it in production, and they're going to support and maintain it. So it's going to work in our best interest if we move those logs out into a standard location. For Linux, that standard logging location is called varlog, you can see there's already all of this system and application logs here, and we want to add our own to it.

[00:33] The first thing we want to do is create a folder that is dedicated for our logs. We'll do that using the make dir or make D-I-R command, and we'll call our folder PM2. We'll also want to change the ownership of that folder so that it's owned by the process running PM2, which in this case is our node user, and we're going to do that so that PM2 can write to that folder. Let's go ahead and go back into our config file, and let's change a couple of things to take advantage of that.

[01:10] We're going to add the log file object, and we're going to set that to be the path that we just created. We want this to be app1.log, and we want to capture our error logs too, so we're going to create an error file object and now whenever an error or an application log is written, those are going to our default folders. I'm going to do the same thing for app2 here. That's all set up, and it looks correct.

[01:45] Now we've got PM2 running and maintaining both of our apps, but we've changed our configuration file, so we want to essentially blow out that config and rebuild it. We're going to do that with the PM2 delete command, and the pass in our configuration object and it will basically run through that configuration object, the JSON file, and delete everything that's defined in it.

[02:15] You can see in the output here the processes were restarted, in this case they were killed, and the running processes it's just a blank box because there's nothing running. Then we can bring it all back up by issuing PM2 start and then the name of our config file. Now it's up and running, and now if we take a look at our varlog\PM2 folder, we've got all of our log files that were created here.

[02:47] You can see that PM2 appends the ID number to the name of your log file, so that if you have a similar or duplicate file name you'll be able to distinguish those. If we look inside of one of those log files, if we just pick app1 log you can see we haven't done anything with it really, so there's not a lot of output there, but you can see that it is writing to the log successfully.

egghead
egghead
~ 26 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today