在该操作发生所有事情后,Express 会记录资源使用情况
Express logs resource usage after everything happened for that operation
我正在使用 Express 4.17.1
— 和 Winston 3.3.3
/ Morgan 1.10.0
进行日志记录,我已经设法以这种方式配置:
import winston from "winston";
const options = {
console: {
colorize: true,
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.printf((msg) => {
return `${msg.timestamp} [${msg.level}] - ${msg.message}`;
})
),
handleExceptions: true,
json: true,
level: "debug",
},
};
const logger = winston.createLogger({
exitOnError: false,
transports: [new winston.transports.Console(options.console)], // alert > error > warning > notice > info > debug
});
logger.stream = {
write: (message) => {
logger.info(message.slice(0, -1)); // ...use lowest log level so the output will be picked up by any transports
},
};
export { logger };
然后我在 application.js
中导入 Winston 配置,所有 Express 设置都是这样完成的:
import express from "express";
import morgan from "morgan";
import { logger } from "./config/winston.js";
const app = express();
app.use(express.json());
// ...some more settings
app.use(morgan("dev", { stream: logger.stream })); // combined, dev
// ...route Definitions
export { app };
我遇到的问题是某些日志语句看起来像是 Winston(我猜)根据处理过的 Express 路由(我喜欢)自动处理的,但它们是在所有常用语句之后记录的对应的工作流程。这是一个例子:
2021-03-03T16:25:22.199Z [info] - Searching all customers...
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: 'dFKYVOlaQyJswbxoex7Y6',
sql: 'select `customers`.* from `customers`'
}
2021-03-03T16:25:22.203Z [info] - Done with all customers workflow...
2021-03-03T16:25:22.215Z [info] - GET /api/customers 200 11.727 ms - 395
我希望 2021-03-03T16:25:22.215Z [info] - GET /api/customers 200 11.727 ms - 395
成为该工作流程中的第一个部分,因为它是工作流程的开始,但它在最后被记录——这有点令人困惑。
有没有办法解决这个问题,或者这是设计使然吗?如果可以修复,我应该在 Winston 配置或其他任何地方 add/remove/tweak add/remove/tweak。
根据 morgan-documentation,有一个设置允许您在请求时而不是在响应[=19]时写入访问日志=](后者是默认值):
Write log line on request instead of response. This means that a
requests will be logged even if the server crashes, but data from the
response (like the response code, content length, etc.) cannot be
logged.
因此将 { immediate: true }
传递给 morgan 应该使日志出现在工作流的开头,但日志显然只包含请求相关数据。
我正在使用 Express 4.17.1
— 和 Winston 3.3.3
/ Morgan 1.10.0
进行日志记录,我已经设法以这种方式配置:
import winston from "winston";
const options = {
console: {
colorize: true,
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
winston.format.printf((msg) => {
return `${msg.timestamp} [${msg.level}] - ${msg.message}`;
})
),
handleExceptions: true,
json: true,
level: "debug",
},
};
const logger = winston.createLogger({
exitOnError: false,
transports: [new winston.transports.Console(options.console)], // alert > error > warning > notice > info > debug
});
logger.stream = {
write: (message) => {
logger.info(message.slice(0, -1)); // ...use lowest log level so the output will be picked up by any transports
},
};
export { logger };
然后我在 application.js
中导入 Winston 配置,所有 Express 设置都是这样完成的:
import express from "express";
import morgan from "morgan";
import { logger } from "./config/winston.js";
const app = express();
app.use(express.json());
// ...some more settings
app.use(morgan("dev", { stream: logger.stream })); // combined, dev
// ...route Definitions
export { app };
我遇到的问题是某些日志语句看起来像是 Winston(我猜)根据处理过的 Express 路由(我喜欢)自动处理的,但它们是在所有常用语句之后记录的对应的工作流程。这是一个例子:
2021-03-03T16:25:22.199Z [info] - Searching all customers...
{
method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: 'dFKYVOlaQyJswbxoex7Y6',
sql: 'select `customers`.* from `customers`'
}
2021-03-03T16:25:22.203Z [info] - Done with all customers workflow...
2021-03-03T16:25:22.215Z [info] - GET /api/customers 200 11.727 ms - 395
我希望 2021-03-03T16:25:22.215Z [info] - GET /api/customers 200 11.727 ms - 395
成为该工作流程中的第一个部分,因为它是工作流程的开始,但它在最后被记录——这有点令人困惑。
有没有办法解决这个问题,或者这是设计使然吗?如果可以修复,我应该在 Winston 配置或其他任何地方 add/remove/tweak add/remove/tweak。
根据 morgan-documentation,有一个设置允许您在请求时而不是在响应[=19]时写入访问日志=](后者是默认值):
Write log line on request instead of response. This means that a requests will be logged even if the server crashes, but data from the response (like the response code, content length, etc.) cannot be logged.
因此将 { immediate: true }
传递给 morgan 应该使日志出现在工作流的开头,但日志显然只包含请求相关数据。