fix linting

This commit is contained in:
einhornimmond 2025-06-11 12:17:37 +02:00
parent 5a8376cccf
commit c315d841bd
9 changed files with 76 additions and 53 deletions

View File

@ -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=="],

View File

@ -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"

View File

@ -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'
export { createLog4jsConfig, type Category, initLogger, defaultCategory } from './log4js-config'

View File

@ -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,
}

View File

@ -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(' ')
}
}
}

View File

@ -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)
}
}

View File

@ -25,4 +25,3 @@ export function defaultCategory(name: string, level: LogLevel): Category {
additionalErrorsFile: true,
}
}

View File

@ -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([

View File

@ -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==