我不明白 deno 日志记录 API。有人可以解释一下吗? 我正在尝试将所有消息(错误、警告、信息等)记录到一个文件中。如果能有一个时间戳就好了。
这里是一些代码,基本上来自文档中的示例。 没有任何内容写入日志文件。
import * as log from "https://deno.land/std/log/mod.ts";
async function startLogger() {
await log.setup({
handlers: {
console: new log.handlers.ConsoleHandler("DEBUG"),
file: new log.handlers.FileHandler("WARNING", {
filename: "./logTest.txt",
// you can change format of output message using any keys in `LogRecord`
formatter: "{levelName} {msg}",
}),
},
loggers: {
// configure default logger available via short-hand methods above
default: {
level: "DEBUG",
handlers: ["console", "file"],
},
tasks: {
level: "ERROR",
handlers: ["console"],
},
},
});
// get default logger
const logger = log.getLogger();
return logger
}
var logger = await startLogger()
logger.debug('debug should this show up in a file?')
logger.error('error should this show up in a file?')
logger.warning('warn should this show up in a file?')
logger.info('warn should this show up in a file?')
deno run --allow-write --allow-read testLogger.js
最佳答案
FileHandler
每 30 秒刷新一次到磁盘。如果等待 30 秒,您将看到数据正在写入。
来自文档:
This handler uses a buffer for writing to file and will automatically flush every 30 seconds, though you can trigger this yourself with
fileHandler.flush()
. Log messages with a log level greater than error are immediately flushed.
关于logging - 如何使用 Deno 记录到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62181019/