feat: add logtargets option to control where homepage logs

pull/3066/head
Jon Seager 3 months ago
parent 619f365c92
commit 3be28a2c8b
No known key found for this signature in database

@ -3,15 +3,10 @@ import { format as utilFormat } from "node:util";
import winston from "winston";
import checkAndCopyConfig, { getSettings, CONF_DIR } from "utils/config/config";
import checkAndCopyConfig, { CONF_DIR, getSettings } from "utils/config/config";
let winstonLogger;
function init() {
checkAndCopyConfig("settings.yaml");
const settings = getSettings();
const logpath = settings.logpath || CONF_DIR;
function combineMessageAndSplat() {
return {
// eslint-disable-next-line no-unused-vars
@ -38,10 +33,8 @@ function init() {
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
}
winstonLogger = winston.createLogger({
level: process.env.LOG_LEVEL || "info",
transports: [
new winston.transports.Console({
function getConsoleLogger() {
return new winston.transports.Console({
format: winston.format.combine(
winston.format.errors({ stack: true }),
combineMessageAndSplat(),
@ -51,9 +44,14 @@ function init() {
),
handleExceptions: true,
handleRejections: true,
}),
});
}
function getFileLogger() {
const settings = getSettings();
const logpath = settings.logpath || CONF_DIR;
new winston.transports.File({
return new winston.transports.File({
format: winston.format.combine(
winston.format.errors({ stack: true }),
combineMessageAndSplat(),
@ -63,8 +61,31 @@ function init() {
filename: `${logpath}/logs/homepage.log`,
handleExceptions: true,
handleRejections: true,
}),
],
});
}
function init() {
checkAndCopyConfig("settings.yaml");
const configuredTargets = process.env.LOG_TARGETS || "both";
const loggingTransports = [];
switch (configuredTargets) {
case "both":
loggingTransports.push(getConsoleLogger(), getFileLogger());
break;
case "stdout":
loggingTransports.push(getConsoleLogger());
break;
case "file":
loggingTransports.push(getFileLogger());
break;
default:
loggingTransports.push(getConsoleLogger(), getFileLogger());
}
winstonLogger = winston.createLogger({
level: process.env.LOG_LEVEL || "info",
transports: loggingTransports,
});
// patch the console log mechanism to use our logger

Loading…
Cancel
Save