Arĝentakato
Arĝentakato
DDeno
Created by Arĝentakato on 4/2/2024 in #help
Logger Structure args
I am implementing a simple access log in Deno Fresh, but I am not quite sure how to use the standard library Logger. I want to record logs with remote host etc. at the beginning of the sentence like nginx logs, but can I specify the type of log argument? Currently, the implementation is as follows:
log.setup({
handlers: {
accsessLoggerFmt: new log.ConsoleHandler("INFO", {
formatter: (record) => {
const { datetime, levelName, msg, args } = record;

const d = new Date(datetime.getTime() - datetime.getTimezoneOffset() * 6e4);
const logTime = d.toISOString().slice(0, -5) + d.toString().replace(/^.*GMT([-+]\d{2})(\d{2}).*$/, "$1:$2");

let log = `${logTime} ${levelName.padEnd(7)}`;

for (const arg of args) {
log += ` ${arg}`;
}

log += ` ${msg}`;

return log;
}),
},
loggers: {
accessLogger: {
level: "INFO",
handlers: ["accsessLoggerFmt"],
},
},
});

const requestId = ...
const ip = ...

logger.info("Something", requestId, ip);
log.setup({
handlers: {
accsessLoggerFmt: new log.ConsoleHandler("INFO", {
formatter: (record) => {
const { datetime, levelName, msg, args } = record;

const d = new Date(datetime.getTime() - datetime.getTimezoneOffset() * 6e4);
const logTime = d.toISOString().slice(0, -5) + d.toString().replace(/^.*GMT([-+]\d{2})(\d{2}).*$/, "$1:$2");

let log = `${logTime} ${levelName.padEnd(7)}`;

for (const arg of args) {
log += ` ${arg}`;
}

log += ` ${msg}`;

return log;
}),
},
loggers: {
accessLogger: {
level: "INFO",
handlers: ["accsessLoggerFmt"],
},
},
});

const requestId = ...
const ip = ...

logger.info("Something", requestId, ip);
A current implementation requires inserting the remote host or other information to be displayed in the argument each time, which we believe is tedious and prone to errors. I have read the documentation but could not come up with a good solution. Any advice would be appreciated.
6 replies