@vsfedorenko/next-logger 0.1.0 → 0.2.0
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 +99 -121
- package/README.ru.md +209 -0
- package/README.zh.md +199 -0
- package/dist/config.d.ts +25 -17
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +32 -70
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +31 -50
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -54
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +40 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +50 -0
- package/dist/init.js.map +1 -0
- package/dist/logger.d.ts +8 -15
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +8 -16
- package/dist/logger.js.map +1 -1
- package/dist/patches/console.d.ts +17 -18
- package/dist/patches/console.d.ts.map +1 -1
- package/dist/patches/console.js +23 -34
- package/dist/patches/console.js.map +1 -1
- package/dist/patches/next.d.ts +11 -36
- package/dist/patches/next.d.ts.map +1 -1
- package/dist/patches/next.js +22 -113
- package/dist/patches/next.js.map +1 -1
- package/dist/withLogger.d.ts +36 -0
- package/dist/withLogger.d.ts.map +1 -0
- package/dist/withLogger.js +34 -0
- package/dist/withLogger.js.map +1 -0
- package/package.json +4 -21
- package/dist/presets/all.d.ts +0 -14
- package/dist/presets/all.d.ts.map +0 -1
- package/dist/presets/all.js +0 -15
- package/dist/presets/all.js.map +0 -1
- package/dist/presets/next-only.d.ts +0 -12
- package/dist/presets/next-only.d.ts.map +0 -1
- package/dist/presets/next-only.js +0 -13
- package/dist/presets/next-only.js.map +0 -1
package/README.zh.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# @vsfedorenko/next-logger
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@vsfedorenko/next-logger)
|
|
4
|
+
|
|
5
|
+
> 语言:[English](README.md) | [Русский](README.ru.md) | **中文**
|
|
6
|
+
|
|
7
|
+
**一个面向 Next.js 的通用日志工具包。**
|
|
8
|
+
|
|
9
|
+
它包装全局的 `console.*`——也就是 Next.js 自身内部日志器所流向的同一个
|
|
10
|
+
接收端——使所有诊断输出都经过一个可按级别控制的
|
|
11
|
+
[consola](https://github.com/unjs/consola) 实例,并提供可插拔的 reporter
|
|
12
|
+
接入结构化 **JSON** 等。无需自定义服务器,无需模块 monkey-patch(在
|
|
13
|
+
Turbopack 下本来就够不着)。
|
|
14
|
+
|
|
15
|
+
灵感来自 [`sainsburys-tech/next-logger`](https://github.com/sainsburys-tech/next-logger),
|
|
16
|
+
它用 [pino](https://getpino.io) 实现同样的事。本包将 pino 替换为 consola,
|
|
17
|
+
并通过惯用的 `withLogger()` 配置包装器来传递配置。
|
|
18
|
+
|
|
19
|
+
## 安装
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @vsfedorenko/next-logger consola
|
|
23
|
+
# 或
|
|
24
|
+
bun add @vsfedorenko/next-logger consola
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
`consola` 是 peer 依赖——请与本包一起安装。
|
|
28
|
+
|
|
29
|
+
## 快速开始
|
|
30
|
+
|
|
31
|
+
两步。
|
|
32
|
+
|
|
33
|
+
**1. 包装你的 Next.js 配置**(`next.config.ts`):
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { withLogger } from "@vsfedorenko/next-logger";
|
|
37
|
+
|
|
38
|
+
export default withLogger({ consola: { level: 4 } })({
|
|
39
|
+
// ...你的其他 next 配置
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**2. 在 instrumentation 中调用 `init()`**(`instrumentation.ts`,项目根目录):
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
export async function register() {
|
|
47
|
+
if (process.env.NEXT_RUNTIME === "nodejs") {
|
|
48
|
+
const { init } = await import("@vsfedorenko/next-logger");
|
|
49
|
+
init();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
完成。服务端的每一个 `console.*` 调用现在都会经过 consola。Next.js 自身的
|
|
55
|
+
日志(构建输出、路由编译等)也会被捕获——它们共用同一个 `console.*` 接收端。
|
|
56
|
+
|
|
57
|
+
> 可运行的示例应用见 [`examples/basic/`](examples/basic/)。
|
|
58
|
+
|
|
59
|
+
## 配置
|
|
60
|
+
|
|
61
|
+
`withLogger(options)` 通过 Next.js 经过校验的 `env` 配置键将 `options`
|
|
62
|
+
序列化到 `NEXT_LOGGER_CONFIG` 环境变量——构建时内联,运行时读回。不会出现
|
|
63
|
+
"Unrecognized key" 警告,在 webpack 和 Turbopack 下都能工作。
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
withLogger({
|
|
67
|
+
consola: {
|
|
68
|
+
level: 4, // debug
|
|
69
|
+
formatOptions: { date: false }, // consola 格式化选项
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
仅支持可序列化的 consola 选项(`level`、`formatOptions`、…)。
|
|
75
|
+
|
|
76
|
+
### 跳过 console 补丁
|
|
77
|
+
|
|
78
|
+
`init({ console: false })` 构建日志器但不包装 `console.*`。如果你只想要
|
|
79
|
+
配置好的 consola 实例(通过 `getLogger()`)做手动日志、但不想动全局
|
|
80
|
+
`console`,就用这个。
|
|
81
|
+
|
|
82
|
+
## 日志级别
|
|
83
|
+
|
|
84
|
+
级别按以下顺序解析:
|
|
85
|
+
|
|
86
|
+
1. 来自 `withLogger` 的 `consola.level`
|
|
87
|
+
2. `LOG_LEVEL`(数字或名称)
|
|
88
|
+
3. `NEXT_PUBLIC_LOG_LEVEL`(数字或名称)
|
|
89
|
+
4. `3`(info)——默认值
|
|
90
|
+
|
|
91
|
+
命名级别:`silent` (-∞)、`fatal` (0)、`error` (0)、`warn` (1)、
|
|
92
|
+
`log` (2)、`info` (3)、`success` (3)、`debug` (4)、`trace` (5)、`verbose` (∞)。
|
|
93
|
+
|
|
94
|
+
## 日志格式
|
|
95
|
+
|
|
96
|
+
服务端输出格式由环境变量控制:
|
|
97
|
+
|
|
98
|
+
1. `LOG_FORMAT`(`text` 或 `json`)
|
|
99
|
+
2. `NEXT_PUBLIC_LOG_FORMAT`(同上)
|
|
100
|
+
|
|
101
|
+
默认回退到 `text`(consola 默认的 pretty reporter)。
|
|
102
|
+
|
|
103
|
+
### `text`(默认)
|
|
104
|
+
|
|
105
|
+
人类可读,在 TTY 中带颜色与时间戳——consola 内置的 reporter。适合本地
|
|
106
|
+
开发。
|
|
107
|
+
|
|
108
|
+
### `json`
|
|
109
|
+
|
|
110
|
+
按行输出 JSON 到 stdout(错误 → stderr),适合结构化日志聚合器
|
|
111
|
+
(Loki、Datadog、CloudWatch、Elasticsearch)。适合生产环境。
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{"level":"info","type":"log","tag":"console","msg":"API /api/hello hit","date":"2026-07-12T10:00:00.000Z"}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
每行包含:
|
|
118
|
+
|
|
119
|
+
| 字段 | 说明 |
|
|
120
|
+
|--------|-----------------------------------------------------------------------|
|
|
121
|
+
| `level`| 命名级别(`error`/`warn`/`info`/`debug`/`trace`) |
|
|
122
|
+
| `type` | consola 日志类型(`error`、`warn`、`info`、`success`、`ready`、`event` 等)|
|
|
123
|
+
| `tag` | consola 标签(`next.js`、`console`、…) |
|
|
124
|
+
| `msg` | 消息字符串(多个字符串参数以空格连接) |
|
|
125
|
+
| `date` | ISO 8601 时间戳 |
|
|
126
|
+
| `args` | 额外的结构化参数(无则省略) |
|
|
127
|
+
|
|
128
|
+
错误会被序列化为 `{ name, message, stack }`。循环引用会变成
|
|
129
|
+
`[Circular]`。BigInt 会被转成字符串。
|
|
130
|
+
|
|
131
|
+
## 浏览器 / Client Components
|
|
132
|
+
|
|
133
|
+
服务端入口会补丁 `console.*`,这只在 Node.js 中有意义。对于
|
|
134
|
+
Client Components 或任何浏览器端代码,请使用
|
|
135
|
+
**`@vsfedorenko/next-logger/browser`** 子路径:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
"use client";
|
|
139
|
+
import { logger } from "@vsfedorenko/next-logger/browser";
|
|
140
|
+
|
|
141
|
+
export function MyComponent() {
|
|
142
|
+
logger.info("已渲染");
|
|
143
|
+
logger.warn("弃用提示");
|
|
144
|
+
return <div>…</div>;
|
|
145
|
+
}
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
该入口基于环境变量驱动的默认值构建一个 consola 实例(同样的级别解析:
|
|
149
|
+
`LOG_LEVEL` → `NEXT_PUBLIC_LOG_LEVEL` → `3`),不含任何服务端补丁机制。
|
|
150
|
+
若要让级别在构建期内联进浏览器 bundle,请使用 `NEXT_PUBLIC_LOG_LEVEL`。
|
|
151
|
+
|
|
152
|
+
## 工作原理
|
|
153
|
+
|
|
154
|
+
1. **配置包装器**(`withLogger`,构建时)——将日志器选项序列化到
|
|
155
|
+
`NEXT_LOGGER_CONFIG`,通过 Next 的 `env` 键。Next 在构建时将其内联,
|
|
156
|
+
因此运行时通过 `process.env.NEXT_LOGGER_CONFIG` 读取,无需文件系统或
|
|
157
|
+
Next.js 内部导入。
|
|
158
|
+
|
|
159
|
+
2. **Console 接收端补丁**(`patches/console.ts`,运行时)——包装
|
|
160
|
+
`console.{log,debug,info,warn,error}`,使每次调用都经过共享的 consola
|
|
161
|
+
实例。`log` 与 `info` 都映射到 consola 的 `info`。
|
|
162
|
+
|
|
163
|
+
3. **Next 日志分类器**(`patches/next.ts`,运行时)——检查每个 `console.*`
|
|
164
|
+
调用:如果第一个参数携带 Next.js 标记符号(`▲`、`✓`、`⚠`、`●`、`✗`、…),
|
|
165
|
+
该行标记为 `next.js`;否则标记为 `console`。这在 Turbopack 下有效,
|
|
166
|
+
因为旧的基于 `require.cache` 的 monkey-patch 已经失效(Next 的日志器
|
|
167
|
+
存在于单独的 bundle 实例中)。
|
|
168
|
+
|
|
169
|
+
### 空消息跳过
|
|
170
|
+
|
|
171
|
+
当消息为**空**时(没有参数,或只有 `undefined`/`null`/`""`——即没有诊断
|
|
172
|
+
价值、在 consola 下会渲染成一条只有标签而无内容的行),补丁会跳过打印。
|
|
173
|
+
这镜像了 Next.js 自身的行为:`prefixedLog` 在消息为空时会丢弃前缀。
|
|
174
|
+
|
|
175
|
+
为假但存在的值(`0`、`false`)**不**算作空,会照常打印。
|
|
176
|
+
|
|
177
|
+
### Turbopack 说明
|
|
178
|
+
|
|
179
|
+
Next.js 的**启动横幅**(`▲ Next.js`、`✓ Ready`、…)在 instrumentation 钩子
|
|
180
|
+
运行*之前*就打印了,所以这些特定行不会被捕获。启动后发出的任何日志——
|
|
181
|
+
路由编译、请求时输出、你自己的 `console.*` 调用——都会正常经过补丁。
|
|
182
|
+
|
|
183
|
+
## 与 `sainsburys-tech/next-logger` 的差异
|
|
184
|
+
|
|
185
|
+
| 关注点 | sainsburys-tech (pino) | 本包 (consola) |
|
|
186
|
+
|------------|-------------------------------------------|---------------------------------------------|
|
|
187
|
+
| 后端 | pino(JSON 到 stdout) | consola(默认 pretty) |
|
|
188
|
+
| 配置传递 | `next-logger.config.js` + 预加载 | `withLogger()` 包装器(惯用、类型安全) |
|
|
189
|
+
| 拦截方式 | 补丁 `next/dist/build/output/log` | 包装 `console.*` 接收端(Turbopack 安全) |
|
|
190
|
+
| 参数归一化 | 自定义 `hooks.logMethod` | 无需——consola 自带处理 console 风格参数 |
|
|
191
|
+
| 子日志器 | `logger.child({ name })` | `consola.withTag(tag)` |
|
|
192
|
+
| `trace` 级 | 回退到 `debug`(Winston 无 trace) | 原生——consola 有 `trace` |
|
|
193
|
+
| 默认级别 | 硬编码 `debug` | 由环境驱动(`LOG_LEVEL`) |
|
|
194
|
+
| Turbopack | `require.cache` 补丁失效 | console 接收端——正常工作 |
|
|
195
|
+
| 语言 | 纯 JS(CommonJS) | TypeScript(CJS 输出) |
|
|
196
|
+
|
|
197
|
+
## 许可证
|
|
198
|
+
|
|
199
|
+
MIT
|
package/dist/config.d.ts
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import type { ConsolaInstance, ConsolaOptions } from "consola";
|
|
2
2
|
/**
|
|
3
|
-
* Shape of
|
|
3
|
+
* Shape of the config passed to {@link withLogger}, serialised to JSON and
|
|
4
|
+
* delivered to the runtime via the `NEXT_LOGGER_CONFIG` env var (inlined at
|
|
5
|
+
* build time by Next.js' `env` config key).
|
|
4
6
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* library's default options as a starting point).
|
|
7
|
+
* Because the value crosses a build→runtime boundary as JSON, `consola` can
|
|
8
|
+
* only be a partial options object here (not a live instance or factory). The
|
|
9
|
+
* full instance/factory forms remain supported when {@link resolveLoggerConfig}
|
|
10
|
+
* is called directly with such a value (e.g. in tests).
|
|
10
11
|
*/
|
|
11
12
|
export interface NextLoggerConfig {
|
|
12
13
|
consola?: ConsolaInstance | Partial<ConsolaOptions> | ((defaults: Partial<ConsolaOptions>) => ConsolaInstance);
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
|
-
* Discriminated result of config
|
|
16
|
-
*
|
|
17
|
-
* - `kind: "instance"` — the config supplied a consola instance or a factory
|
|
18
|
-
* that produced one; use it directly.
|
|
19
|
-
* - `kind: "options"` — the config supplied a partial options object (or no
|
|
20
|
-
* config at all); build a consola from these merged options.
|
|
16
|
+
* Discriminated result of config resolution.
|
|
21
17
|
*/
|
|
22
18
|
export type ResolvedConfig = {
|
|
23
19
|
readonly kind: "instance";
|
|
@@ -26,13 +22,25 @@ export type ResolvedConfig = {
|
|
|
26
22
|
readonly kind: "options";
|
|
27
23
|
readonly options: Partial<ConsolaOptions>;
|
|
28
24
|
};
|
|
25
|
+
/** The env var that carries the serialised {@link NextLoggerConfig}. */
|
|
26
|
+
export declare const CONFIG_ENV_VAR = "NEXT_LOGGER_CONFIG";
|
|
27
|
+
/**
|
|
28
|
+
* Resolves a raw config value into a {@link ResolvedConfig}. Pure — exported
|
|
29
|
+
* for unit testing.
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveLoggerConfig(raw: NextLoggerConfig | undefined): ResolvedConfig;
|
|
29
32
|
/**
|
|
30
|
-
*
|
|
33
|
+
* Reads the `logger` config delivered by {@link withLogger} via the
|
|
34
|
+
* `NEXT_LOGGER_CONFIG` env var. Falls back to the bare defaults when the var is
|
|
35
|
+
* absent or unparseable.
|
|
36
|
+
*
|
|
37
|
+
* The access is a LITERAL `process.env.NEXT_LOGGER_CONFIG` (not computed via
|
|
38
|
+
* the {@link CONFIG_ENV_VAR} constant) so that Next.js' build-time `env`
|
|
39
|
+
* inlining (DefinePlugin) substitutes the value into the instrumentation
|
|
40
|
+
* bundle — a computed `process.env[const]` reference would NOT be inlined and
|
|
41
|
+
* would read `undefined` at runtime.
|
|
31
42
|
*
|
|
32
|
-
*
|
|
33
|
-
* supplied one or a factory) or the options object to build from (the config's
|
|
34
|
-
* partial options merged with defaults, or the bare defaults when no config
|
|
35
|
-
* exists).
|
|
43
|
+
* Sync and free of any `next` dependency.
|
|
36
44
|
*/
|
|
37
45
|
export declare function loadConfig(): ResolvedConfig;
|
|
38
46
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAI/D;;;;;;;;;GASG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EACJ,eAAe,GACf,OAAO,CAAC,cAAc,CAAC,GACvB,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,cAAc,CAAC,KAAK,eAAe,CAAC,CAAC;CAC9D;AAED;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAAC,QAAQ,CAAC,QAAQ,EAAE,eAAe,CAAA;CAAE,GACjE;IAAE,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CAAE,CAAC;AAE5E,wEAAwE;AACxE,eAAO,MAAM,cAAc,uBAAuB,CAAC;AAEnD;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,gBAAgB,GAAG,SAAS,GAChC,cAAc,CAiBhB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,IAAI,cAAc,CAQ3C"}
|
package/dist/config.js
CHANGED
|
@@ -1,95 +1,57 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CONFIG_ENV_VAR = void 0;
|
|
4
|
+
exports.resolveLoggerConfig = resolveLoggerConfig;
|
|
3
5
|
exports.loadConfig = loadConfig;
|
|
4
|
-
const lilconfig_1 = require("lilconfig");
|
|
5
6
|
const defaults_1 = require("./defaults");
|
|
6
7
|
const types_1 = require("./types");
|
|
8
|
+
/** The env var that carries the serialised {@link NextLoggerConfig}. */
|
|
9
|
+
exports.CONFIG_ENV_VAR = "NEXT_LOGGER_CONFIG";
|
|
7
10
|
/**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* Uses `jiti` when available (the standard transpile-on-require used by
|
|
11
|
-
* Nuxt/consola/tailwind). `jiti` is an optional peer dependency — when absent,
|
|
12
|
-
* a clear error is thrown pointing to the missing package.
|
|
13
|
-
*
|
|
14
|
-
* Lazily required so the default path (no `.ts` config) never pays the jiti
|
|
15
|
-
* import cost.
|
|
11
|
+
* Resolves a raw config value into a {@link ResolvedConfig}. Pure — exported
|
|
12
|
+
* for unit testing.
|
|
16
13
|
*/
|
|
17
|
-
|
|
18
|
-
const jiti = tryRequireJiti();
|
|
19
|
-
if (jiti === null) {
|
|
20
|
-
throw new Error(`next-logger: cannot load TypeScript config "${filepath}" — install "jiti" (optional peer dependency).`);
|
|
21
|
-
}
|
|
22
|
-
return jiti(filepath);
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Resolves the `jiti` callable if installed, or returns `null`.
|
|
26
|
-
*
|
|
27
|
-
* `jiti` v2 exports `createJiti`; the returned {@link Jiti} instance extends
|
|
28
|
-
* `NodeRequire`, so it is directly callable as `jiti(filepath)`.
|
|
29
|
-
*/
|
|
30
|
-
function tryRequireJiti() {
|
|
31
|
-
try {
|
|
32
|
-
const mod = require("jiti");
|
|
33
|
-
const jiti = mod.createJiti(__filename);
|
|
34
|
-
return jiti;
|
|
35
|
-
}
|
|
36
|
-
catch {
|
|
37
|
-
return null;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// Only register a loader for `.ts`; lilconfig's `defaultLoadersSync` handles
|
|
41
|
-
// `.js`/`.cjs`/`.json` natively via `require(filepath)` / `JSON.parse`.
|
|
42
|
-
const loaders = {
|
|
43
|
-
...lilconfig_1.defaultLoadersSync,
|
|
44
|
-
".ts": tsLoader,
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Searches for `next-logger.config.{ts,js,cjs,...}` from cwd upward.
|
|
48
|
-
*
|
|
49
|
-
* Always returns a value: either a built consola instance (when the config
|
|
50
|
-
* supplied one or a factory) or the options object to build from (the config's
|
|
51
|
-
* partial options merged with defaults, or the bare defaults when no config
|
|
52
|
-
* exists).
|
|
53
|
-
*/
|
|
54
|
-
function loadConfig() {
|
|
55
|
-
const explorer = (0, lilconfig_1.lilconfigSync)("next-logger", {
|
|
56
|
-
searchPlaces: [
|
|
57
|
-
"next-logger.config.ts",
|
|
58
|
-
"next-logger.config.js",
|
|
59
|
-
"next-logger.config.cjs",
|
|
60
|
-
".next-loggerrc.ts",
|
|
61
|
-
".next-loggerrc.js",
|
|
62
|
-
".next-loggerrc.cjs",
|
|
63
|
-
".next-loggerrc",
|
|
64
|
-
".next-loggerrc.json",
|
|
65
|
-
"package.json",
|
|
66
|
-
],
|
|
67
|
-
loaders,
|
|
68
|
-
});
|
|
69
|
-
const result = explorer.search();
|
|
70
|
-
const raw = result?.config;
|
|
14
|
+
function resolveLoggerConfig(raw) {
|
|
71
15
|
const def = raw?.consola;
|
|
72
|
-
// No config, or config without a `consola` key → bare defaults.
|
|
73
16
|
if (def == null) {
|
|
74
17
|
return { kind: "options", options: defaults_1.defaultConsolaOptions };
|
|
75
18
|
}
|
|
76
|
-
// Factory — caller receives the library defaults, returns a consola.
|
|
77
19
|
if (typeof def === "function") {
|
|
78
20
|
const factory = def;
|
|
79
21
|
return { kind: "instance", instance: factory(defaults_1.defaultConsolaOptions) };
|
|
80
22
|
}
|
|
81
|
-
// Consola instance — use as-is (type guard narrows safely).
|
|
82
23
|
if ((0, types_1.isConsolaInstance)(def)) {
|
|
83
24
|
return { kind: "instance", instance: def };
|
|
84
25
|
}
|
|
85
|
-
// Partial options object — merge with defaults (formatOptions nested).
|
|
86
|
-
const extra = def;
|
|
87
26
|
return {
|
|
88
27
|
kind: "options",
|
|
89
|
-
options: mergeOptions(
|
|
28
|
+
options: mergeOptions(def),
|
|
90
29
|
};
|
|
91
30
|
}
|
|
92
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Reads the `logger` config delivered by {@link withLogger} via the
|
|
33
|
+
* `NEXT_LOGGER_CONFIG` env var. Falls back to the bare defaults when the var is
|
|
34
|
+
* absent or unparseable.
|
|
35
|
+
*
|
|
36
|
+
* The access is a LITERAL `process.env.NEXT_LOGGER_CONFIG` (not computed via
|
|
37
|
+
* the {@link CONFIG_ENV_VAR} constant) so that Next.js' build-time `env`
|
|
38
|
+
* inlining (DefinePlugin) substitutes the value into the instrumentation
|
|
39
|
+
* bundle — a computed `process.env[const]` reference would NOT be inlined and
|
|
40
|
+
* would read `undefined` at runtime.
|
|
41
|
+
*
|
|
42
|
+
* Sync and free of any `next` dependency.
|
|
43
|
+
*/
|
|
44
|
+
function loadConfig() {
|
|
45
|
+
const json = process.env.NEXT_LOGGER_CONFIG;
|
|
46
|
+
if (!json)
|
|
47
|
+
return resolveLoggerConfig(undefined);
|
|
48
|
+
try {
|
|
49
|
+
return resolveLoggerConfig(JSON.parse(json));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return resolveLoggerConfig(undefined);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
93
55
|
function mergeOptions(extra) {
|
|
94
56
|
return {
|
|
95
57
|
...defaults_1.defaultConsolaOptions,
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;;AAmCA,kDAmBC;AAeD,gCAQC;AA5ED,yCAAmD;AACnD,mCAA4C;AA0B5C,wEAAwE;AAC3D,QAAA,cAAc,GAAG,oBAAoB,CAAC;AAEnD;;;GAGG;AACH,SAAgB,mBAAmB,CACjC,GAAiC;IAEjC,MAAM,GAAG,GAAG,GAAG,EAAE,OAAO,CAAC;IAEzB,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;QAChB,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,gCAAqB,EAAE,CAAC;IAC7D,CAAC;IACD,IAAI,OAAO,GAAG,KAAK,UAAU,EAAE,CAAC;QAC9B,MAAM,OAAO,GAAG,GAA6D,CAAC;QAC9E,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,OAAO,CAAC,gCAAqB,CAAC,EAAE,CAAC;IACxE,CAAC;IACD,IAAI,IAAA,yBAAiB,EAAC,GAAG,CAAC,EAAE,CAAC;QAC3B,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,EAAE,CAAC;IAC7C,CAAC;IACD,OAAO;QACL,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,YAAY,CAAC,GAA8B,CAAC;KACtD,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,UAAU;IACxB,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;IAC5C,IAAI,CAAC,IAAI;QAAE,OAAO,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACjD,IAAI,CAAC;QACH,OAAO,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAqB,CAAC,CAAC;IACnE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,mBAAmB,CAAC,SAAS,CAAC,CAAC;IACxC,CAAC;AACH,CAAC;AAED,SAAS,YAAY,CAAC,KAA8B;IAClD,OAAO;QACL,GAAG,gCAAqB;QACxB,GAAG,KAAK;QACR,aAAa,EAAE;YACb,GAAG,gCAAqB,CAAC,aAAa;YACtC,GAAG,KAAK,CAAC,aAAa;SACvB;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,86 +1,67 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* # @vsfedorenko/next-logger
|
|
3
3
|
*
|
|
4
|
-
* A universal logging kit for Next.js.
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* custom Next.js server.
|
|
4
|
+
* A universal logging kit for Next.js. Wraps the global `console.*` (which
|
|
5
|
+
* Next.js' own internal logger also funnels through) so all diagnostic output
|
|
6
|
+
* flows through a single level-controllable consola sink — without monkey
|
|
7
|
+
* patching Next's module (which is unreachable under Turbopack).
|
|
9
8
|
*
|
|
10
9
|
* ## Usage
|
|
11
10
|
*
|
|
12
|
-
*
|
|
11
|
+
* Wrap your Next config and call `init()` from instrumentation:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* // next.config.ts
|
|
15
|
+
* import { withLogger } from "@vsfedorenko/next-logger";
|
|
16
|
+
*
|
|
17
|
+
* export default withLogger({ consola: { level: 4 } })({
|
|
18
|
+
* // ...your next config
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
13
21
|
*
|
|
14
22
|
* ```ts
|
|
15
23
|
* // instrumentation.ts (project root)
|
|
16
24
|
* export async function register() {
|
|
17
25
|
* if (process.env.NEXT_RUNTIME === "nodejs") {
|
|
18
|
-
* await import("@vsfedorenko/next-logger");
|
|
26
|
+
* const { init } = await import("@vsfedorenko/next-logger");
|
|
27
|
+
* init();
|
|
19
28
|
* }
|
|
20
29
|
* }
|
|
21
30
|
* ```
|
|
22
31
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* await import("@vsfedorenko/next-logger/presets/next-only");
|
|
28
|
-
* ```
|
|
29
|
-
*
|
|
30
|
-
* ### `-r` preload
|
|
31
|
-
*
|
|
32
|
-
* ```sh
|
|
33
|
-
* node -r @vsfedorenko/next-logger server.js
|
|
34
|
-
* ```
|
|
32
|
+
* `init()` patches `console.*`. To skip patching console, pass
|
|
33
|
+
* `{ console: false }`.
|
|
35
34
|
*
|
|
36
35
|
* ## Configuration
|
|
37
36
|
*
|
|
38
|
-
*
|
|
37
|
+
* `withLogger(options)` serialises `options` into the `NEXT_LOGGER_CONFIG` env
|
|
38
|
+
* var via Next.js' validated `env` config key (no "Unrecognized key" warning),
|
|
39
|
+
* inlined at build time and read back at runtime. Only serialisable consola
|
|
40
|
+
* options are supported (level, formatOptions, …):
|
|
39
41
|
*
|
|
40
42
|
* ```ts
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* export default {
|
|
44
|
-
* consola: createConsola({ level: 4 }), // full custom instance
|
|
45
|
-
* };
|
|
46
|
-
* ```
|
|
47
|
-
*
|
|
48
|
-
* Or partial options (merged with defaults):
|
|
49
|
-
*
|
|
50
|
-
* ```ts
|
|
51
|
-
* export default {
|
|
52
|
-
* consola: { level: 4, formatOptions: { date: false } },
|
|
53
|
-
* };
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* Or a factory (receives the library's default options):
|
|
57
|
-
*
|
|
58
|
-
* ```ts
|
|
59
|
-
* import type { ConsolaOptions } from "consola";
|
|
60
|
-
*
|
|
61
|
-
* export default {
|
|
62
|
-
* consola: (defaults: Partial<ConsolaOptions>) =>
|
|
63
|
-
* createConsola({ ...defaults, level: 5 }),
|
|
64
|
-
* };
|
|
43
|
+
* withLogger({ consola: { level: 4, formatOptions: { date: false } } })
|
|
65
44
|
* ```
|
|
66
45
|
*
|
|
67
46
|
* ## Log level
|
|
68
47
|
*
|
|
69
|
-
* Without
|
|
48
|
+
* Without `withLogger`, the level resolves from (in order) `LOG_LEVEL` or
|
|
70
49
|
* `NEXT_PUBLIC_LOG_LEVEL` (numeric or named: silent/fatal/error/warn/info/log/
|
|
71
50
|
* debug/trace/verbose), falling back to `3` (info).
|
|
72
51
|
*/
|
|
73
|
-
|
|
74
|
-
export {
|
|
75
|
-
export {
|
|
52
|
+
export { withLogger } from "./withLogger";
|
|
53
|
+
export type { LoggerPluginOptions } from "./withLogger";
|
|
54
|
+
export { init, getLogger } from "./init";
|
|
55
|
+
export type { InitOptions } from "./init";
|
|
56
|
+
export { buildLogger } from "./logger";
|
|
57
|
+
export { loadConfig, resolveLoggerConfig, CONFIG_ENV_VAR } from "./config";
|
|
76
58
|
export type { NextLoggerConfig, ResolvedConfig } from "./config";
|
|
77
59
|
export { defaultConsolaOptions, resolveFormat } from "./defaults";
|
|
78
60
|
export type { LogFormat } from "./defaults";
|
|
79
61
|
export { createJsonReporter } from "./reporters/json";
|
|
80
|
-
export { patchNext, routeNextMethod, NEXT_PREFIXES } from "./patches/next";
|
|
81
|
-
export type { NextMethodName } from "./patches/next";
|
|
82
62
|
export { patchConsole, routeConsoleMethod, CONSOLE_METHODS } from "./patches/console";
|
|
83
63
|
export type { ConsoleMethodName } from "./patches/console";
|
|
64
|
+
export { isNextLog } from "./patches/next";
|
|
84
65
|
export { isEmptyMessage, skipEmpty } from "./patches/util";
|
|
85
66
|
export type { LogFunction, NextLogFn, NextLogModule } from "./types";
|
|
86
67
|
export type { ConsolaInstance, ConsolaOptions, ConsolaReporter, FormatOptions, InputLogObject, LogLevel, LogObject, LogType, } from "consola";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAGH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,YAAY,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAGxD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzC,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAG1C,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,UAAU,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC3E,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAClE,YAAY,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAG5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAGtD,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACtF,YAAY,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAG3D,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAIrE,YAAY,EACV,eAAe,EACf,cAAc,EACd,eAAe,EACf,aAAa,EACb,cAAc,EACd,QAAQ,EACR,SAAS,EACT,OAAO,GACR,MAAM,SAAS,CAAC"}
|