问题描述
我正在尝试在 Node.js 应用程序中实现日志记录。 为此,我选择了 log4js,它工作了一段时间,但突然开始行动了。
当我的日志文件未创建时,一切运行顺利。日志文件被创建,不同类型的日志级别都写入其中。然后我第二次运行我的代码时,它崩溃并出现以下错误:
(node:14468) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory,copyfile 'C:\Users\username\Documents\code\js\logging\test.log' -> 'C:\Users\username\Documents\code\js\logging\test.log.04-03-2021:15:05:50'
(node:14468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:14468) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
它似乎找不到它创建的日志文件,即使错误消息中的路径是正确的。
我制作了一个小代码示例,它重现了我的错误。 我有一个 app.js:
const logger = require('./logger.js');
logger.writeInfo("Info test");
logger.writeError("Error test");
logger.writeWarning("Warning test");
还有一个 logger.js:
const log4js = require('log4js');
const logger = log4js.getLogger();
log4js.configure({
appenders: {
fileAppender:
{
type: 'file',filename: 'test.log',pattern: 'dd-MM-yyyy:hh:mm:ss'
}
},categories: {
default:
{
appenders: ['fileAppender'],level: 'info'
}
}
});
module.exports = {
writeInfo: (message) => {
logger.info(message);
},writeWarning: (message) => {
logger.warn(message);
},writeError: (message) => {
logger.error(message);
}
};
第一次运行后,我的test.log的输出是:
[2021-03-04T15:05:50.978] [INFO] default - Info test
[2021-03-04T15:05:50.979] [ERROR] default - Error test
[2021-03-04T15:05:50.979] [WARN] default - Warning test
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)