Skip to content

Commit

Permalink
fixed eslint problems + compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
rstiller committed Jul 1, 2023
1 parent c935002 commit 8f57681
Show file tree
Hide file tree
Showing 30 changed files with 160 additions and 155 deletions.
3 changes: 1 addition & 2 deletions examples/typescript-log4js/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
LoggingEvent,
configure,
getLogger
} from 'log4js'
Expand Down Expand Up @@ -51,7 +50,7 @@ slf4tsLogger.error('SLF4TS ERROR log message with error', new Error())
.catch(console.log)

slf4tsLogger.setMetadata({
user: (logEntry: LoggingEvent) => 'computed-value'
user: () => 'computed-value'
})
slf4tsLogger.trace('SLF4TS TRACE log message with number', 123)
.catch(console.log)
Expand Down
36 changes: 18 additions & 18 deletions packages/slf4ts-api/lib/slf4ts/LoggerBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'source-map-support/register'
import * as fs from 'fs'
import * as path from 'path'

import { LogLevel } from './LoggerConfiguration'
import { type LogLevel } from './LoggerConfiguration'

/**
* Builds the instance of the underlying logger framework.
Expand All @@ -25,7 +25,7 @@ export interface LoggerImplementation<T, P extends any[]> {
* @returns A Promise completed when the log statement was processed by the underlying logging-framework.
* @memberof LoggerImplementation
*/
log(...args: any[]): Promise<any>
log: (...args: any[]) => Promise<any>

/**
* Gets the underlying implementation of the logger.
Expand All @@ -35,7 +35,7 @@ export interface LoggerImplementation<T, P extends any[]> {
* @param {string} name The name of the logger.
* @memberof LoggerImplementation
*/
getImplementation(group: string, name: string): T
getImplementation: (group: string, name: string) => T

/**
* Sets the configuration for the specified logger instance.
Expand All @@ -46,7 +46,7 @@ export interface LoggerImplementation<T, P extends any[]> {
* @param {string} name The name of the logger.
* @memberof LoggerImplementation
*/
setConfig<T>(config: T, group: string, name: string): void
setConfig: <T>(config: T, group: string, name: string) => void

/**
* Informs the logger implementation of the log-level change.
Expand All @@ -56,7 +56,7 @@ export interface LoggerImplementation<T, P extends any[]> {
* @param {string} name The name of the logger.
* @memberof LoggerImplementation
*/
setLogLevel(logLevel: LogLevel, group: string, name: string): void
setLogLevel: (logLevel: LogLevel, group: string, name: string) => void

/**
* Informs the logger implementation of the metadata change.
Expand All @@ -66,15 +66,15 @@ export interface LoggerImplementation<T, P extends any[]> {
* @param {string} name The name of the logger.
* @memberof LoggerImplementation
*/
setMetadata(metadata: any, group: string, name: string): void
setMetadata: (metadata: any, group: string, name: string) => void

/**
* Sets the logger builder instance
*
* @param {LoggerBuilder<T, P>} builder
* @memberof LoggerImplementation
*/
setLoggerBuilder(builder: LoggerBuilder<T, P>): void
setLoggerBuilder: (builder: LoggerBuilder<T, P>) => void

}

Expand All @@ -91,21 +91,21 @@ export interface LoggerBinding<T = any, P extends any[] = any> {
* @returns {LoggerImplementation} The logger implementation.
* @memberof LoggerBinding
*/
getLoggerImplementation(): LoggerImplementation<T, P>
getLoggerImplementation: () => LoggerImplementation<T, P>
/**
* Gets the vendor string.
*
* @returns {string} The vendor name.
* @memberof LoggerBinding
*/
getVendor(): string
getVendor: () => string
/**
* Gets the version string.
*
* @returns {string} The version number.
* @memberof LoggerBinding
*/
getVersion(): string
getVersion: () => string
}

/**
Expand All @@ -130,7 +130,7 @@ export class LoggerBindings {
* @type {LoggerBinding[]}
* @memberof LoggerBindings
*/
private readonly bindings: LoggerBinding[] = [];
private readonly bindings: LoggerBinding[] = []

/**
* Creates an instance of LoggerBindings.
Expand All @@ -140,18 +140,19 @@ export class LoggerBindings {
*/
public constructor (additionalPaths: string[] = []) {
if ('LOGGER_BINDING_ADDITIONAL_PATH' in process.env) {
additionalPaths.push(process.env.LOGGER_BINDING_ADDITIONAL_PATH)
additionalPaths.push(process.env.LOGGER_BINDING_ADDITIONAL_PATH ?? '')
}
if ('mainModule' in process && 'paths' in process.mainModule) {
process.mainModule.paths
const mainModule = require.main ?? { paths: [], require: () => {} }
if ('mainModule' in process && 'paths' in mainModule) {
mainModule.paths
.forEach((mainPath) => additionalPaths.push(mainPath))
}

const moduleFolders: string[] = this.getAllModuleFolders(additionalPaths)
const loggerBindings: string[] = this.getAllLoggerBindings(moduleFolders)

loggerBindings.forEach((binding) => {
const registerFunc = require.main.require(binding)
const registerFunc = mainModule.require(binding)
registerFunc(this)
})
}
Expand Down Expand Up @@ -184,15 +185,14 @@ export class LoggerBindings {
* @memberof LoggerBindings
*/
public getBindings (): LoggerBinding[] {
return [].concat(this.bindings)
return this.bindings.slice()
}

private getAllModuleFolders (additionalPaths: string[]): string[] {
const rootPaths: string[] = (module as any).paths
const moduleFolders: string[] = []

rootPaths.concat(additionalPaths).forEach((rootPath) =>
this.visitNodeModules(rootPath).forEach((folder) => moduleFolders.push(folder)))
rootPaths.concat(additionalPaths).forEach((rootPath) => { this.visitNodeModules(rootPath).forEach((folder) => moduleFolders.push(folder)) })

return moduleFolders
}
Expand Down
10 changes: 5 additions & 5 deletions packages/slf4ts-api/lib/slf4ts/LoggerConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export enum LogLevel {
* @class LoggerConfigurationImpl
*/
export class LoggerConfigurationImpl {
private readonly events: EventEmitter = new EventEmitter();
private readonly logLevelMapping: Map<string, LogLevel> = new Map();
private readonly configMapping: Map<string, any> = new Map();
private defaultLogLevel: LogLevel = LogLevel.INFO;
private readonly events: EventEmitter = new EventEmitter()
private readonly logLevelMapping = new Map<string, LogLevel>()
private readonly configMapping = new Map<string, any>()
private defaultLogLevel: LogLevel = LogLevel.INFO

/**
* Gets the log-level for the given group and name.
Expand All @@ -42,7 +42,7 @@ export class LoggerConfigurationImpl {
const compoundKey = `${group}:${name}`

if (this.logLevelMapping.has(compoundKey)) {
return this.logLevelMapping.get(compoundKey)
return this.logLevelMapping.get(compoundKey) ?? this.defaultLogLevel
}

return this.defaultLogLevel
Expand Down
47 changes: 24 additions & 23 deletions packages/slf4ts-api/lib/slf4ts/LoggerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'source-map-support/register'

import { LoggerBindings, LoggerImplementation, LoggerBuilder } from './LoggerBindings'
import { LoggerBindings, type LoggerImplementation, type LoggerBuilder } from './LoggerBindings'
import { LoggerConfiguration, LogLevel } from './LoggerConfiguration'

/**
Expand All @@ -17,59 +17,59 @@ export interface ILoggerInstance<T> {
* @returns {Promise<any>} A promise completing when the logging-implementation processed the log statement.
* @memberof ILoggerInstance
*/
trace(...args: any[]): Promise<any>
trace: (...args: any[]) => Promise<any>
/**
* Logs the given message using DEBUG log-level.
*
* @param {...args: any[]} [args] messages, metadata and errors to log.
* @returns {Promise<any>} A promise completing when the logging-implementation processed the log statement.
* @memberof ILoggerInstance
*/
debug(...args: any[]): Promise<any>
debug: (...args: any[]) => Promise<any>
/**
* Logs the given message using INFO log-level.
*
* @param {...args: any[]} [args] messages, metadata and errors to log.
* @returns {Promise<any>} A promise completing when the logging-implementation processed the log statement.
* @memberof ILoggerInstance
*/
info(...args: any[]): Promise<any>
info: (...args: any[]) => Promise<any>
/**
* Logs the given message using WARN log-level.
*
* @param {...args: any[]} [args] messages, metadata and errors to log.
* @returns {Promise<any>} A promise completing when the logging-implementation processed the log statement.
* @memberof ILoggerInstance
*/
warn(...args: any[]): Promise<any>
warn: (...args: any[]) => Promise<any>
/**
* Logs the given message using ERROR log-level.
*
* @param {...args: any[]} [args] messages, metadata and errors to log.
* @returns {Promise<any>} A promise completing when the logging-implementation processed the log statement.
* @memberof ILoggerInstance
*/
error(...args: any[]): Promise<any>
error: (...args: any[]) => Promise<any>
/**
* Gets the current log-level.
*
* @returns {LogLevel} The log-level.
* @memberof ILoggerInstance
*/
getLogLevel(): LogLevel
getLogLevel: () => LogLevel
/**
* Sets the metadata assigned to every future invocation of any of the log-methods.
*
* @param {*} metadata metadata object - can be undefined or null.
* @memberof ILoggerInstance
*/
setMetadata(metadata: any): void
setMetadata: (metadata: any) => void
/**
* Gets the underlying implementation of the logger.
*
* @memberof ILoggerInstance
*/
getImplementation(): T
getImplementation: () => T
}

/**
Expand All @@ -80,11 +80,11 @@ export interface ILoggerInstance<T> {
* @implements {ILoggerInstance}
*/
export class DefaultLoggerInstance<T, P extends any[]> implements ILoggerInstance<T> {
private readonly impl: LoggerImplementation<T, P>;
private readonly name: string;
private readonly group: string;
private commonMetadata: any;
private logLevel: LogLevel;
private readonly impl: LoggerImplementation<T, P>
private readonly name: string
private readonly group: string
private commonMetadata: any
private logLevel: LogLevel

/**
* Creates an instance of DefaultLoggerInstance.
Expand Down Expand Up @@ -183,7 +183,7 @@ export class DefaultLoggerInstance<T, P extends any[]> implements ILoggerInstanc
if (logLevel <= this.logLevel) {
return this.impl.log.apply(this.impl, arguments)
}
return Promise.resolve()
await Promise.resolve()
}
}

Expand All @@ -193,7 +193,7 @@ export class DefaultLoggerInstance<T, P extends any[]> implements ILoggerInstanc
* @class NullLoggerImplementation
* @implements {LoggerImplementation}
*/
class NullLoggerImplementation implements LoggerImplementation<null, null> {
class NullLoggerImplementation implements LoggerImplementation<null, any[]> {
public async log (...args: any[]): Promise<any> {
return null
}
Expand All @@ -214,7 +214,7 @@ class NullLoggerImplementation implements LoggerImplementation<null, null> {
// nothing
}

public setLoggerBuilder (builder: LoggerBuilder<null, null>): void {
public setLoggerBuilder (builder: LoggerBuilder<null, any[]>): void {
// nothing
}
}
Expand Down Expand Up @@ -250,13 +250,14 @@ export class LoggerFactory {
return LoggerFactory.LOGGER_INSTANCE_CACHE.get(compoundKey) as ILoggerInstance<T>
}

const defaultLoggerBuilder: LoggerBuilder<T, P> = () => null as T
const instance = new DefaultLoggerInstance<T, P>(
name,
group,
LoggerConfiguration.getLogLevel(group, name),
LoggerFactory.COMMON_METADATA,
LoggerFactory.LOGGER as LoggerImplementation<T, P>,
builder)
builder ?? defaultLoggerBuilder)
LoggerFactory.LOGGER_INSTANCE_CACHE.set(compoundKey, instance)
return instance
}
Expand Down Expand Up @@ -310,11 +311,11 @@ export class LoggerFactory {
})
}

private static COMMON_METADATA: any = undefined;
private static LOGGER: LoggerImplementation<unknown, unknown[]> = new NullLoggerImplementation();
private static ROOT_LOGGER: DefaultLoggerInstance<unknown, unknown[]>;
private static INITIALIZED: boolean = false;
private static readonly LOGGER_INSTANCE_CACHE: Map<string, DefaultLoggerInstance<unknown, unknown[]>> = new Map();
private static COMMON_METADATA: any = undefined
private static LOGGER: LoggerImplementation<unknown, unknown[]> = new NullLoggerImplementation()
private static ROOT_LOGGER: DefaultLoggerInstance<unknown, unknown[]>
private static INITIALIZED: boolean = false
private static readonly LOGGER_INSTANCE_CACHE = new Map<string, DefaultLoggerInstance<unknown, unknown[]>>()

private static initialize<T, P extends any[]>(): void {
const BINDINGS = new LoggerBindings().getBindings()
Expand Down
2 changes: 1 addition & 1 deletion packages/slf4ts-api/test/slf4ts/LoggerBindingTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const expect = chai.expect
// hack to re-initialize the path for module resolution
const nodeModuleExtraPath = path.join(__dirname, '..', '..', 'example-node-modules')
process.env.NODE_PATH = nodeModuleExtraPath
require('module').Module._initPaths()
require('module').Module._initPaths() // eslint-disable-line @typescript-eslint/no-var-requires

@suite
export class LoggerBindingsTest {
Expand Down
4 changes: 2 additions & 2 deletions packages/slf4ts-api/test/slf4ts/LoggerFactoryTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { suite, test } from '@testdeck/mocha'
import * as path from 'path'

import { LoggerConfiguration, LogLevel } from '../../lib/slf4ts/LoggerConfiguration'
import { DefaultLoggerInstance, LoggerFactory } from '../../lib/slf4ts/LoggerFactory'
import { type DefaultLoggerInstance, LoggerFactory } from '../../lib/slf4ts/LoggerFactory'

const expect = chai.expect
const fail = chai.assert.fail
Expand All @@ -17,7 +17,7 @@ const fail = chai.assert.fail
const nodeModuleExtraPath = path.join(__dirname, '..', '..', 'example-node-modules')
process.env.NODE_PATH = nodeModuleExtraPath
process.env.LOGGER_BINDING_ADDITIONAL_PATH = nodeModuleExtraPath
require('module').Module._initPaths()
require('module').Module._initPaths() // eslint-disable-line @typescript-eslint/no-var-requires

@suite
export class LoggerFactoryTest {
Expand Down
6 changes: 3 additions & 3 deletions packages/slf4ts-bunyan/lib/slf4ts/BunyanLoggerBinding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'source-map-support/register'

import * as fs from 'fs'
import * as path from 'path'
import * as Logger from 'bunyan'
import { LoggerBinding } from 'slf4ts-api'
import type * as Logger from 'bunyan'
import { type LoggerBinding } from 'slf4ts-api'

import { BunyanLoggerImplementation } from './BunyanLoggerImplementation'

Expand All @@ -15,7 +15,7 @@ import { BunyanLoggerImplementation } from './BunyanLoggerImplementation'
* @implements {LoggerBinding}
*/
export class BunyanLoggerBinding implements LoggerBinding<Logger, [Logger.LoggerOptions]> {
private readonly packageJson: any;
private readonly packageJson: any

public constructor () {
const modulePath = path.parse(module.filename)
Expand Down
Loading

0 comments on commit 8f57681

Please sign in to comment.