@nexusts/logger 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.d.ts +35 -0
- package/dist/index.js +3 -19
- package/dist/index.js.map +4 -4
- package/dist/logger.module.d.ts +34 -0
- package/dist/logger.service.d.ts +54 -0
- package/dist/transports/index.d.ts +34 -0
- package/dist/types.d.ts +57 -0
- package/package.json +6 -11
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ This module is part of the NexusTS monorepo. Each module is published as its own
|
|
|
15
15
|
Most apps start with just the core:
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
bun add @nexusts/core
|
|
18
|
+
bun add @nexusts/core
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Then add this module only if you need it:
|
|
@@ -26,7 +26,7 @@ bun add @nexusts/logger
|
|
|
26
26
|
|
|
27
27
|
## Peer dependencies
|
|
28
28
|
|
|
29
|
-
None.
|
|
29
|
+
**None.** No external dependencies. Uses `pino` (bundled) under the hood.
|
|
30
30
|
|
|
31
31
|
## Usage
|
|
32
32
|
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API for `nexusjs/logger`.
|
|
3
|
+
*
|
|
4
|
+
* Quick start:
|
|
5
|
+
*
|
|
6
|
+
* // src/app/app.module.ts
|
|
7
|
+
* import { Module } from 'nexusjs';
|
|
8
|
+
* import { LoggerModule } from 'nexusjs/logger';
|
|
9
|
+
*
|
|
10
|
+
* @Module({
|
|
11
|
+
* imports: [
|
|
12
|
+
* LoggerModule.forRoot({
|
|
13
|
+
* level: 'info',
|
|
14
|
+
* pretty: process.env.NODE_ENV !== 'production',
|
|
15
|
+
* }),
|
|
16
|
+
* ],
|
|
17
|
+
* })
|
|
18
|
+
* export class AppModule {}
|
|
19
|
+
*
|
|
20
|
+
* // any service
|
|
21
|
+
* import { Logger } from 'nexusjs/logger';
|
|
22
|
+
*
|
|
23
|
+
* @Injectable()
|
|
24
|
+
* class MyService {
|
|
25
|
+
* constructor(@Inject(Logger.TOKEN) private logger: Logger) {}
|
|
26
|
+
*
|
|
27
|
+
* async handle() {
|
|
28
|
+
* this.logger.info({ userId: 'u-1' }, 'user signed in');
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*/
|
|
32
|
+
export * from "./types.js";
|
|
33
|
+
export { Logger } from "./logger.service.js";
|
|
34
|
+
export { LoggerModule } from "./logger.module.js";
|
|
35
|
+
export { PinoTransport, PrettyTransport, NullTransport, } from "./transports/index.js";
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
-
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
-
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
-
for (let key of __getOwnPropNames(mod))
|
|
11
|
-
if (!__hasOwnProp.call(to, key))
|
|
12
|
-
__defProp(to, key, {
|
|
13
|
-
get: () => mod[key],
|
|
14
|
-
enumerable: true
|
|
15
|
-
});
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
2
|
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
19
3
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
4
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -33,7 +17,7 @@ var __legacyMetadataTS = (k, v) => {
|
|
|
33
17
|
var __require = import.meta.require;
|
|
34
18
|
// packages/logger/src/logger.service.ts
|
|
35
19
|
import { AsyncLocalStorage } from "async_hooks";
|
|
36
|
-
import { Inject, Injectable } from "@nexusts/core
|
|
20
|
+
import { Inject, Injectable } from "@nexusts/core";
|
|
37
21
|
|
|
38
22
|
// packages/logger/src/transports/index.ts
|
|
39
23
|
var pinoSingleton = null;
|
|
@@ -278,7 +262,7 @@ Logger = __legacyDecorateClassTS([
|
|
|
278
262
|
], Logger);
|
|
279
263
|
// packages/logger/src/logger.module.ts
|
|
280
264
|
import"reflect-metadata";
|
|
281
|
-
import { Module } from "@nexusts/core
|
|
265
|
+
import { Module } from "@nexusts/core";
|
|
282
266
|
class LoggerModule {
|
|
283
267
|
static forRoot(options = {}) {
|
|
284
268
|
class ConfiguredLoggerModule {
|
|
@@ -313,5 +297,5 @@ export {
|
|
|
313
297
|
Logger
|
|
314
298
|
};
|
|
315
299
|
|
|
316
|
-
//# debugId=
|
|
300
|
+
//# debugId=5B7EA50A1A110AD064756E2164756E21
|
|
317
301
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/logger.service.ts", "../src/transports/index.ts", "../src/logger.module.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * `Logger` — the user-facing logging interface.\n *\n * Logger is request-scoped via `AsyncLocalStorage`: any `logger.info(...)`\n * call inside a request automatically merges in fields set by\n * `logger.with({ requestId, userId, ... })`.\n *\n * Usage:\n * constructor(@Inject(Logger.TOKEN) private logger: Logger) {}\n *\n * this.logger.info({ userId: 'u-1' }, 'user signed in');\n * this.logger.error({ err }, 'failed to save');\n */\n\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { Inject, Injectable } from \"@nexusts/core
|
|
5
|
+
"/**\n * `Logger` — the user-facing logging interface.\n *\n * Logger is request-scoped via `AsyncLocalStorage`: any `logger.info(...)`\n * call inside a request automatically merges in fields set by\n * `logger.with({ requestId, userId, ... })`.\n *\n * Usage:\n * constructor(@Inject(Logger.TOKEN) private logger: Logger) {}\n *\n * this.logger.info({ userId: 'u-1' }, 'user signed in');\n * this.logger.error({ err }, 'failed to save');\n */\n\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { Inject, Injectable } from \"@nexusts/core\";\nimport type {\n\tLogLevel,\n\tLogRecord,\n\tLogTransport,\n\tLoggerOptions,\n\tLogContext,\n} from \"./types.js\";\nimport { PinoTransport, PrettyTransport, NullTransport } from \"./transports/index.js\";\n\nconst LEVEL_RANK: Record<LogLevel, number> = {\n\ttrace: 10,\n\tdebug: 20,\n\tinfo: 30,\n\twarn: 40,\n\terror: 50,\n\tfatal: 60,\n};\n\n@Injectable()\nexport class Logger {\n\t/** DI token — use with `@Inject(Logger.TOKEN)`. */\n\tstatic readonly TOKEN = Symbol.for(\"nexus:Logger\");\n\n\ttransports: LogTransport[];\n\tsilent: boolean;\n\tbase: Record<string, unknown>;\n\tlevel: LogLevel;\n\tals = new AsyncLocalStorage<LogContext>();\n\n\tconstructor(@Inject(\"LOGGER_OPTIONS\") options: LoggerOptions = {}) {\n\t\tthis.silent = options.silent ?? false;\n\t\tthis.base = options.base ?? {};\n\t\tthis.level = options.level ?? (process.env[\"NODE_ENV\"] === \"production\" ? \"info\" : \"debug\");\n\t\tif (options.transports && options.transports.length > 0) {\n\t\t\tthis.transports = options.transports;\n\t\t} else {\n\t\t\tconst pretty = options.pretty ?? process.env[\"NODE_ENV\"] !== \"production\";\n\t\t\tthis.transports = [\n\t\t\t\tpretty\n\t\t\t\t\t? new PrettyTransport(this.level, this.base)\n\t\t\t\t\t: new PinoTransport(this.level, this.base),\n\t\t\t];\n\t\t}\n\t}\n\n\t// ===========================================================================\n\t// Level methods\n\t// ===========================================================================\n\n\ttrace(meta: Record<string, unknown>, msg: string): void;\n\ttrace(msg: string): void;\n\ttrace(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"trace\", arg1, arg2);\n\t}\n\n\tdebug(meta: Record<string, unknown>, msg: string): void;\n\tdebug(msg: string): void;\n\tdebug(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"debug\", arg1, arg2);\n\t}\n\n\tinfo(meta: Record<string, unknown>, msg: string): void;\n\tinfo(msg: string): void;\n\tinfo(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"info\", arg1, arg2);\n\t}\n\n\twarn(meta: Record<string, unknown>, msg: string): void;\n\twarn(msg: string): void;\n\twarn(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"warn\", arg1, arg2);\n\t}\n\n\terror(meta: Record<string, unknown>, msg: string): void;\n\terror(msg: string): void;\n\terror(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"error\", arg1, arg2);\n\t}\n\n\tfatal(meta: Record<string, unknown>, msg: string): void;\n\tfatal(msg: string): void;\n\tfatal(arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tthis.emit(\"fatal\", arg1, arg2);\n\t}\n\n\t// ===========================================================================\n\t// Context\n\t// ===========================================================================\n\n\t/**\n\t * Run `fn` inside a logger context — every log emitted during\n\t * `fn()` is tagged with `meta`.\n\t */\n\twith<T>(meta: LogContext, fn: () => T): T {\n\t\tconst prev = this.als.getStore() ?? {};\n\t\tconst next: LogContext = { ...prev, ...meta };\n\t\treturn this.als.run(next, fn);\n\t}\n\n\t/** Read the current request context (or empty object). */\n\tget context(): LogContext {\n\t\treturn this.als.getStore() ?? {};\n\t}\n\n\t// ===========================================================================\n\t// Child loggers\n\t// ===========================================================================\n\n\t/**\n\t * Derive a child logger that always merges `bindings` into every\n\t * record. Useful for service-scoped loggers.\n\t */\n\tchild(bindings: Record<string, unknown>): Logger {\n\t\tconst child = Object.create(Logger.prototype) as Logger;\n\t\tchild.transports = this.transports;\n\t\tchild.silent = this.silent;\n\t\tchild.base = { ...this.base, ...bindings };\n\t\tchild.level = this.level;\n\t\tchild.als = this.als;\n\t\treturn child;\n\t}\n\n\t// ===========================================================================\n\t// Lifecycle\n\t// ===========================================================================\n\n\t/** Wait for transports to finish loading (Pino is async). */\n\tasync ready(): Promise<void> {\n\t\tfor (const t of this.transports) {\n\t\t\tconst r = (t as { ready?: () => Promise<void> }).ready;\n\t\t\tif (r) await r.call(t);\n\t\t}\n\t}\n\n\t// ===========================================================================\n\t// Internal\n\t// ===========================================================================\n\n\tprivate emit(level: LogLevel, arg1: Record<string, unknown> | string, arg2?: string): void {\n\t\tif (this.silent) return;\n\t\tif (LEVEL_RANK[level] < LEVEL_RANK[this.level]) return;\n\n\t\tlet meta: Record<string, unknown> = {};\n\t\tlet msg: string;\n\t\tif (typeof arg1 === \"string\") {\n\t\t\tmsg = arg1;\n\t\t} else {\n\t\t\tmeta = arg1;\n\t\t\tmsg = arg2 ?? \"\";\n\t\t}\n\n\t\tconst ctx = this.als.getStore() ?? {};\n\t\tconst record: LogRecord = {\n\t\t\tlevel,\n\t\t\ttime: Date.now(),\n\t\t\tmsg,\n\t\t\t...this.base,\n\t\t\t...meta,\n\t\t\t...ctx,\n\t\t};\n\t\tfor (const t of this.transports) {\n\t\t\ttry {\n\t\t\t\tt.write(record);\n\t\t\t} catch {\n\t\t\t\t// never let a logging error crash the request\n\t\t\t}\n\t\t}\n\t}\n}\n\n// Re-export NullTransport for tests.\nexport { NullTransport };",
|
|
6
6
|
"/**\n * Built-in transports.\n *\n * - **PinoTransport** — JSON output via `pino` (production).\n * - **PrettyTransport** — colorized via `pino-pretty` (development).\n *\n * Both wrap the pino API and feed records into the same write loop,\n * so switching is one config flag.\n */\n\nimport type { LogLevel, LogRecord, LogTransport } from \"../types.js\";\n\ninterface PinoLike {\n\tlevel: string;\n\tinfo: (obj: object, msg?: string) => void;\n\twarn: (obj: object, msg?: string) => void;\n\terror: (obj: object, msg?: string) => void;\n\tdebug: (obj: object, msg?: string) => void;\n\ttrace: (obj: object, msg?: string) => void;\n\tfatal: (obj: object, msg?: string) => void;\n\tflush?: () => void;\n}\n\nlet pinoSingleton: PinoLike | null = null;\nlet prettySingleton: PinoLike | null = null;\n\nasync function loadPino(\n\tpretty: boolean,\n\tlevel: LogLevel,\n\tbase?: Record<string, unknown>,\n): Promise<PinoLike> {\n\tconst pinoMod = await import(\"pino\");\n\tconst pino = pinoMod.default ?? pinoMod;\n\tif (pretty) {\n\t\tif (prettySingleton) return prettySingleton;\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\tconst opts: any = {\n\t\t\tlevel,\n\t\t\tbase,\n\t\t\terrorKey: \"error\",\n\t\t\ttranslateTime: \"HH:MM:ss.l\",\n\t\t\tignore: \"pid,hostname\",\n\t\t\tcolorize: true,\n\t\t};\n\t\t// Try to use pino-pretty if installed.\n\t\ttry {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-require-imports\n\t\t\tconst prettyMod = (\n\t\t\t\tglobalThis as { require?: (id: string) => unknown }\n\t\t\t).require?.(\"pino-pretty\") as ((opts: unknown) => unknown) | undefined;\n\t\t\tif (prettyMod) {\n\t\t\t\tprettySingleton = (pino as unknown as (opts: unknown) => PinoLike)(\n\t\t\t\t\topts,\n\t\t\t\t);\n\t\t\t\treturn prettySingleton;\n\t\t\t}\n\t\t} catch {\n\t\t\t// fall through\n\t\t}\n\t\tprettySingleton = (pino as unknown as (opts: unknown) => PinoLike)({\n\t\t\tlevel,\n\t\t\tbase,\n\t\t\ttimestamp: () => `,\"time\":\"${new Date().toISOString()}\"`,\n\t\t});\n\t\treturn prettySingleton;\n\t}\n\tif (pinoSingleton) return pinoSingleton;\n\tpinoSingleton = (pino as unknown as (opts: unknown) => PinoLike)({\n\t\tlevel,\n\t\tbase,\n\t\ttimestamp: () => `,\"time\":\"${new Date().toISOString()}\"`,\n\t});\n\treturn pinoSingleton;\n}\n\n/** JSON transport via pino. */\nexport class PinoTransport implements LogTransport {\n\treadonly name = \"pino\";\n\treadonly isDefault = true;\n\t#pino: PinoLike | null = null;\n\t#ready: Promise<void>;\n\n\tconstructor(level: LogLevel, base?: Record<string, unknown>) {\n\t\tthis.#ready = loadPino(false, level, base).then((p) => {\n\t\t\tthis.#pino = p;\n\t\t});\n\t}\n\n\tasync ready(): Promise<void> {\n\t\tawait this.#ready;\n\t}\n\n\twrite(record: LogRecord): void {\n\t\tif (!this.#pino) {\n\t\t\t// Fall back to stdout until pino is loaded.\n\t\t\tconsole.log(JSON.stringify(record));\n\t\t\treturn;\n\t\t}\n\t\tconst { level, time, msg, ...rest } = record;\n\t\tconst obj = { time, ...rest };\n\t\tswitch (level) {\n\t\t\tcase \"trace\":\n\t\t\t\tthis.#pino.trace(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"debug\":\n\t\t\t\tthis.#pino.debug(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"info\":\n\t\t\t\tthis.#pino.info(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"warn\":\n\t\t\t\tthis.#pino.warn(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"error\":\n\t\t\t\tthis.#pino.error(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"fatal\":\n\t\t\t\tthis.#pino.fatal(obj, msg);\n\t\t\t\tbreak;\n\t\t}\n\t}\n}\n\n/** Pretty-print transport via pino-pretty (development). */\nexport class PrettyTransport implements LogTransport {\n\treadonly name = \"pretty\";\n\treadonly isDefault = true;\n\t#pino: PinoLike | null = null;\n\t#ready: Promise<void>;\n\n\tconstructor(level: LogLevel, base?: Record<string, unknown>) {\n\t\tthis.#ready = loadPino(true, level, base).then((p) => {\n\t\t\tthis.#pino = p;\n\t\t});\n\t}\n\n\tasync ready(): Promise<void> {\n\t\tawait this.#ready;\n\t}\n\n\twrite(record: LogRecord): void {\n\t\tif (!this.#pino) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.log(`[${record.level}] ${record.msg}`, record);\n\t\t\treturn;\n\t\t}\n\t\tconst { level, time, msg, ...rest } = record;\n\t\tconst obj = { time, ...rest };\n\t\tswitch (level) {\n\t\t\tcase \"trace\":\n\t\t\t\tthis.#pino.trace(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"debug\":\n\t\t\t\tthis.#pino.debug(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"info\":\n\t\t\t\tthis.#pino.info(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"warn\":\n\t\t\t\tthis.#pino.warn(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"error\":\n\t\t\t\tthis.#pino.error(obj, msg);\n\t\t\t\tbreak;\n\t\t\tcase \"fatal\":\n\t\t\t\tthis.#pino.fatal(obj, msg);\n\t\t\t\tbreak;\n\t\t}\n\t}\n}\n\n/** Null transport — drops everything. For tests. */\nexport class NullTransport implements LogTransport {\n\treadonly name = \"null\";\n\treadonly isDefault = false;\n\twrite(_record: LogRecord): void {\n\t\t// discard\n\t}\n}\n",
|
|
7
|
-
"/**\n * `LoggerModule` — drop-in module for structured logging.\n *\n * Usage:\n * @Module({\n * imports: [\n * LoggerModule.forRoot({\n * level: 'info', // 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'\n * pretty: process.env.NODE_ENV !== 'production',\n * base: { service: 'my-app' },\n * }),\n * ],\n * })\n * export class AppModule {}\n *\n * // any service\n * @Injectable()\n * class MyService {\n * constructor(@Inject(Logger.TOKEN) private logger: Logger) {}\n *\n * async handle() {\n * await this.logger.with({ requestId: 'r-1' }, async () => {\n * this.logger.info({ userId: 'u-1' }, 'processing');\n * });\n * }\n * }\n */\n\nimport \"reflect-metadata\";\nimport { Module } from \"@nexusts/core
|
|
7
|
+
"/**\n * `LoggerModule` — drop-in module for structured logging.\n *\n * Usage:\n * @Module({\n * imports: [\n * LoggerModule.forRoot({\n * level: 'info', // 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'\n * pretty: process.env.NODE_ENV !== 'production',\n * base: { service: 'my-app' },\n * }),\n * ],\n * })\n * export class AppModule {}\n *\n * // any service\n * @Injectable()\n * class MyService {\n * constructor(@Inject(Logger.TOKEN) private logger: Logger) {}\n *\n * async handle() {\n * await this.logger.with({ requestId: 'r-1' }, async () => {\n * this.logger.info({ userId: 'u-1' }, 'processing');\n * });\n * }\n * }\n */\n\nimport \"reflect-metadata\";\nimport { Module } from \"@nexusts/core\";\nimport { Logger } from \"./logger.service.js\";\nimport type { LoggerOptions } from \"./types.js\";\n\n@Module({\n\tproviders: [Logger, { provide: Logger.TOKEN, useExisting: Logger }],\n\texports: [Logger, Logger.TOKEN],\n})\nexport class LoggerModule {\n\tstatic forRoot(options: LoggerOptions = {}) {\n\t\t@Module({\n\t\t\tproviders: [\n\t\t\t\tLogger,\n\t\t\t\t{ provide: Logger.TOKEN, useExisting: Logger },\n\t\t\t\t{ provide: \"LOGGER_OPTIONS\", useValue: options },\n\t\t\t],\n\t\t\texports: [Logger, Logger.TOKEN],\n\t\t})\n\t\tclass ConfiguredLoggerModule {}\n\n\t\tObject.defineProperty(ConfiguredLoggerModule, \"name\", {\n\t\t\tvalue: \"ConfiguredLoggerModule\",\n\t\t});\n\n\t\treturn ConfiguredLoggerModule;\n\t}\n}\n"
|
|
8
8
|
],
|
|
9
|
-
"mappings": "
|
|
10
|
-
"debugId": "
|
|
9
|
+
"mappings": ";;;;;;;;;;;;;;;;;;AAcA;AACA;;;ACQA,IAAI,gBAAiC;AACrC,IAAI,kBAAmC;AAEvC,eAAe,QAAQ,CACtB,QACA,OACA,MACoB;AAAA,EACpB,MAAM,UAAU,MAAa;AAAA,EAC7B,MAAM,OAAO,QAAQ,WAAW;AAAA,EAChC,IAAI,QAAQ;AAAA,IACX,IAAI;AAAA,MAAiB,OAAO;AAAA,IAE5B,MAAM,OAAY;AAAA,MACjB;AAAA,MACA;AAAA,MACA,UAAU;AAAA,MACV,eAAe;AAAA,MACf,QAAQ;AAAA,MACR,UAAU;AAAA,IACX;AAAA,IAEA,IAAI;AAAA,MAEH,MAAM,YACL,WACC,UAAU,aAAa;AAAA,MAC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AA,SACL;AAAA,SACA;AAAA,IACJ;AAAA,IACA,WAAW,KAAK,KAAK,YAAY;AAAA,MAChC,IAAI;AAAA,QACH,EAAE,MAAM,MAAM;AAAA,QACb,MAAM;AAAA,IAGT;AAAA;AAEF;AArJa,SAAN;AAAA,EADN,WAAW;AAAA,EAWE,kCAAO,gBAAgB;AAAA,EAV9B;AAAA;AAAA;AAAA,GAAM;;AEPb;AACA;AAQO,MAAM,aAAa;AAAA,SAClB,OAAO,CAAC,UAAyB,CAAC,GAAG;AAAA,IAS3C,MAAM,uBAAuB;AAAA,IAAC;AAAA,IAAxB,yBAAN;AAAA,MARC,OAAO;AAAA,QACP,WAAW;AAAA,UACV;AAAA,UACA,EAAE,SAAS,OAAO,OAAO,aAAa,OAAO;AAAA,UAC7C,EAAE,SAAS,kBAAkB,UAAU,QAAQ;AAAA,QAChD;AAAA,QACA,SAAS,CAAC,QAAQ,OAAO,KAAK;AAAA,MAC/B,CAAC;AAAA,OACK;AAAA,IAEN,OAAO,eAAe,wBAAwB,QAAQ;AAAA,MACrD,OAAO;AAAA,IACR,CAAC;AAAA,IAED,OAAO;AAAA;AAET;AAlBa,eAAN;AAAA,EAJN,OAAO;AAAA,IACP,WAAW,CAAC,QAAQ,EAAE,SAAS,OAAO,OAAO,aAAa,OAAO,CAAC;AAAA,IAClE,SAAS,CAAC,QAAQ,OAAO,KAAK;AAAA,EAC/B,CAAC;AAAA,GACY;",
|
|
10
|
+
"debugId": "5B7EA50A1A110AD064756E2164756E21",
|
|
11
11
|
"names": []
|
|
12
12
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `LoggerModule` — drop-in module for structured logging.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* @Module({
|
|
6
|
+
* imports: [
|
|
7
|
+
* LoggerModule.forRoot({
|
|
8
|
+
* level: 'info', // 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'
|
|
9
|
+
* pretty: process.env.NODE_ENV !== 'production',
|
|
10
|
+
* base: { service: 'my-app' },
|
|
11
|
+
* }),
|
|
12
|
+
* ],
|
|
13
|
+
* })
|
|
14
|
+
* export class AppModule {}
|
|
15
|
+
*
|
|
16
|
+
* // any service
|
|
17
|
+
* @Injectable()
|
|
18
|
+
* class MyService {
|
|
19
|
+
* constructor(@Inject(Logger.TOKEN) private logger: Logger) {}
|
|
20
|
+
*
|
|
21
|
+
* async handle() {
|
|
22
|
+
* await this.logger.with({ requestId: 'r-1' }, async () => {
|
|
23
|
+
* this.logger.info({ userId: 'u-1' }, 'processing');
|
|
24
|
+
* });
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
*/
|
|
28
|
+
import "reflect-metadata";
|
|
29
|
+
import type { LoggerOptions } from "./types.js";
|
|
30
|
+
export declare class LoggerModule {
|
|
31
|
+
static forRoot(options?: LoggerOptions): {
|
|
32
|
+
new (): {};
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `Logger` — the user-facing logging interface.
|
|
3
|
+
*
|
|
4
|
+
* Logger is request-scoped via `AsyncLocalStorage`: any `logger.info(...)`
|
|
5
|
+
* call inside a request automatically merges in fields set by
|
|
6
|
+
* `logger.with({ requestId, userId, ... })`.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* constructor(@Inject(Logger.TOKEN) private logger: Logger) {}
|
|
10
|
+
*
|
|
11
|
+
* this.logger.info({ userId: 'u-1' }, 'user signed in');
|
|
12
|
+
* this.logger.error({ err }, 'failed to save');
|
|
13
|
+
*/
|
|
14
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
15
|
+
import type { LogLevel, LogTransport, LoggerOptions, LogContext } from "./types.js";
|
|
16
|
+
import { NullTransport } from "./transports/index.js";
|
|
17
|
+
export declare class Logger {
|
|
18
|
+
/** DI token — use with `@Inject(Logger.TOKEN)`. */
|
|
19
|
+
static readonly TOKEN: unique symbol;
|
|
20
|
+
transports: LogTransport[];
|
|
21
|
+
silent: boolean;
|
|
22
|
+
base: Record<string, unknown>;
|
|
23
|
+
level: LogLevel;
|
|
24
|
+
als: AsyncLocalStorage<LogContext>;
|
|
25
|
+
constructor(options?: LoggerOptions);
|
|
26
|
+
trace(meta: Record<string, unknown>, msg: string): void;
|
|
27
|
+
trace(msg: string): void;
|
|
28
|
+
debug(meta: Record<string, unknown>, msg: string): void;
|
|
29
|
+
debug(msg: string): void;
|
|
30
|
+
info(meta: Record<string, unknown>, msg: string): void;
|
|
31
|
+
info(msg: string): void;
|
|
32
|
+
warn(meta: Record<string, unknown>, msg: string): void;
|
|
33
|
+
warn(msg: string): void;
|
|
34
|
+
error(meta: Record<string, unknown>, msg: string): void;
|
|
35
|
+
error(msg: string): void;
|
|
36
|
+
fatal(meta: Record<string, unknown>, msg: string): void;
|
|
37
|
+
fatal(msg: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Run `fn` inside a logger context — every log emitted during
|
|
40
|
+
* `fn()` is tagged with `meta`.
|
|
41
|
+
*/
|
|
42
|
+
with<T>(meta: LogContext, fn: () => T): T;
|
|
43
|
+
/** Read the current request context (or empty object). */
|
|
44
|
+
get context(): LogContext;
|
|
45
|
+
/**
|
|
46
|
+
* Derive a child logger that always merges `bindings` into every
|
|
47
|
+
* record. Useful for service-scoped loggers.
|
|
48
|
+
*/
|
|
49
|
+
child(bindings: Record<string, unknown>): Logger;
|
|
50
|
+
/** Wait for transports to finish loading (Pino is async). */
|
|
51
|
+
ready(): Promise<void>;
|
|
52
|
+
private emit;
|
|
53
|
+
}
|
|
54
|
+
export { NullTransport };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in transports.
|
|
3
|
+
*
|
|
4
|
+
* - **PinoTransport** — JSON output via `pino` (production).
|
|
5
|
+
* - **PrettyTransport** — colorized via `pino-pretty` (development).
|
|
6
|
+
*
|
|
7
|
+
* Both wrap the pino API and feed records into the same write loop,
|
|
8
|
+
* so switching is one config flag.
|
|
9
|
+
*/
|
|
10
|
+
import type { LogLevel, LogRecord, LogTransport } from "../types.js";
|
|
11
|
+
/** JSON transport via pino. */
|
|
12
|
+
export declare class PinoTransport implements LogTransport {
|
|
13
|
+
#private;
|
|
14
|
+
readonly name = "pino";
|
|
15
|
+
readonly isDefault = true;
|
|
16
|
+
constructor(level: LogLevel, base?: Record<string, unknown>);
|
|
17
|
+
ready(): Promise<void>;
|
|
18
|
+
write(record: LogRecord): void;
|
|
19
|
+
}
|
|
20
|
+
/** Pretty-print transport via pino-pretty (development). */
|
|
21
|
+
export declare class PrettyTransport implements LogTransport {
|
|
22
|
+
#private;
|
|
23
|
+
readonly name = "pretty";
|
|
24
|
+
readonly isDefault = true;
|
|
25
|
+
constructor(level: LogLevel, base?: Record<string, unknown>);
|
|
26
|
+
ready(): Promise<void>;
|
|
27
|
+
write(record: LogRecord): void;
|
|
28
|
+
}
|
|
29
|
+
/** Null transport — drops everything. For tests. */
|
|
30
|
+
export declare class NullTransport implements LogTransport {
|
|
31
|
+
readonly name = "null";
|
|
32
|
+
readonly isDefault = false;
|
|
33
|
+
write(_record: LogRecord): void;
|
|
34
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger types — the contract for `nexusjs/logger`.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `@adonisjs/logger` + NestJS's built-in Logger. One logger
|
|
5
|
+
* class, two transports:
|
|
6
|
+
*
|
|
7
|
+
* - **Pino** (production default) — JSON output, fast
|
|
8
|
+
* - **Pretty** (development) — colorized, human-readable
|
|
9
|
+
*
|
|
10
|
+
* Logs are request-scoped via AsyncLocalStorage: any `logger.info(...)`
|
|
11
|
+
* inside a request automatically includes `requestId`, `userId`,
|
|
12
|
+
* `tenantId` if those have been set.
|
|
13
|
+
*/
|
|
14
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";
|
|
15
|
+
/** A single log record (Pino-compatible shape). */
|
|
16
|
+
export interface LogRecord {
|
|
17
|
+
level: LogLevel;
|
|
18
|
+
time: number;
|
|
19
|
+
msg: string;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
/** Transport: receives every record, writes it somewhere. */
|
|
23
|
+
export interface LogTransport {
|
|
24
|
+
/** Display name. */
|
|
25
|
+
readonly name: string;
|
|
26
|
+
/** Whether this transport is the active default. */
|
|
27
|
+
readonly isDefault?: boolean;
|
|
28
|
+
/** Write a record. Must be sync; queue async work internally. */
|
|
29
|
+
write(record: LogRecord): void;
|
|
30
|
+
}
|
|
31
|
+
/** Configuration for the LoggerModule. */
|
|
32
|
+
export interface LoggerOptions {
|
|
33
|
+
/** Minimum level to emit. Default: `'info'` in production, `'debug'` in dev. */
|
|
34
|
+
level?: LogLevel;
|
|
35
|
+
/** Pretty-print (dev mode). Default: `NODE_ENV !== 'production'`. */
|
|
36
|
+
pretty?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Custom transports. The first one with `isDefault: true` (or the
|
|
39
|
+
* first one if none is default) is used as the active transport.
|
|
40
|
+
*/
|
|
41
|
+
transports?: LogTransport[];
|
|
42
|
+
/**
|
|
43
|
+
* Static fields attached to every record (e.g. service name).
|
|
44
|
+
*/
|
|
45
|
+
base?: Record<string, unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* Disable all logging. Useful in tests.
|
|
48
|
+
*/
|
|
49
|
+
silent?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** Per-request context (AsyncLocalStorage value). */
|
|
52
|
+
export interface LogContext {
|
|
53
|
+
requestId?: string;
|
|
54
|
+
userId?: string;
|
|
55
|
+
tenantId?: string;
|
|
56
|
+
[key: string]: unknown;
|
|
57
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexusts/logger",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Pino-backed structured logging",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -12,20 +12,15 @@
|
|
|
12
12
|
"import": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
16
|
-
"dist",
|
|
17
|
-
"README.md"
|
|
18
|
-
],
|
|
15
|
+
"files": ["dist", "README.md"],
|
|
19
16
|
"scripts": {
|
|
20
17
|
"build": "bun run ../../build.ts"
|
|
21
18
|
},
|
|
22
|
-
"keywords": [
|
|
23
|
-
"nexusts",
|
|
24
|
-
"framework",
|
|
25
|
-
"bun"
|
|
26
|
-
],
|
|
19
|
+
"keywords": ["nexusts", "framework", "bun"],
|
|
27
20
|
"license": "MIT",
|
|
21
|
+
|
|
22
|
+
|
|
28
23
|
"dependencies": {
|
|
29
|
-
"@nexusts/core": "
|
|
24
|
+
"@nexusts/core": "file:../core"
|
|
30
25
|
}
|
|
31
26
|
}
|