Merge pull request #251 from JazzFisch/patch-nextjs-logging
Patch console object to instead use homepage's logger for logging
This commit is contained in:
commit
ed28d69d76
4 changed files with 84 additions and 68 deletions
|
@ -1,4 +1,4 @@
|
|||
import logger from "utils/logger";
|
||||
import createLogger from "utils/logger";
|
||||
import genericProxyHandler from "utils/proxies/generic";
|
||||
import credentialedProxyHandler from "utils/proxies/credentialed";
|
||||
import rutorrentProxyHandler from "utils/proxies/rutorrent";
|
||||
|
@ -7,6 +7,8 @@ import npmProxyHandler from "utils/proxies/npm";
|
|||
import transmissionProxyHandler from "utils/proxies/transmission";
|
||||
import qbittorrentProxyHandler from "utils/proxies/qbittorrent";
|
||||
|
||||
const logger = createLogger('servicesProxy');
|
||||
|
||||
function asJson(data) {
|
||||
if (data?.length > 0) {
|
||||
const json = JSON.parse(data.toString());
|
||||
|
|
|
@ -10,6 +10,7 @@ import ServicesGroup from "components/services/group";
|
|||
import BookmarksGroup from "components/bookmarks/group";
|
||||
import Widget from "components/widget";
|
||||
import Revalidate from "components/revalidate";
|
||||
import createLogger from "utils/logger";
|
||||
import { getSettings } from "utils/config";
|
||||
import { ColorContext } from "utils/color-context";
|
||||
import { ThemeContext } from "utils/theme-context";
|
||||
|
@ -26,7 +27,9 @@ const ColorToggle = dynamic(() => import("components/color-toggle"), {
|
|||
const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"];
|
||||
|
||||
export function getStaticProps() {
|
||||
let logger;
|
||||
try {
|
||||
logger = createLogger('index');
|
||||
const { providers, ...settings } = getSettings();
|
||||
|
||||
return {
|
||||
|
@ -35,6 +38,7 @@ export function getStaticProps() {
|
|||
},
|
||||
};
|
||||
} catch (e) {
|
||||
if (logger) { logger.error(e); }
|
||||
return {
|
||||
props: {
|
||||
initialSettings: {},
|
||||
|
|
|
@ -1,80 +1,88 @@
|
|||
/* eslint-disable no-console */
|
||||
import { join } from "path";
|
||||
import { format as utilFormat } from "node:util"
|
||||
|
||||
import winston from "winston";
|
||||
|
||||
const configPath = join(process.cwd(), "config");
|
||||
let winstonLogger;
|
||||
|
||||
function messageFormatter(logInfo) {
|
||||
if (logInfo.stack) {
|
||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
||||
function init() {
|
||||
const configPath = join(process.cwd(), "config");
|
||||
|
||||
function combineMessageAndSplat() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
||||
};
|
||||
|
||||
const consoleFormat = winston.format.combine(
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.splat(),
|
||||
winston.format.timestamp(),
|
||||
winston.format.colorize(),
|
||||
winston.format.printf(messageFormatter)
|
||||
);
|
||||
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}`;
|
||||
}
|
||||
|
||||
const fileFormat = winston.format.combine(
|
||||
winston.format.errors({ stack: true }),
|
||||
winston.format.splat(),
|
||||
winston.format.timestamp(),
|
||||
winston.format.printf(messageFormatter)
|
||||
);
|
||||
if (logInfo.stack) {
|
||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
||||
}
|
||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
||||
};
|
||||
|
||||
const logger = winston.createLogger({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
format: consoleFormat,
|
||||
handleExceptions: true,
|
||||
handleRejections: true
|
||||
}),
|
||||
winstonLogger = winston.createLogger({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
transports: [
|
||||
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: fileFormat,
|
||||
filename: `${configPath}/logs/homepage.log`,
|
||||
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: `${configPath}/logs/homepage.log`,
|
||||
handleExceptions: true,
|
||||
handleRejections: true
|
||||
}),
|
||||
]
|
||||
});
|
||||
|
||||
function debug(message, ...args) {
|
||||
logger.debug(message, ...args);
|
||||
// patch the console log mechanism to use our logger
|
||||
const consoleMethods = ['log', 'debug', 'info', 'warn', 'error']
|
||||
consoleMethods.forEach(method => {
|
||||
// workaround for https://github.com/winstonjs/winston/issues/1591
|
||||
switch (method) {
|
||||
case 'log':
|
||||
console[method] = winstonLogger.info.bind(winstonLogger);
|
||||
break;
|
||||
default:
|
||||
console[method] = winstonLogger[method].bind(winstonLogger);
|
||||
break;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function verbose(message, ...args) {
|
||||
logger.verbose(message, ...args);
|
||||
}
|
||||
export default function createLogger(label) {
|
||||
if (!winstonLogger) {
|
||||
init();
|
||||
}
|
||||
|
||||
function info(message, ...args) {
|
||||
logger.info(message, ...args);
|
||||
}
|
||||
|
||||
function warn(message, ...args) {
|
||||
logger.warn(message, ...args);
|
||||
}
|
||||
|
||||
function error(message, ...args) {
|
||||
logger.error(message, ...args);
|
||||
}
|
||||
|
||||
function crit(message, ...args) {
|
||||
logger.crit(message, ...args);
|
||||
}
|
||||
|
||||
const thisModule = {
|
||||
debug,
|
||||
verbose,
|
||||
info,
|
||||
warn,
|
||||
error,
|
||||
crit
|
||||
};
|
||||
|
||||
export default thisModule;
|
||||
return winstonLogger.child({ label });
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
import getServiceWidget from "utils/service-helpers";
|
||||
import { formatApiCall } from "utils/api-helpers";
|
||||
import { httpProxy } from "utils/http";
|
||||
import logger from "utils/logger";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const logger = createLogger('genericProxyHandler');
|
||||
|
||||
export default async function genericProxyHandler(req, res, maps) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
|
Loading…
Add table
Reference in a new issue