node.js pm2 更改日志文件位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45953322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pm2 changing log file location
提问by Jeet
I have couple of questions regarding pm2
我有几个关于 pm2 的问题
- How can I change the location of
server-error-0.logandserver-out-0.logfiles location fromc:\users\user\.pm2\logsto other drive, due to restriction in server's c drive access. - Can I log the error and info in database instead of a log file? Do I need to write a separate module for that or is there any way to achieve this?
- 由于服务器的 c 驱动器访问限制,我如何将位置
server-error-0.log和server-out-0.log文件位置从c:\users\user\.pm2\logs其他驱动器更改为其他驱动器。 - 我可以在数据库而不是日志文件中记录错误和信息吗?我是否需要为此编写一个单独的模块,或者有什么方法可以实现这一点?
回答by shaochuancs
How can I change the location of ...log file location?
如何更改 ...日志文件位置的位置?
To change pm2's log file location, there are 2 solutions: define log path as parameter when pm2 command is executed (-l, -o, -e), or start pm2 from a configuration file.
到变化PM2的日志文件位置,有2个解决方案:执行PM2命令时定义日志路径作为参数(-l,-o,-e),或从配置文件开始PM2。
For the parameter solution, here is an example:
对于参数解决方案,这里是一个例子:
pm2 start app.js -o ./out.log -e ./err.log
If you don't want to define log path every time when pm2 is executed, you can generate a configuration file, define error_fileand out_file, and start pm2 from that:
如果不想每次执行 pm2 时都定义日志路径,可以生成一个配置文件,定义error_file和out_file,然后从那里启动 pm2:
Generate a configuration file:
pm2 ecosystem simple. This would generate a fileecosystem.config.js, with following content:module.exports = { apps : [{ name : "app1", script : "./app.js" }] }Define
error_file(for error log) andout_file(for info log) in the file, such as:module.exports = { apps : [{ name : "app1", script : "./app.js", error_file : "./err.log", out_file : "./out.log" }] }Delete existing processes in pm2:
pm2 delete <pid>You can get pid by doing:
pm2 statusStart the process from the configuration file:
pm2 start ecosystem.config.js
生成配置文件:
pm2 ecosystem simple. 这将生成一个文件ecosystem.config.js,内容如下:module.exports = { apps : [{ name : "app1", script : "./app.js" }] }在文件中定义
error_file(用于错误日志)和out_file(用于信息日志),例如:module.exports = { apps : [{ name : "app1", script : "./app.js", error_file : "./err.log", out_file : "./out.log" }] }删除 pm2 中的现有进程:
pm2 delete <pid>您可以通过执行以下操作来获取 pid:
pm2 status从配置文件启动进程:
pm2 start ecosystem.config.js
In this way, the logs are saved to ./err.logand ./out.log.
这样,日志就保存到./err.log和./out.log。
Please refer to the documentfor detail information.
有关详细信息,请参阅文档。
Can I log the error and info in database instead of a log file?
我可以在数据库而不是日志文件中记录错误和信息吗?
I didn't find any resources in official document. It seems you need to write code and save log to database yourself.
我在官方文档中没有找到任何资源。看来您需要自己编写代码并将日志保存到数据库中。
回答by shaochuancs
Just wanted to add to @shaochuancs answer, that before doing step 3, make sure you delete the old process. If you don't delete the old process, the changes that you made to your process file will not take into effect after you start your app.
只是想添加到@shaochuancs 的答案中,在执行第 3 步之前,请确保删除旧进程。如果您不删除旧流程,则您对流程文件所做的更改在您启动应用程序后将不会生效。
You will need to issue this command before doing step 3 above:
在执行上述步骤 3 之前,您需要发出此命令:
pm2 delete <pid>
回答by Adiii
If you want to write both an error log and console log to the same file, might be a use case, like I am interested to have log in OneFile to push to ELK.you can use -l
如果您想将错误日志和控制台日志写入同一个文件,可能是一个用例,就像我有兴趣登录 OneFile 以推送到 ELK。您可以使用 -l
-l --log [path] specify filepath to output both out and error logs
Here is the example
这是例子
pm2 start server.js -l /app/logs/server.log
After doing changes do not forget to run this command as mentioned in the answer.
进行更改后,不要忘记按照答案中的说明运行此命令。
pm2 delete <pid>
回答by Ivan Vovk
In case you want pm2 on startup with changed logs path:
如果您希望 pm2 在启动时更改日志路径:
- pm2 delete all
- pm2 start ecosystem.js
- pm2 save
- pm2 startup
- pm2 全部删除
- pm2 启动生态系统.js
- pm2 保存
- pm2 启动

