我目前正在学习在 nodejs 的上下文中进行日志记录。目前我的设置使用 morgan 作为快速中间件,winston 作为我的日志记录。我的问题是摩根使用什么级别的日志记录?到目前为止,这是我的 appjs。我正在尝试决定如何拆分我的日志记录。
import bodyParser from "body-parser";
import express from "express";
import helmet from "helmet";
import morgan from "morgan";
import path from "path";
import { logger, stream } from "./utils/logger";
import es from "./esClient";
const app = express();
app.use(helmet());
app.use(morgan("combined", { stream }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
export default app;
这是我导入的记录器文件
import winston from "winston";
import path from "path";
winston.emitErrs = true;
const logger = new winston.Logger({
transports: [
new winston.transports.File({
level: "info",
name: "info",
filename: path.join(__dirname,"..","..","logs/access.log"),
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: true
}),
new winston.transports.File({
level: "verbose",
name: "elasticsearch",
filename: path.join(__dirname,"..","..","logs/elasticsearch.log"),
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: true
}),
new winston.transports.File({
level: "error",
name: "error",
filename: path.join(__dirname,"..","..","logs/errors.log"),
json: true,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: true
}),
new winston.transports.Console({
colorize: true,
level: "warning"
})
]
});
const stream = {
write: function(message, encoding){
logger.info(message);
}
};
export { logger, stream };
编辑:为澄清起见,我不希望将我的 http 请求日志记录到我的数据库日志中。有没有办法做到这一点?