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.
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.
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.
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.
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.
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.