Currently, I have a NodeJS program which logs a lot of info to the console, as usual for many NodeJS programs. I was wondering if there is a way to have an output log for everything that is logged into the console with the file named after the timestamp it was created.
I have been researching online for ways to save the logs and so far I have been able to find redirection, such as node app.js > logs/bot.log
. Although this works great, I needed pm2 so that any crashes restart yet I have not been able to find how to make pm2 have timestamped logs. I have found out that pm2 can have logs, but I've had trouble figuring out how to fix them for my needs.
Not much code is needed to show this, but I just need all messages that are in the console to be logged to a file.
console.log("Sometimes, I dream about cheese...");
Ideally, I would like that and all other logs from before to be saved into a file called bot.log
inside of the folder logs
( /logs/bot.log
).
I'm hoping that I'll be able to receive help towards saving logs with the timestamps as their names of everything that has been printed to console, virtually making copies of the console that I can access at later dates for debugging etc. Thank you for any help!
PM2 does log to files.
You can view logs for specific or all applications with these commands:
# Display option for pm2 logs command
pm2 logs -h
# Display all apps logs
pm2 logs
# Display only `api` application logs
pm2 logs api
# Display X lines of api log file
pm2 logs big-api --lines 1000
You can also specify the path to both output and error logs with
pm2 start yourapp.js --output /logs/bot.log --error /logs/boterror.log
There is more including log rotation for managing large log files here: http://pm2.keymetrics.io/docs/usage/log-management/
You can be a little more specific if you need more help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.