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 genericProxyHandler from "utils/proxies/generic";
|
||||||
import credentialedProxyHandler from "utils/proxies/credentialed";
|
import credentialedProxyHandler from "utils/proxies/credentialed";
|
||||||
import rutorrentProxyHandler from "utils/proxies/rutorrent";
|
import rutorrentProxyHandler from "utils/proxies/rutorrent";
|
||||||
|
@ -7,6 +7,8 @@ import npmProxyHandler from "utils/proxies/npm";
|
||||||
import transmissionProxyHandler from "utils/proxies/transmission";
|
import transmissionProxyHandler from "utils/proxies/transmission";
|
||||||
import qbittorrentProxyHandler from "utils/proxies/qbittorrent";
|
import qbittorrentProxyHandler from "utils/proxies/qbittorrent";
|
||||||
|
|
||||||
|
const logger = createLogger('servicesProxy');
|
||||||
|
|
||||||
function asJson(data) {
|
function asJson(data) {
|
||||||
if (data?.length > 0) {
|
if (data?.length > 0) {
|
||||||
const json = JSON.parse(data.toString());
|
const json = JSON.parse(data.toString());
|
||||||
|
|
|
@ -10,6 +10,7 @@ import ServicesGroup from "components/services/group";
|
||||||
import BookmarksGroup from "components/bookmarks/group";
|
import BookmarksGroup from "components/bookmarks/group";
|
||||||
import Widget from "components/widget";
|
import Widget from "components/widget";
|
||||||
import Revalidate from "components/revalidate";
|
import Revalidate from "components/revalidate";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
import { getSettings } from "utils/config";
|
import { getSettings } from "utils/config";
|
||||||
import { ColorContext } from "utils/color-context";
|
import { ColorContext } from "utils/color-context";
|
||||||
import { ThemeContext } from "utils/theme-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"];
|
const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"];
|
||||||
|
|
||||||
export function getStaticProps() {
|
export function getStaticProps() {
|
||||||
|
let logger;
|
||||||
try {
|
try {
|
||||||
|
logger = createLogger('index');
|
||||||
const { providers, ...settings } = getSettings();
|
const { providers, ...settings } = getSettings();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -35,6 +38,7 @@ export function getStaticProps() {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
if (logger) { logger.error(e); }
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
initialSettings: {},
|
initialSettings: {},
|
||||||
|
|
|
@ -1,42 +1,62 @@
|
||||||
|
/* eslint-disable no-console */
|
||||||
import { join } from "path";
|
import { join } from "path";
|
||||||
|
import { format as utilFormat } from "node:util"
|
||||||
|
|
||||||
import winston from "winston";
|
import winston from "winston";
|
||||||
|
|
||||||
|
let winstonLogger;
|
||||||
|
|
||||||
|
function init() {
|
||||||
const configPath = join(process.cwd(), "config");
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function messageFormatter(logInfo) {
|
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) {
|
if (logInfo.stack) {
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.stack}`;
|
||||||
}
|
}
|
||||||
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
return `[${logInfo.timestamp}] ${logInfo.level}: ${logInfo.message}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const consoleFormat = winston.format.combine(
|
winstonLogger = winston.createLogger({
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
winston.format.splat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.colorize(),
|
|
||||||
winston.format.printf(messageFormatter)
|
|
||||||
);
|
|
||||||
|
|
||||||
const fileFormat = winston.format.combine(
|
|
||||||
winston.format.errors({ stack: true }),
|
|
||||||
winston.format.splat(),
|
|
||||||
winston.format.timestamp(),
|
|
||||||
winston.format.printf(messageFormatter)
|
|
||||||
);
|
|
||||||
|
|
||||||
const logger = winston.createLogger({
|
|
||||||
level: process.env.LOG_LEVEL || 'info',
|
level: process.env.LOG_LEVEL || 'info',
|
||||||
transports: [
|
transports: [
|
||||||
new winston.transports.Console({
|
new winston.transports.Console({
|
||||||
format: consoleFormat,
|
format: winston.format.combine(
|
||||||
|
winston.format.errors({ stack: true}),
|
||||||
|
combineMessageAndSplat(),
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.colorize(),
|
||||||
|
winston.format.printf(messageFormatter)
|
||||||
|
),
|
||||||
handleExceptions: true,
|
handleExceptions: true,
|
||||||
handleRejections: true
|
handleRejections: true
|
||||||
}),
|
}),
|
||||||
|
|
||||||
new winston.transports.File({
|
new winston.transports.File({
|
||||||
format: fileFormat,
|
format: winston.format.combine(
|
||||||
|
winston.format.errors({ stack: true}),
|
||||||
|
combineMessageAndSplat(),
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.printf(messageFormatter)
|
||||||
|
),
|
||||||
filename: `${configPath}/logs/homepage.log`,
|
filename: `${configPath}/logs/homepage.log`,
|
||||||
handleExceptions: true,
|
handleExceptions: true,
|
||||||
handleRejections: true
|
handleRejections: true
|
||||||
|
@ -44,37 +64,25 @@ const logger = winston.createLogger({
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
function debug(message, ...args) {
|
// patch the console log mechanism to use our logger
|
||||||
logger.debug(message, ...args);
|
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) {
|
export default function createLogger(label) {
|
||||||
logger.verbose(message, ...args);
|
if (!winstonLogger) {
|
||||||
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
function info(message, ...args) {
|
return winstonLogger.child({ label });
|
||||||
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;
|
|
|
@ -1,7 +1,9 @@
|
||||||
import getServiceWidget from "utils/service-helpers";
|
import getServiceWidget from "utils/service-helpers";
|
||||||
import { formatApiCall } from "utils/api-helpers";
|
import { formatApiCall } from "utils/api-helpers";
|
||||||
import { httpProxy } from "utils/http";
|
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) {
|
export default async function genericProxyHandler(req, res, maps) {
|
||||||
const { group, service, endpoint } = req.query;
|
const { group, service, endpoint } = req.query;
|
||||||
|
|
Loading…
Add table
Reference in a new issue