-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcel Link
committed
Jul 27, 2024
1 parent
a283557
commit 3b726e6
Showing
31 changed files
with
800 additions
and
1,301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"exclude": ["**/test/**", "**/example/**"], | ||
"check-coverage": true, | ||
"branches": 90, | ||
"lines": 90, | ||
"functions": 90, | ||
"statements": 90 | ||
"branches": 100, | ||
"lines": 100, | ||
"functions": 100, | ||
"statements": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,51 @@ | ||
{ | ||
// Disable the default formatter, use eslint instead | ||
"prettier.enable": false, | ||
"editor.formatOnSave": false, | ||
|
||
// Auto fix | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": "explicit" | ||
"source.fixAll.eslint": "explicit", | ||
"source.organizeImports": "never" | ||
}, | ||
|
||
// Silent the stylistic rules in you IDE, but still auto fix them | ||
"eslint.rules.customizations": [ | ||
{ "rule": "style/*", "severity": "off", "fixable": true }, | ||
{ "rule": "format/*", "severity": "off", "fixable": true }, | ||
{ "rule": "*-indent", "severity": "off", "fixable": true }, | ||
{ "rule": "*-spacing", "severity": "off", "fixable": true }, | ||
{ "rule": "*-spaces", "severity": "off", "fixable": true }, | ||
{ "rule": "*-order", "severity": "off", "fixable": true }, | ||
{ "rule": "*-dangle", "severity": "off", "fixable": true }, | ||
{ "rule": "*-newline", "severity": "off", "fixable": true }, | ||
{ "rule": "*quotes", "severity": "off", "fixable": true }, | ||
{ "rule": "*semi", "severity": "off", "fixable": true } | ||
], | ||
|
||
// Enable eslint for all supported languages | ||
"eslint.validate": [ | ||
"javascript", | ||
"javascriptreact", | ||
"typescript", | ||
"typescriptreact", | ||
"vue", | ||
"html", | ||
"markdown", | ||
"json", | ||
"jsonc", | ||
"yaml", | ||
"toml", | ||
"xml", | ||
"gql", | ||
"graphql", | ||
"astro", | ||
"css", | ||
"less", | ||
"scss", | ||
"pcss", | ||
"postcss" | ||
], | ||
"mochaExplorer.files": "**/dist/test/**/*.test.js", | ||
"typescript.tsdk": "node_modules/typescript/lib", | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,21 @@ | ||
/* eslint-disable @typescript-eslint/max-params */ | ||
import { randomUUID } from 'crypto' | ||
|
||
import { Context } from './Context.js' | ||
import { Override } from './Override.js' | ||
import { LogLevel } from '../enums/loglevel.js' | ||
import { placeholder } from '../helper/placeholder.js' | ||
import type { LoggerEntry, LoggerLog, LoggerOptions } from '../types/logger.js' | ||
|
||
export class Logger { | ||
public static options: LoggerOptions = { | ||
|
||
} | ||
|
||
public static readonly override = new Override() | ||
|
||
public readonly name: string | ||
private readonly options: LoggerOptions | ||
|
||
get logLevel (): LogLevel { | ||
const context = Context.getStore() | ||
return this.contextLogLevel(context) | ||
} | ||
|
||
get traceId (): string { | ||
const context = Context.getStore() | ||
return context?.traceId ?? randomUUID() | ||
} | ||
|
||
constructor (name: string = '', options: LoggerOptions = {}) { | ||
this.name = name | ||
this.options = options | ||
} | ||
|
||
extend (name: string = '', options: LoggerOptions = {}): Logger { | ||
return new Logger(Logger.mergeName(this.name, name), Logger.mergeOptions(this.options, options)) | ||
} | ||
|
||
get debug (): LoggerLog | undefined { | ||
return this.log(LogLevel.Debug) | ||
} | ||
|
||
get info (): LoggerLog | undefined { | ||
return this.log(LogLevel.Info) | ||
} | ||
import type { LoggerInitOptions, LoggerOptions } from '../types/logger.js' | ||
import { LoggerFork } from './LoggerFork.js' | ||
import { DestinationConsole } from './destination/DestinationConsole.js' | ||
|
||
get warn (): LoggerLog | undefined { | ||
return this.log(LogLevel.Warn) | ||
export class Logger extends LoggerFork { | ||
public get override (): Override { | ||
return this.options.override | ||
} | ||
|
||
get error (): LoggerLog | undefined { | ||
return this.log(LogLevel.Error) | ||
public constructor (options: LoggerInitOptions) { | ||
if(options.override === undefined) options.override = new Override() | ||
if(options.name === '') options.name = undefined | ||
if(options.destination === undefined) options.destination = new DestinationConsole() | ||
super(options as LoggerOptions) | ||
} | ||
|
||
public async flush (): Promise<void> { | ||
const context = Context.getStore() | ||
const destination = context?.logger.destination ?? this.options.destination ?? Logger.options.destination | ||
await destination?.flush?.() | ||
} | ||
|
||
private contextLogLevel (context: Context | undefined): number { | ||
const logLevel = Logger.override.getLogLevel(this.name, context?.name) | ||
if (logLevel !== undefined) return logLevel | ||
return context?.logger.logLevel ?? this.options.logLevel ?? Logger.options.logLevel ?? LogLevel.None | ||
} | ||
|
||
private log (logLevel: LogLevel): LoggerLog | undefined { | ||
const context = Context.getStore() | ||
if (logLevel < this.contextLogLevel(context)) return undefined | ||
return (message: string, data?: Record<string, any>, trace?: boolean): void => { | ||
const destination = context?.logger.destination ?? this.options.destination ?? Logger.options.destination | ||
destination?.write(Logger.processDataRaw(context, logLevel, this.name, this.options, message, data, trace)) | ||
} | ||
} | ||
|
||
private static processDataRaw ( | ||
context: Context | undefined, | ||
logLevel: LogLevel, | ||
name: string, | ||
options: LoggerOptions, | ||
message: string, | ||
data?: Record<string, any>, | ||
trace?: boolean): LoggerEntry { | ||
const d: LoggerEntry = Object.assign({}, Logger.options.data, options.data, data, context?.logger.data) | ||
d.logger = name | ||
|
||
if (context !== undefined) { | ||
d.traceId = context.traceId | ||
d.context = context.name | ||
} | ||
|
||
d.logLevel = logLevel | ||
|
||
d.message = placeholder(message, d) | ||
if (trace === true) d.trace = (new Error()).stack | ||
d.timestamp = Date.now() | ||
|
||
return d | ||
} | ||
|
||
public static processData ( | ||
logLevel: LogLevel, | ||
name: string, | ||
options: LoggerOptions, | ||
message: string, | ||
data?: Record<string, any>, | ||
trace?: boolean): LoggerEntry { | ||
return Logger.processDataRaw(Context.getStore(), logLevel, name, options, message, data, trace) | ||
} | ||
|
||
public static mergeOptions ( | ||
opt1: LoggerOptions = {}, | ||
opt2: LoggerOptions = {} | ||
): LoggerOptions { | ||
return { | ||
logLevel: opt2.logLevel ?? opt1.logLevel, | ||
destination: opt2.destination ?? opt1.destination, | ||
data: Object.assign({}, opt1.data, opt2.data) | ||
} | ||
} | ||
|
||
public static async flush (): Promise<void> { | ||
await Logger.options.destination?.flush?.() | ||
} | ||
|
||
public static mergeName (name1: string | undefined, name2: string): string { | ||
return [name1, name2].filter(e => e !== undefined).join(':') | ||
await this.options.destination?.flush?.() | ||
} | ||
} |
Oops, something went wrong.