简体   繁体   English

PM2登录到带有时间戳的文件

[英]PM2 Log to a file with timestamps

Currently, I have a NodeJS program which logs a lot of info to the console, as usual for many NodeJS programs. 当前,我有一个NodeJS程序,与许多NodeJS程序一样,该程序将很多信息记录到控制台。 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 . 我一直在在线研究保存日志的方法,到目前为止,我已经能够找到重定向,例如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. 尽管这很好用,但我需要pm2,以便重新启动任何崩溃,但我仍然找不到如何使pm2带有时间戳的日志。 I have found out that pm2 can have logs, but I've had trouble figuring out how to fix them for my needs. 我发现pm2可以包含日志,但是我在确定如何解决它们的问题时遇到了麻烦。

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 ). 理想情况下,我希望将之前的所有其他日志以及所有其他日志保存到文件夹logs/logs/bot.log )内部的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. PM2确实记录到文件。

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/ 此处还包括用于管理大型日志文件的日志轮换功能: http : //pm2.keymetrics.io/docs/usage/log-management/

You can be a little more specific if you need more help 如果您需要更多帮助,可以更具体一些

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM