feat(logging): add log level env variable (#2299)
This commit is contained in:
@@ -5,11 +5,8 @@
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./src/index.d.ts",
|
||||
"default": "./src/index.mjs"
|
||||
},
|
||||
"./override": "./src/override.cjs"
|
||||
".": "./src/index.ts",
|
||||
"./env": "./src/env.ts"
|
||||
},
|
||||
"typesVersions": {
|
||||
"*": {
|
||||
@@ -26,9 +23,11 @@
|
||||
},
|
||||
"prettier": "@homarr/prettier-config",
|
||||
"dependencies": {
|
||||
"@homarr/env": "workspace:^0.1.0",
|
||||
"ioredis": "5.5.0",
|
||||
"superjson": "2.2.2",
|
||||
"winston": "3.17.0"
|
||||
"winston": "3.17.0",
|
||||
"zod": "^3.24.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@homarr/eslint-config": "workspace:^0.2.0",
|
||||
|
||||
10
packages/log/src/env.ts
Normal file
10
packages/log/src/env.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { createEnv } from "@homarr/env";
|
||||
|
||||
export const env = createEnv({
|
||||
server: {
|
||||
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]).default("info"),
|
||||
},
|
||||
experimental__runtimeEnv: process.env,
|
||||
});
|
||||
4
packages/log/src/index.d.ts
vendored
4
packages/log/src/index.d.ts
vendored
@@ -1,4 +0,0 @@
|
||||
import type { Logger } from "winston";
|
||||
|
||||
// The following is just to make prettier happy
|
||||
export const logger: Logger = undefined as unknown as Logger;
|
||||
@@ -1,12 +1,14 @@
|
||||
import type { transport as Transport } from "winston";
|
||||
import winston, { format, transports } from "winston";
|
||||
|
||||
import { RedisTransport } from "./redis-transport.mjs";
|
||||
import { env } from "./env";
|
||||
import { RedisTransport } from "./redis-transport";
|
||||
|
||||
const logMessageFormat = format.printf(({ level, message, timestamp }) => {
|
||||
return `${timestamp} ${level}: ${message}`;
|
||||
return `${timestamp as string} ${level}: ${message as string}`;
|
||||
});
|
||||
|
||||
const logTransports = [new transports.Console()];
|
||||
const logTransports: Transport[] = [new transports.Console()];
|
||||
|
||||
// Only add the Redis transport if we are not in CI
|
||||
if (!(Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS))) {
|
||||
@@ -16,6 +18,7 @@ if (!(Boolean(process.env.CI) || Boolean(process.env.DISABLE_REDIS_LOGS))) {
|
||||
const logger = winston.createLogger({
|
||||
format: format.combine(format.colorize(), format.timestamp(), logMessageFormat),
|
||||
transports: logTransports,
|
||||
level: env.LOG_LEVEL,
|
||||
});
|
||||
|
||||
export { logger };
|
||||
@@ -1,42 +0,0 @@
|
||||
void (async () => {
|
||||
const { logger } = await import("./index.mjs");
|
||||
|
||||
const nextLogger = require("next/dist/build/output/log");
|
||||
|
||||
const getWinstonMethodForConsole = (consoleMethod) => {
|
||||
switch (consoleMethod) {
|
||||
case "error":
|
||||
return (...messages) => logger.error(messages.join(" "));
|
||||
case "warn":
|
||||
return (...messages) => logger.warn(messages.join(" "));
|
||||
case "debug":
|
||||
return (...messages) => logger.debug(messages.join(" "));
|
||||
case "log":
|
||||
case "info":
|
||||
default:
|
||||
return (...messages) => logger.info(messages.join(" "));
|
||||
}
|
||||
};
|
||||
|
||||
const consoleMethods = ["log", "debug", "info", "warn", "error"];
|
||||
consoleMethods.forEach((method) => {
|
||||
console[method] = getWinstonMethodForConsole(method);
|
||||
});
|
||||
|
||||
const getWinstonMethodForNext = (nextMethod) => {
|
||||
switch (nextMethod) {
|
||||
case "error":
|
||||
return (...messages) => logger.error(messages.join(" "));
|
||||
case "warn":
|
||||
return (...messages) => logger.warn(messages.join(" "));
|
||||
case "trace":
|
||||
return (...messages) => logger.info(messages.join(" "));
|
||||
default:
|
||||
return (...messages) => logger.info(messages.join(" "));
|
||||
}
|
||||
};
|
||||
|
||||
Object.keys(nextLogger.prefixes).forEach((method) => {
|
||||
nextLogger[method] = getWinstonMethodForNext(method);
|
||||
});
|
||||
})();
|
||||
@@ -7,15 +7,12 @@ import Transport from "winston-transport";
|
||||
// of the base functionality and `.exceptions.handle()`.
|
||||
//
|
||||
export class RedisTransport extends Transport {
|
||||
/** @type {Redis} */
|
||||
redis;
|
||||
private redis: Redis | null = null;
|
||||
|
||||
/**
|
||||
* Log the info to the Redis channel
|
||||
* @param {{ message: string; timestamp: string; level: string; }} info
|
||||
* @param {() => void} callback
|
||||
*/
|
||||
log(info, callback) {
|
||||
log(info: { message: string; timestamp: string; level: string }, callback: () => void) {
|
||||
setImmediate(() => {
|
||||
this.emit("logged", info);
|
||||
});
|
||||
@@ -3,6 +3,6 @@
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
|
||||
},
|
||||
"include": ["*.ts", "src", "index.mjs"],
|
||||
"include": ["*.ts", "src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user