nodejs日志文件在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10815218/
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
Where is nodejs log file?
提问by Eugeny Pavlenko
I can't find a place where nodejs log file is stored. Because in my node server I have "Segmentation fault", I want to look at log file for additional info...
我找不到存储 nodejs 日志文件的地方。因为在我的节点服务器中我有“分段错误”,我想查看日志文件以获取其他信息...
回答by ziad-saab
There is no log file. Each node.js "app" is a separate entity. By default it will log errors to STDERR and output to STDOUT. You can change that when you run it from your shell to log to a file instead.
没有日志文件。每个 node.js “应用程序”都是一个单独的实体。默认情况下,它会将错误记录到 STDERR 并输出到 STDOUT。当您从 shell 运行它以记录到文件时,您可以更改它。
node my_app.js > my_app_log.log 2> my_app_err.log
Alternatively (recommended), you can add logging inside your application either manually or with one of the many log libraries:
或者(推荐),您可以手动或使用许多日志库之一在应用程序中添加日志记录:
回答by Mario Haubenwallner
forevermight be of interest to you. It will run your .js-File 24/7 with logging options. Here are two snippets from the help text:
您可能对永远感兴趣。它将使用日志记录选项 24/7 运行您的 .js-File。以下是帮助文本中的两个片段:
[Long Running Process] The forever process will continue to run outputting log messages to the console. ex. forever -o out.log -e err.log my-script.js
[Long Running Process] Forever 进程将继续运行并输出日志信息到控制台。前任。永远 -o out.log -e err.log my-script.js
and
和
[Daemon] The forever process will run as a daemon which will make the target process start in the background. This is extremely useful for remote starting simple node.js scripts without using nohup. It is recommended to run start with -o -l, & -e. ex. forever start -l forever.log -o out.log -e err.log my-daemon.js forever stop my-daemon.js
[守护进程] 永远进程将作为守护进程运行,这将使目标进程在后台启动。这对于在不使用 nohup 的情况下远程启动简单的 node.js 脚本非常有用。建议使用 -o -l, & -e 开始运行。前任。永远开始 -l 永远.log -o out.log -e err.log my-daemon.js 永远停止 my-daemon.js
回答by Neil Guy Lindberg
If you use docker in your dev you can do this in another shell: docker attach running_node_app_container_name
如果您在开发中使用 docker,则可以在另一个 shell 中执行此操作:docker attach running_node_app_container_name
That will show you STDOUT and STDERR.
这将向您显示 STDOUT 和 STDERR。
回答by Rajiv Krishna
For nodejs log file you can use winston and morgan and in place of your console.log() statement user winston.log() or other winston methods to log. For working with winston and morgan you need to install them using npm. Example: npm i -S winston npm i -S morgan
对于 nodejs 日志文件,您可以使用 winston 和 morgan 并代替您的 console.log() 语句用户 winston.log() 或其他 winston 方法来记录。要使用 winston 和 morgan,您需要使用 npm 安装它们。示例: npm i -S winston npm i -S morgan
Then create a folder in your project with name winston and then create a config.js in that folder and copy this code given below.
然后在您的项目中创建一个名为 winston 的文件夹,然后在该文件夹中创建一个 config.js 并复制下面给出的代码。
const appRoot = require('app-root-path');
const winston = require('winston');
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
let logger;
if (process.env.logging === 'off') {
logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
],
exitOnError: false, // do not exit on handled exceptions
});
} else {
logger = winston.createLogger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
}
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write(message) {
logger.info(message);
},
};
module.exports = logger;
After copying the above code make make a folder with name logs parallel to winston or wherever you want and create a file app.log in that logs folder. Go back to config.js and set the path in the 5th line "filename: ${appRoot}/logs/app.log,
" to the respective app.log created by you.
复制上述代码后,创建一个名为 logs 的文件夹,与 winston 或任何您想要的位置平行,并在该日志文件夹中创建一个文件 app.log。返回 config.js 并将第 5 行“filename: ${appRoot}/logs/app.log,”中的路径设置为您创建的相应 app.log。
After this go to your index.js and include the following code in it.
在此之后转到您的 index.js 并在其中包含以下代码。
const morgan = require('morgan');
const winston = require('./winston/config');
const express = require('express');
const app = express();
app.use(morgan('combined', { stream: winston.stream }));
winston.info('You have successfully started working with winston and morgan');

