feat: add winston logger (#143)

* feat: add winston logger

* refactor: improve usage of winston logger overide, remove start.js file and logger.js file used for override

* fix: formatting

* fix: formatting
This commit is contained in:
Meier Lukas
2024-02-20 21:28:27 +01:00
committed by GitHub
parent b4749e7091
commit 3d50f8165f
10 changed files with 148 additions and 43 deletions

View File

@@ -14,4 +14,7 @@ AUTH_URL='http://localhost:3000'
# @see https://next-auth.js.org/configuration/options#secret
AUTH_SECRET='supersecret'
TURBO_TELEMETRY_DISABLED=1
TURBO_TELEMETRY_DISABLED=1
# Configure logging to use winston logger
NODE_OPTIONS='-r @homarr/log/override'

View File

@@ -20,6 +20,7 @@
"@homarr/definitions": "workspace:^0.1.0",
"@homarr/form": "workspace:^0.1.0",
"@homarr/gridstack": "^1.0.0",
"@homarr/log": "workspace:^",
"@homarr/notifications": "workspace:^0.1.0",
"@homarr/spotlight": "workspace:^0.1.0",
"@homarr/translation": "workspace:^0.1.0",

View File

@@ -13,7 +13,7 @@
"db:push": "pnpm -F db push",
"db:studio": "pnpm -F db studio",
"db:migration:generate": "pnpm -F db migration:generate",
"dev": "node start.js",
"dev": "turbo dev --parallel",
"format": "turbo format --continue -- --cache --cache-location node_modules/.cache/.prettiercache",
"format:fix": "turbo format --continue -- --write --cache --cache-location node_modules/.cache/.prettiercache",
"lint": "turbo lint --continue -- --cache --cache-location node_modules/.cache/.eslintcache",
@@ -38,8 +38,5 @@
"vite-tsconfig-paths": "^4.3.1",
"vitest": "^1.3.1"
},
"prettier": "@homarr/prettier-config",
"dependencies": {
"winston": "^3.11.0"
}
}
"prettier": "@homarr/prettier-config"
}

42
packages/log/package.json Normal file
View File

@@ -0,0 +1,42 @@
{
"name": "@homarr/log",
"private": true,
"version": "0.1.0",
"exports": {
".": {
"types": "./src/index.d.ts",
"default": "./src/index.mjs"
},
"./override": "./src/override.cjs"
},
"typesVersions": {
"*": {
"*": [
"src/*"
]
}
},
"license": "MIT",
"scripts": {
"clean": "rm -rf .turbo node_modules",
"lint": "eslint .",
"format": "prettier --check . --ignore-path ../../.gitignore",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"winston": "3.11.0"
},
"devDependencies": {
"@homarr/eslint-config": "workspace:^0.2.0",
"@homarr/prettier-config": "workspace:^0.1.0",
"@homarr/tsconfig": "workspace:^0.1.0",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
},
"eslintConfig": {
"extends": [
"@homarr/eslint-config/base"
]
},
"prettier": "@homarr/prettier-config"
}

4
packages/log/src/index.d.ts vendored Normal file
View File

@@ -0,0 +1,4 @@
import type { Logger } from "winston";
// The following is just to make prettier happy
export const logger: Logger = undefined as unknown as Logger;

View File

@@ -0,0 +1,16 @@
import winston, { format, transports } from "winston";
const logMessageFormat = format.printf(({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
});
const logger = winston.createLogger({
format: format.combine(
format.colorize(),
format.timestamp(),
logMessageFormat,
),
transports: [new transports.Console()],
});
export { logger };

View File

@@ -0,0 +1,45 @@
void (async () => {
const { logger } = await import("./index.mjs");
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-var-requires
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(" "));
}
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
Object.keys(nextLogger.prefixes).forEach((method) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
nextLogger[method] = getWinstonMethodForNext(method);
});
})();

View File

@@ -0,0 +1,8 @@
{
"extends": "@homarr/tsconfig/base.json",
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["*.ts", "src", "index.mjs"],
"exclude": ["node_modules"]
}

29
pnpm-lock.yaml generated
View File

@@ -7,10 +7,6 @@ settings:
importers:
.:
dependencies:
winston:
specifier: ^3.11.0
version: 3.11.0
devDependencies:
'@homarr/prettier-config':
specifier: workspace:^0.1.0
@@ -75,6 +71,9 @@ importers:
'@homarr/gridstack':
specifier: ^1.0.0
version: 1.0.0
'@homarr/log':
specifier: workspace:^
version: link:../../packages/log
'@homarr/notifications':
specifier: workspace:^0.1.0
version: link:../../packages/notifications
@@ -409,6 +408,28 @@ importers:
specifier: ^5.3.3
version: 5.3.3
packages/log:
dependencies:
winston:
specifier: 3.11.0
version: 3.11.0
devDependencies:
'@homarr/eslint-config':
specifier: workspace:^0.2.0
version: link:../../tooling/eslint
'@homarr/prettier-config':
specifier: workspace:^0.1.0
version: link:../../tooling/prettier
'@homarr/tsconfig':
specifier: workspace:^0.1.0
version: link:../../tooling/typescript
eslint:
specifier: ^8.56.0
version: 8.56.0
typescript:
specifier: ^5.3.3
version: 5.3.3
packages/notifications:
dependencies:
'@homarr/ui':

View File

@@ -1,32 +0,0 @@
import childProcess from "child_process";
import winston from "winston";
const logMessageFormat = winston.format.printf(
({ level, message, timestamp }) => {
return `${timestamp} ${level}: ${message}`;
},
);
const logger = winston.createLogger({
format: winston.format.combine(
winston.format.colorize(),
winston.format.timestamp(),
logMessageFormat,
),
transports: [new winston.transports.Console()],
});
const turboProcess = childProcess.exec("turbo dev --parallel");
turboProcess.stdout.on("data", (data) => {
if (["warn", "warning"].some((prefix) => data.includes(prefix))) {
logger.warn(data);
} else {
logger.info(data);
}
});
turboProcess.stderr.on("data", (error) => {
logger.error(error);
});