我找不到存放nodejs日志文件的地方。因为在我的节点服务器中我有“分段故障”,所以我想查看日志文件中的其他信息...
发布于 2017-01-07 05:32:54
如果你在你的开发中使用docker,你可以在另一个shell中完成这项工作: docker attach running_node_app_container_name
这将向您显示STDOUT和STDERR。
发布于 2018-10-31 14:43:36
对于nodejs日志文件,您可以使用winston和morgan,而不是console.log()语句、user winston.log()或其他winston方法来记录日志。要使用winston和morgan,您需要使用npm安装它们。例子: npm i摩根,winston npm i -S -S
然后,在项目中创建一个名为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;
复制完上面的代码后,创建一个带有名称日志的文件夹,使其与winston平行,或者在您想要的任何地方创建一个文件夹,并在该日志文件夹中创建一个文件app.log。返回config.js并将第5行"filename:${appRoot}/logs/app.log
“中的路径设置为您创建的相应app.log。
在此之后,转到您的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');
https://stackoverflow.com/questions/10815218
复制相似问题