@xylabs/logger
Advanced tools
+7
-10
| { | ||
| "name": "@xylabs/logger", | ||
| "version": "5.0.80", | ||
| "version": "5.0.81", | ||
| "description": "XYLabs Logger Library", | ||
@@ -32,3 +32,2 @@ "keywords": [ | ||
| "types": "./dist/neutral/index.d.ts", | ||
| "source": "./src/index.ts", | ||
| "default": "./dist/neutral/index.mjs" | ||
@@ -39,7 +38,5 @@ }, | ||
| "module": "./dist/neutral/index.mjs", | ||
| "source": "./src/index.ts", | ||
| "types": "./dist/neutral/index.d.ts", | ||
| "files": [ | ||
| "dist", | ||
| "src", | ||
| "!**/*.bench.*", | ||
@@ -50,10 +47,10 @@ "!**/*.spec.*", | ||
| "dependencies": { | ||
| "@xylabs/enum": "~5.0.80", | ||
| "@xylabs/error": "~5.0.80", | ||
| "@xylabs/typeof": "~5.0.80" | ||
| "@xylabs/enum": "~5.0.81", | ||
| "@xylabs/error": "~5.0.81", | ||
| "@xylabs/typeof": "~5.0.81" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "~25.2.3", | ||
| "@xylabs/ts-scripts-yarn3": "~7.3.2", | ||
| "@xylabs/tsconfig": "~7.3.2", | ||
| "@types/node": "~25.4.0", | ||
| "@xylabs/ts-scripts-yarn3": "~7.4.11", | ||
| "@xylabs/tsconfig": "~7.4.11", | ||
| "typescript": "~5.9.3", | ||
@@ -60,0 +57,0 @@ "vitest": "~4.0.18" |
+6
-23
@@ -38,3 +38,2 @@ # @xylabs/logger | ||
| - [LogFunction](#type-aliases/LogFunction) | ||
| - [~~LogLevel~~](#type-aliases/LogLevel) | ||
| - [LogLevelKey](#type-aliases/LogLevelKey) | ||
@@ -73,3 +72,3 @@ - [LogVerbosity](#type-aliases/LogVerbosity) | ||
| ```ts | ||
| new ConsoleLogger(level): ConsoleLogger; | ||
| new ConsoleLogger(level?): ConsoleLogger; | ||
| ``` | ||
@@ -79,3 +78,3 @@ | ||
| #### level | ||
| #### level? | ||
@@ -445,3 +444,3 @@ [`LogLevelValue`](#../type-aliases/LogLevelValue) = `LogLevel.warn` | ||
| ```ts | ||
| new LevelLogger(logger, level): LevelLogger; | ||
| new LevelLogger(logger, level?): LevelLogger; | ||
| ``` | ||
@@ -455,3 +454,3 @@ | ||
| #### level | ||
| #### level? | ||
@@ -777,3 +776,3 @@ [`LogLevelValue`](#../type-aliases/LogLevelValue) = `LogLevel.warn` | ||
| ```ts | ||
| function getFunctionName(depth): string; | ||
| function getFunctionName(depth?): string; | ||
| ``` | ||
@@ -783,3 +782,3 @@ | ||
| ### depth | ||
| ### depth? | ||
@@ -873,18 +872,2 @@ `number` = `2` | ||
| ### <a id="LogLevel"></a>LogLevel | ||
| [**@xylabs/logger**](#../README) | ||
| *** | ||
| ```ts | ||
| type LogLevel = LogLevelValue; | ||
| ``` | ||
| ## Deprecated | ||
| Use `LogLevelValue` instead. | ||
| This name conflicts with the `LogLevel` enum and | ||
| makes it confusing to import | ||
| ### <a id="LogLevelKey"></a>LogLevelKey | ||
@@ -891,0 +874,0 @@ |
| import type { LogLevelValue } from './LevelLogger.ts' | ||
| import { LevelLogger, LogLevel } from './LevelLogger.ts' | ||
| export class ConsoleLogger extends LevelLogger { | ||
| constructor(level: LogLevelValue = LogLevel.warn) { | ||
| super(console, level) | ||
| } | ||
| } |
| import { handleError } from '@xylabs/error' | ||
| import { isNumber } from '@xylabs/typeof' | ||
| export const getFunctionName = (depth = 2) => { | ||
| try { | ||
| throw new Error('Getting function name') | ||
| } catch (ex) { | ||
| return handleError(ex, (error) => { | ||
| let newIndex: number | undefined | ||
| const stackParts = error.stack?.split('\n')[depth]?.split(' ') | ||
| const funcName | ||
| = stackParts?.find((item, index) => { | ||
| if (item.length > 0 && item !== 'at') { | ||
| // check if constructor | ||
| if (item === 'new') { | ||
| newIndex = index | ||
| } | ||
| return true | ||
| } | ||
| }) ?? '<unknown>' | ||
| return isNumber(newIndex) ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName | ||
| }) | ||
| } | ||
| } |
| import type { Logger } from './LevelLogger.ts' | ||
| export class IdLogger implements Logger { | ||
| private _id?: () => string | ||
| private _logger: Logger | ||
| constructor(logger: Logger, id?: () => string) { | ||
| this._logger = logger | ||
| this._id = id | ||
| } | ||
| set id(id: string) { | ||
| this._id = () => id | ||
| } | ||
| debug(...data: unknown[]) { | ||
| this._logger?.debug(this.prefix(), ...data) | ||
| } | ||
| error(...data: unknown[]) { | ||
| this._logger?.error(this.prefix(), ...data) | ||
| } | ||
| info(...data: unknown[]) { | ||
| this._logger?.info(this.prefix(), ...data) | ||
| } | ||
| log(...data: unknown[]) { | ||
| this._logger?.log(this.prefix(), ...data) | ||
| } | ||
| trace(...data: unknown[]) { | ||
| this._logger?.trace(this.prefix(), ...data) | ||
| } | ||
| warn(...data: unknown[]) { | ||
| this._logger?.warn(this.prefix(), ...data) | ||
| } | ||
| private prefix() { | ||
| return `[${this._id?.()}]` | ||
| } | ||
| } |
| export * from './ConsoleLogger.ts' | ||
| export * from './getFunctionName.ts' | ||
| export * from './IdLogger.ts' | ||
| export * from './LevelLogger.ts' | ||
| export * from './NoOpLogFunction.ts' | ||
| export * from './SilentLogger.ts' |
| import type { EnumKey, EnumValue } from '@xylabs/enum' | ||
| import { Enum } from '@xylabs/enum' | ||
| import { NoOpLogFunction } from './NoOpLogFunction.ts' | ||
| export type LogFunction = (...data: unknown[]) => void | ||
| /** | ||
| * Interface to handle overlap between Winston & | ||
| * `console` with as much congruency as possible. | ||
| */ | ||
| export interface Logger { | ||
| debug: LogFunction | ||
| error: LogFunction | ||
| info: LogFunction | ||
| log: LogFunction | ||
| trace: LogFunction | ||
| warn: LogFunction | ||
| } | ||
| export const LogLevel = Enum({ | ||
| error: 1, | ||
| warn: 2, | ||
| info: 3, | ||
| log: 4, | ||
| debug: 5, | ||
| trace: 6, | ||
| }) | ||
| export type LogLevelKey = EnumKey<typeof LogLevel> | ||
| export type LogVerbosity = LogLevelKey | ||
| export type LogLevelValue = EnumValue<typeof LogLevel> | ||
| export class LevelLogger implements Logger { | ||
| readonly level: LogLevelValue | ||
| readonly logger: Logger | ||
| constructor(logger: Logger, level: LogLevelValue = LogLevel.warn) { | ||
| this.level = level | ||
| this.logger = logger | ||
| } | ||
| get debug() { | ||
| return this.level >= LogLevel.debug ? this.logger.debug : NoOpLogFunction | ||
| } | ||
| get error() { | ||
| return this.level >= LogLevel.error ? this.logger.error : NoOpLogFunction | ||
| } | ||
| get info() { | ||
| return this.level >= LogLevel.info ? this.logger.info : NoOpLogFunction | ||
| } | ||
| get log() { | ||
| return this.level >= LogLevel.log ? this.logger.log : NoOpLogFunction | ||
| } | ||
| get trace() { | ||
| return this.level >= LogLevel.trace ? this.logger.trace : NoOpLogFunction | ||
| } | ||
| get warn() { | ||
| return this.level >= LogLevel.warn ? this.logger.warn : NoOpLogFunction | ||
| } | ||
| } |
| export const NoOpLogFunction = (..._data: unknown[]) => void {} |
| import type { Logger } from './LevelLogger.ts' | ||
| import { NoOpLogFunction } from './NoOpLogFunction.ts' | ||
| /** | ||
| * A logger that does not log anything. | ||
| * This is useful when you want to disable logging | ||
| * like when running unit tests or in silent mode. | ||
| * It implements the `Logger` interface but all methods | ||
| * are no-op functions. | ||
| */ | ||
| export class SilentLogger implements Logger { | ||
| readonly debug = NoOpLogFunction | ||
| readonly error = NoOpLogFunction | ||
| readonly info = NoOpLogFunction | ||
| readonly log = NoOpLogFunction | ||
| readonly trace = NoOpLogFunction | ||
| readonly warn = NoOpLogFunction | ||
| } |
36491
-11.13%19
-26.92%198
-41.42%952
-1.75%Updated
Updated
Updated