+1
-1
| export { Logger, type LogEntry, type Attributes, type LoggerOptions, type Transport, } from "./logger.js"; | ||
| export { LogLevel, disableLogging } from "./level.js"; | ||
| export { LogLevel, disableLogging, DISABLE_LOGGING } from "./level.js"; | ||
| export { isError, type StackFrame, type FrameLocation, type OtherLocation, type FileLocation, parseStack, } from "./error.js"; | ||
| export { NdJsonTransport } from "./transport/ndjson.js"; | ||
| export { PrettyTransport } from "./transport/pretty.js"; |
+1
-1
| export { Logger, } from "./logger.js"; | ||
| export { LogLevel, disableLogging } from "./level.js"; | ||
| export { LogLevel, disableLogging, DISABLE_LOGGING } from "./level.js"; | ||
| export { isError, parseStack, } from "./error.js"; | ||
| export { NdJsonTransport } from "./transport/ndjson.js"; | ||
| export { PrettyTransport } from "./transport/pretty.js"; |
+18
-7
@@ -1,9 +0,20 @@ | ||
| export declare enum LogLevel { | ||
| Fatal = 60, | ||
| Error = 50, | ||
| Warn = 40, | ||
| Info = 30, | ||
| Debug = 20, | ||
| Trace = 10 | ||
| export declare const DISABLE_LOGGING = 999; | ||
| export declare class LogLevel { | ||
| static Fatal: LogLevel; | ||
| static Error: LogLevel; | ||
| static Warn: LogLevel; | ||
| static Info: LogLevel; | ||
| static Debug: LogLevel; | ||
| static Trace: LogLevel; | ||
| readonly name: LogLevelName; | ||
| readonly level: number; | ||
| private constructor(); | ||
| static fromName(name: string): LogLevel; | ||
| toString(): LogLevelName; | ||
| static names(): readonly LogLevelName[]; | ||
| } | ||
| export type LogLevelName = "fatal" | "error" | "warn" | "info" | "debug" | "trace"; | ||
| /** | ||
| * @deprecated Use `DISABLE_LOGGING` instead. | ||
| */ | ||
| export declare const disableLogging = 999; |
+41
-9
@@ -1,10 +0,42 @@ | ||
| export var LogLevel; | ||
| (function (LogLevel) { | ||
| LogLevel[LogLevel["Fatal"] = 60] = "Fatal"; | ||
| LogLevel[LogLevel["Error"] = 50] = "Error"; | ||
| LogLevel[LogLevel["Warn"] = 40] = "Warn"; | ||
| LogLevel[LogLevel["Info"] = 30] = "Info"; | ||
| LogLevel[LogLevel["Debug"] = 20] = "Debug"; | ||
| LogLevel[LogLevel["Trace"] = 10] = "Trace"; | ||
| })(LogLevel || (LogLevel = {})); | ||
| export const DISABLE_LOGGING = 999; | ||
| export class LogLevel { | ||
| static Fatal = new LogLevel("fatal", 60); | ||
| static Error = new LogLevel("error", 50); | ||
| static Warn = new LogLevel("warn", 40); | ||
| static Info = new LogLevel("info", 30); | ||
| static Debug = new LogLevel("debug", 20); | ||
| static Trace = new LogLevel("trace", 10); | ||
| name; | ||
| level; | ||
| constructor(name, level) { | ||
| this.name = name; | ||
| this.level = level; | ||
| } | ||
| static fromName(name) { | ||
| switch (name) { | ||
| case "fatal": | ||
| return LogLevel.Fatal; | ||
| case "error": | ||
| return LogLevel.Error; | ||
| case "warn": | ||
| return LogLevel.Warn; | ||
| case "info": | ||
| return LogLevel.Info; | ||
| case "debug": | ||
| return LogLevel.Debug; | ||
| case "trace": | ||
| return LogLevel.Trace; | ||
| } | ||
| throw new Error(`Unknown log level: ${name}`); | ||
| } | ||
| toString() { | ||
| return this.name; | ||
| } | ||
| static names() { | ||
| return ["fatal", "error", "warn", "info", "debug", "trace"]; | ||
| } | ||
| } | ||
| /** | ||
| * @deprecated Use `DISABLE_LOGGING` instead. | ||
| */ | ||
| export const disableLogging = 999; |
+2
-1
@@ -23,3 +23,3 @@ import { LogLevel } from "./level.js"; | ||
| constructor(options?: LoggerOptions); | ||
| log(level: LogLevel, message: string, attributes?: Attributes): void; | ||
| log(logLevel: LogLevel, message: string, attributes?: Attributes): void; | ||
| fatal(message: string, attributes?: Attributes): void; | ||
@@ -30,3 +30,4 @@ error(message: string, attributes?: Attributes): void; | ||
| debug(message: string, attributes?: Attributes): void; | ||
| trace(message: string, attributes?: Attributes): void; | ||
| withContext<T>(attributes: Attributes, next: () => Promise<T> | T): Promise<T>; | ||
| } |
+12
-4
@@ -11,8 +11,13 @@ import { AsyncLocalStorage } from "node:async_hooks"; | ||
| this.transport = options?.transport ?? new NdJsonTransport(); | ||
| this.minLevel = options?.minLevel ?? 0; | ||
| this.minLevel = | ||
| options?.minLevel !== undefined | ||
| ? typeof options.minLevel === "number" | ||
| ? options.minLevel | ||
| : options.minLevel.level | ||
| : 0; | ||
| this.attributes = options?.attributes ?? {}; | ||
| this.storage = new AsyncLocalStorage(); | ||
| } | ||
| log(level, message, attributes) { | ||
| if (level < this.minLevel) { | ||
| log(logLevel, message, attributes) { | ||
| if (logLevel.level < this.minLevel) { | ||
| return; | ||
@@ -23,3 +28,3 @@ } | ||
| time: new Date(), | ||
| level, | ||
| level: logLevel, | ||
| message, | ||
@@ -45,2 +50,5 @@ attributes: attributes ? { ...defaultAttributes, ...attributes } : defaultAttributes, | ||
| } | ||
| trace(message, attributes) { | ||
| this.log(LogLevel.Trace, message, attributes); | ||
| } | ||
| async withContext(attributes, next) { | ||
@@ -47,0 +55,0 @@ const defaultAttributes = this.storage.getStore() ?? {}; |
| import { serializeError } from "serialize-error"; | ||
| import { isError } from "../error.js"; | ||
| import { LogLevel } from "../level.js"; | ||
| const logLevelNames = { | ||
| [LogLevel.Fatal]: "fatal", | ||
| [LogLevel.Error]: "error", | ||
| [LogLevel.Warn]: "warn", | ||
| [LogLevel.Info]: "info", | ||
| [LogLevel.Debug]: "debug", | ||
| [LogLevel.Trace]: "trace", | ||
| }; | ||
| export class NdJsonTransport { | ||
@@ -16,3 +7,3 @@ log(entry) { | ||
| result.time = entry.time.toISOString(); | ||
| result.level = logLevelNames[entry.level]; | ||
| result.level = entry.level.name; | ||
| result.msg = entry.message; | ||
@@ -19,0 +10,0 @@ for (const [key, value] of Object.entries(entry.attributes)) { |
@@ -6,10 +6,9 @@ import path from "node:path"; | ||
| import { isError, parseStack } from "../error.js"; | ||
| import { LogLevel } from "../level.js"; | ||
| const logLevelNames = { | ||
| [LogLevel.Fatal]: chalk.redBright("FATAL"), | ||
| [LogLevel.Error]: chalk.red("ERROR"), | ||
| [LogLevel.Warn]: chalk.yellow("WARN"), | ||
| [LogLevel.Info]: chalk.blue("INFO"), | ||
| [LogLevel.Debug]: chalk.green("DEBUG"), | ||
| [LogLevel.Trace]: chalk.greenBright("TRACING"), | ||
| fatal: chalk.redBright("FATAL"), | ||
| error: chalk.red("ERROR"), | ||
| warn: chalk.yellow("WARN"), | ||
| info: chalk.blue("INFO"), | ||
| debug: chalk.green("DEBUG"), | ||
| trace: chalk.greenBright("TRACING"), | ||
| }; | ||
@@ -21,3 +20,3 @@ const metaStart = chalk.gray("["); | ||
| log(entry) { | ||
| let output = `${metaStart}${entry.time.toISOString().slice(0, -5)}Z ${logLevelNames[entry.level]}${metaEnd} ${entry.message}\n`; | ||
| let output = `${metaStart}${entry.time.toISOString().slice(0, -5)}Z ${logLevelNames[entry.level.name]}${metaEnd} ${entry.message}\n`; | ||
| for (const [key, value] of Object.entries(entry.attributes)) { | ||
@@ -24,0 +23,0 @@ output += `${formatAttribute(key, value, 2, true)}\n`; |
+1
-1
| { | ||
| "name": "logforth", | ||
| "version": "1.2.3", | ||
| "version": "1.3.0", | ||
| "description": "Simple logging framework for NodeJS", | ||
@@ -5,0 +5,0 @@ "type": "module", |
17251
8.08%340
14.09%