Skip to content

Commit

Permalink
feat: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Link committed Jul 27, 2024
1 parent a283557 commit 3b726e6
Show file tree
Hide file tree
Showing 31 changed files with 800 additions and 1,301 deletions.
8 changes: 4 additions & 4 deletions .c8rc.json
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
}
48 changes: 46 additions & 2 deletions .vscode/settings.json
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"
}
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
"private": true,
"type": "module",
"scripts": {
"build": "tsc --build",
"docgen": "typedoc",
"lint": "eslint **/src/**/*.ts **/test/**/*.ts",
"lintfix": "eslint **/src/**/*.ts **/test/**/*.ts --fix",
"coverage": "c8 mocha **/test/**/*.test.js",
"test": "mocha **/test/**/*.test.js",
"bt": "pnpm run build && pnpm run test",
"bc": "pnpm run build && pnpm run coverage"
"build": "tsc --build",
"docgen": "typedoc",
"lint": "eslint **/src/**/*.ts **/test/**/*.ts",
"lintfix": "eslint **/src/**/*.ts **/test/**/*.ts --fix",
"coverage": "c8 mocha **/test/**/*.test.js",
"test": "mocha **/test/**/*.test.js",
"bt": "pnpm run build && pnpm run test",
"bc": "pnpm run build && pnpm run coverage"
},
"devDependencies": {
"@types/mocha": "^10.0.7",
"c8": "^10.1.2",
"eslint": "^8.57.0",
"eslint-config-love": "^59.0.0",
"mocha": "^10.7.0",
"c8": "^10.1.2",
"typescript": "^5.5.4",
"typedoc": "^0.26.3"
"typedoc": "^0.26.5",
"typescript": "^5.5.4"
}
}

7 changes: 2 additions & 5 deletions packages/logasjson/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
{
"name": "logasjson",
"version": "0.0.1",
"version": "1.0.0",
"description": "JSON Logger with adapter for loki.",
"main": "dist/src/index.js",
"sideEffects": false,
"exports": {
"./classes/*": "./dist/src/classes/*.js",
"./helper/*": "./dist/src/helper/*.js",
"./types/*": "./dist/src/types/*.js",
"./enums/*": "./dist/src/enums/*.js"
".": "./dist/src/index.js"
},
"files": [
"/dist/src",
Expand Down
135 changes: 0 additions & 135 deletions packages/logasjson/src/classes/Context.ts

This file was deleted.

132 changes: 12 additions & 120 deletions packages/logasjson/src/classes/Logger.ts
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?.()
}
}
Loading

0 comments on commit 3b726e6

Please sign in to comment.