From c315d841bd1fdbbfd31c8d8a99f99cbd603557f6 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Wed, 11 Jun 2025 12:17:37 +0200 Subject: [PATCH] fix linting --- bun.lock | 3 +- config-schema/package.json | 3 +- config-schema/src/index.ts | 2 +- config-schema/src/log4js-config/appenders.ts | 4 +- .../src/log4js-config/coloredContext.ts | 80 +++++++++++-------- config-schema/src/log4js-config/index.ts | 29 +++---- .../src/log4js-config/types/Category.ts | 1 - .../log4js-config/types/CustomFileAppender.ts | 2 +- yarn.lock | 5 ++ 9 files changed, 76 insertions(+), 53 deletions(-) diff --git a/bun.lock b/bun.lock index 877b643f9..24180713e 100644 --- a/bun.lock +++ b/bun.lock @@ -163,6 +163,7 @@ "joi": "^17.13.3", "source-map-support": "^0.5.21", "yoctocolors-cjs": "^2.1.2", + "zod": "^3.25.61", }, "devDependencies": { "@biomejs/biome": "1.9.4", @@ -3302,7 +3303,7 @@ "zen-observable-ts": ["zen-observable-ts@1.2.5", "", { "dependencies": { "zen-observable": "0.8.15" } }, "sha512-QZWQekv6iB72Naeake9hS1KxHlotfRpe+WGNbNx5/ta+R3DNjVO2bswf63gXlWDcs+EMd7XY8HfVQyP1X6T4Zg=="], - "zod": ["zod@3.25.55", "", {}, "sha512-219huNnkSLQnLsQ3uaRjXsxMrVm5C9W3OOpEVt2k5tvMKuA8nBSu38e0B//a+he9Iq2dvmk2VyYVlHqiHa4YBA=="], + "zod": ["zod@3.25.61", "", {}, "sha512-fzfJgUw78LTNnHujj9re1Ov/JJQkRZZGDMcYqSx7Hp4rPOkKywaFHq0S6GoHeXs0wGNE/sIOutkXgnwzrVOGCQ=="], "@apollo/protobufjs/@types/node": ["@types/node@10.17.60", "", {}, "sha512-F0KIgDJfy2nA3zMLmWGKxcH2ZVEtCZXHHdOQs2gSaQ27+lNeEfGxzkIw90aXswATX7AZ33tahPbzy6KAfUreVw=="], diff --git a/config-schema/package.json b/config-schema/package.json index 1ec4ed1f1..3759d9373 100644 --- a/config-schema/package.json +++ b/config-schema/package.json @@ -30,7 +30,8 @@ "esbuild": "^0.25.2", "joi": "^17.13.3", "source-map-support": "^0.5.21", - "yoctocolors-cjs": "^2.1.2" + "yoctocolors-cjs": "^2.1.2", + "zod": "^3.25.61" }, "engines": { "node": ">=18" diff --git a/config-schema/src/index.ts b/config-schema/src/index.ts index e38fa190f..c2ad9fc96 100644 --- a/config-schema/src/index.ts +++ b/config-schema/src/index.ts @@ -2,4 +2,4 @@ import 'source-map-support/register' export * from './commonSchema' export { DatabaseConfigSchema } from './DatabaseConfigSchema' export { validate } from './validate' -export { createLog4jsConfig, type Category, initLogger, defaultCategory } from './log4js-config' \ No newline at end of file +export { createLog4jsConfig, type Category, initLogger, defaultCategory } from './log4js-config' diff --git a/config-schema/src/log4js-config/appenders.ts b/config-schema/src/log4js-config/appenders.ts index 5c9c93f30..59b42aec3 100644 --- a/config-schema/src/log4js-config/appenders.ts +++ b/config-schema/src/log4js-config/appenders.ts @@ -66,8 +66,8 @@ export function createAppenderConfig( ...fileAppenderTemplate, filename: basePath ? `${basePath}/${filename}` : filename, } - dateFile.layout = { - type: 'coloredContext', + dateFile.layout = { + type: 'coloredContext', withStack: appender.withStack, withFile: appender.withFile, } diff --git a/config-schema/src/log4js-config/coloredContext.ts b/config-schema/src/log4js-config/coloredContext.ts index 18243a3f8..a221c7c9d 100644 --- a/config-schema/src/log4js-config/coloredContext.ts +++ b/config-schema/src/log4js-config/coloredContext.ts @@ -1,5 +1,5 @@ -import { LoggingEvent, Level } from 'log4js' -import colors from 'yoctocolors-cjs' +import { Level, LoggingEvent } from 'log4js' +import colors from 'yoctocolors-cjs' import { LogLevel } from './types' export type coloredContextLayoutConfig = { @@ -7,38 +7,52 @@ export type coloredContextLayoutConfig = { withFile?: LogLevel | boolean } - function colorize(str: string, level: Level): string { - switch(level.colour) { - case 'white': return colors.white(str) - case 'grey': return colors.gray(str) - case 'black': return colors.black(str) - case 'blue': return colors.blue(str) - case 'cyan': return colors.cyan(str) - case 'green': return colors.green(str) - case 'magenta': return colors.magenta(str) - case 'red': return colors.redBright(str) - case 'yellow': return colors.yellow(str) - default: return colors.gray(str) + switch (level.colour) { + case 'white': + return colors.white(str) + case 'grey': + return colors.gray(str) + case 'black': + return colors.black(str) + case 'blue': + return colors.blue(str) + case 'cyan': + return colors.cyan(str) + case 'green': + return colors.green(str) + case 'magenta': + return colors.magenta(str) + case 'red': + return colors.redBright(str) + case 'yellow': + return colors.yellow(str) + default: + return colors.gray(str) } } // distinguish between objects with valid toString function (for examples classes derived from AbstractLoggingView) and other objects function composeDataString(data: (string | Object)[]): string { - return data.map((data) => { - // if it is a object and his toString function return only garbage - if (typeof data === 'object' && data.toString() === '[object Object]') { - return JSON.stringify(data) - } - return data.toString() - }).join(' ') + return data + .map((data) => { + // if it is a object and his toString function return only garbage + if (typeof data === 'object' && data.toString() === '[object Object]') { + return JSON.stringify(data) + } + return data.toString() + }) + .join(' ') } // automatic detect context objects and list them in logfmt style function composeContextString(data: Object): string { - return Object.entries(data).map(([key, value]) => { - return `${key}=${value} ` - }).join(' ').trimEnd() + return Object.entries(data) + .map(([key, value]) => { + return `${key}=${value} ` + }) + .join(' ') + .trimEnd() } // check if option is enabled, either if option is them self a boolean or a valid log level and <= eventLogLevel @@ -55,15 +69,18 @@ function isEnabledByLogLevel(eventLogLevel: Level, targetLogLevel?: LogLevel | b export function createColoredContextLayout(config: coloredContextLayoutConfig) { return (logEvent: LoggingEvent) => { const result: string[] = [] - result.push(colorize( - `[${logEvent.startTime.toISOString()}] [${logEvent.level}] ${logEvent.categoryName} -`, - logEvent.level - )) + result.push( + colorize( + `[${logEvent.startTime.toISOString()}] [${logEvent.level}] ${logEvent.categoryName} -`, + logEvent.level, + ), + ) if (Object.keys(logEvent.context).length > 0) { result.push(composeContextString(logEvent.context)) - } + } result.push(composeDataString(logEvent.data)) - const showCallstack = logEvent.callStack && isEnabledByLogLevel(logEvent.level, config.withStack) + const showCallstack = + logEvent.callStack && isEnabledByLogLevel(logEvent.level, config.withStack) if (!showCallstack && isEnabledByLogLevel(logEvent.level, config.withFile)) { result.push(`\n at ${logEvent.fileName}:${logEvent.lineNumber}`) } @@ -71,6 +88,5 @@ export function createColoredContextLayout(config: coloredContextLayoutConfig) { result.push(`\n${logEvent.callStack}`) } return result.join(' ') - } + } } - \ No newline at end of file diff --git a/config-schema/src/log4js-config/index.ts b/config-schema/src/log4js-config/index.ts index aef4d6099..9652726ce 100644 --- a/config-schema/src/log4js-config/index.ts +++ b/config-schema/src/log4js-config/index.ts @@ -1,8 +1,8 @@ import { readFileSync, writeFileSync } from 'node:fs' -import { addLayout, Configuration, LoggingEvent, configure } from 'log4js' +import { Configuration, LoggingEvent, addLayout, configure } from 'log4js' import { createAppenderConfig } from './appenders' -import { Category, CustomFileAppender, LogLevel, defaultCategory } from './types' import { createColoredContextLayout } from './coloredContext' +import { Category, CustomFileAppender, LogLevel, defaultCategory } from './types' export { Category, LogLevel, defaultCategory } @@ -14,13 +14,13 @@ export { Category, LogLevel, defaultCategory } * @returns {Configuration} the log4js configuration */ -addLayout("json", function() { +addLayout('json', function () { return function (logEvent: LoggingEvent) { return JSON.stringify(logEvent) } }) -addLayout("coloredContext", createColoredContextLayout) +addLayout('coloredContext', createColoredContextLayout) export function createLog4jsConfig(categories: Category[], basePath?: string): Configuration { const customFileAppenders: CustomFileAppender[] = [] @@ -37,12 +37,9 @@ export function createLog4jsConfig(categories: Category[], basePath?: string): C withStack: 'error', }) // needed by log4js, show all error message accidentally without (proper) Category - result.categories['default'] = { + result.categories.default = { level: 'debug', - appenders: [ - 'out', - 'errors', - ], + appenders: ['out', 'errors'], enableCallStack: true, } const appenders = [category.name, 'out'] @@ -67,13 +64,17 @@ export function createLog4jsConfig(categories: Category[], basePath?: string): C * @param {string} logFilesPath - the base path for log files * @param {string} [log4jsConfigFileName] - the name of the log4js config file */ -export function initLogger(categories: Category[], logFilesPath: string, log4jsConfigFileName: string = 'log4js-config.json'): void { - // if not log4js config file exists, create a default one - try { +export function initLogger( + categories: Category[], + logFilesPath: string, + log4jsConfigFileName: string = 'log4js-config.json', +): void { + // if not log4js config file exists, create a default one + try { configure(JSON.parse(readFileSync(log4jsConfigFileName, 'utf-8'))) - } catch(_e) { + } catch (_e) { const options = createLog4jsConfig(categories, logFilesPath) - writeFileSync(log4jsConfigFileName, JSON.stringify(options, null, 2), {encoding: 'utf-8'}) + writeFileSync(log4jsConfigFileName, JSON.stringify(options, null, 2), { encoding: 'utf-8' }) configure(options) } } diff --git a/config-schema/src/log4js-config/types/Category.ts b/config-schema/src/log4js-config/types/Category.ts index 3c00c5077..889401b30 100644 --- a/config-schema/src/log4js-config/types/Category.ts +++ b/config-schema/src/log4js-config/types/Category.ts @@ -25,4 +25,3 @@ export function defaultCategory(name: string, level: LogLevel): Category { additionalErrorsFile: true, } } - \ No newline at end of file diff --git a/config-schema/src/log4js-config/types/CustomFileAppender.ts b/config-schema/src/log4js-config/types/CustomFileAppender.ts index 5aafc13b0..f2281805e 100644 --- a/config-schema/src/log4js-config/types/CustomFileAppender.ts +++ b/config-schema/src/log4js-config/types/CustomFileAppender.ts @@ -15,7 +15,7 @@ import { LogLevel } from './LogLevel' * { name: 'warn', filename: 'warn.log', withStack: true }, * ]) * ``` - * + * * @example if log file should contain the stacktrace only from log level debug and higher * ``` * const appenderConfig = createAppenderConfig([ diff --git a/yarn.lock b/yarn.lock index 08ce15dbe..235de4b00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12694,3 +12694,8 @@ zen-observable@0.8.15, zen-observable@^0.8.0: version "0.8.15" resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.15.tgz#96415c512d8e3ffd920afd3889604e30b9eaac15" integrity sha512-PQ2PC7R9rslx84ndNBZB/Dkv8V8fZEpk83RLgXtYd0fwUgEjseMn1Dgajh2x6S8QbZAFa9p2qVCEuYZNgve0dQ== + +zod@^3.25.61: + version "3.25.61" + resolved "https://registry.yarnpkg.com/zod/-/zod-3.25.61.tgz#2e09ece796c182d44b7defc8efb27aa792eb4c8b" + integrity sha512-fzfJgUw78LTNnHujj9re1Ov/JJQkRZZGDMcYqSx7Hp4rPOkKywaFHq0S6GoHeXs0wGNE/sIOutkXgnwzrVOGCQ==