mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-01 21:13:39 +01:00
Enhancement: support LOG_TARGETS
environment variable (#3075)
This commit is contained in:
parent
9caede1cc3
commit
8b029ac11c
@ -406,6 +406,8 @@ By default the homepage logfile is written to the a `logs` subdirectory of the `
|
|||||||
logpath: /logfile/path
|
logpath: /logfile/path
|
||||||
```
|
```
|
||||||
|
|
||||||
|
By default, logs are sent both to `stdout` and to a file at the path specified. This can be changed by setting the `LOG_TARGETS` environment variable to one of `both` (default), `stdout` or `file`.
|
||||||
|
|
||||||
## Show Docker Stats
|
## Show Docker Stats
|
||||||
|
|
||||||
You can show all docker stats expanded in `settings.yaml`:
|
You can show all docker stats expanded in `settings.yaml`:
|
||||||
|
@ -3,68 +3,89 @@ import { format as utilFormat } from "node:util";
|
|||||||
|
|
||||||
import winston from "winston";
|
import winston from "winston";
|
||||||
|
|
||||||
import checkAndCopyConfig, { getSettings, CONF_DIR } from "utils/config/config";
|
import checkAndCopyConfig, { CONF_DIR, getSettings } from "utils/config/config";
|
||||||
|
|
||||||
let winstonLogger;
|
let winstonLogger;
|
||||||
|
|
||||||
function init() {
|
function combineMessageAndSplat() {
|
||||||
checkAndCopyConfig("settings.yaml");
|
return {
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
transform: (info, opts) => {
|
||||||
|
// combine message and args if any
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
|
info.message = utilFormat(info.message, ...(info[Symbol.for("splat")] || []));
|
||||||
|
return info;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function messageFormatter(logInfo) {
|
||||||
|
if (logInfo.label) {
|
||||||
|
if (logInfo.stack) {
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.stack}`;
|
||||||
|
}
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.message}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (logInfo.stack) {
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
||||||
|
}
|
||||||
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getConsoleLogger() {
|
||||||
|
return new winston.transports.Console({
|
||||||
|
format: winston.format.combine(
|
||||||
|
winston.format.errors({ stack: true }),
|
||||||
|
combineMessageAndSplat(),
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.colorize(),
|
||||||
|
winston.format.printf(messageFormatter),
|
||||||
|
),
|
||||||
|
handleExceptions: true,
|
||||||
|
handleRejections: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFileLogger() {
|
||||||
const settings = getSettings();
|
const settings = getSettings();
|
||||||
const logpath = settings.logpath || CONF_DIR;
|
const logpath = settings.logpath || CONF_DIR;
|
||||||
|
|
||||||
function combineMessageAndSplat() {
|
return new winston.transports.File({
|
||||||
return {
|
format: winston.format.combine(
|
||||||
// eslint-disable-next-line no-unused-vars
|
winston.format.errors({ stack: true }),
|
||||||
transform: (info, opts) => {
|
combineMessageAndSplat(),
|
||||||
// combine message and args if any
|
winston.format.timestamp(),
|
||||||
// eslint-disable-next-line no-param-reassign
|
winston.format.printf(messageFormatter),
|
||||||
info.message = utilFormat(info.message, ...(info[Symbol.for("splat")] || []));
|
),
|
||||||
return info;
|
filename: `${logpath}/logs/homepage.log`,
|
||||||
},
|
handleExceptions: true,
|
||||||
};
|
handleRejections: true,
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function messageFormatter(logInfo) {
|
function init() {
|
||||||
if (logInfo.label) {
|
checkAndCopyConfig("settings.yaml");
|
||||||
if (logInfo.stack) {
|
const configuredTargets = process.env.LOG_TARGETS || "both";
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.stack}`;
|
const loggingTransports = [];
|
||||||
}
|
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: <${logInfo.label}> ${logInfo.message}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (logInfo.stack) {
|
switch (configuredTargets) {
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
case "both":
|
||||||
}
|
loggingTransports.push(getConsoleLogger(), getFileLogger());
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
break;
|
||||||
|
case "stdout":
|
||||||
|
loggingTransports.push(getConsoleLogger());
|
||||||
|
break;
|
||||||
|
case "file":
|
||||||
|
loggingTransports.push(getFileLogger());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
loggingTransports.push(getConsoleLogger(), getFileLogger());
|
||||||
}
|
}
|
||||||
|
|
||||||
winstonLogger = winston.createLogger({
|
winstonLogger = winston.createLogger({
|
||||||
level: process.env.LOG_LEVEL || "info",
|
level: process.env.LOG_LEVEL || "info",
|
||||||
transports: [
|
transports: loggingTransports,
|
||||||
new winston.transports.Console({
|
|
||||||
format: winston.format.combine(
|
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
combineMessageAndSplat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.colorize(),
|
|
||||||
winston.format.printf(messageFormatter),
|
|
||||||
),
|
|
||||||
handleExceptions: true,
|
|
||||||
handleRejections: true,
|
|
||||||
}),
|
|
||||||
|
|
||||||
new winston.transports.File({
|
|
||||||
format: winston.format.combine(
|
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
combineMessageAndSplat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.printf(messageFormatter),
|
|
||||||
),
|
|
||||||
filename: `${logpath}/logs/homepage.log`,
|
|
||||||
handleExceptions: true,
|
|
||||||
handleRejections: true,
|
|
||||||
}),
|
|
||||||
],
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// patch the console log mechanism to use our logger
|
// patch the console log mechanism to use our logger
|
||||||
|
Loading…
x
Reference in New Issue
Block a user