@decohere/scribe-pino 0.0.1-dev.12 → 0.0.1-dev.120
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/commonjs/src/index.js +1 -2
- package/commonjs/src/pino.adapater.js +130 -7
- package/commonjs/src/pino.recipe.js +106 -0
- package/commonjs/src/pino.transport-guard.js +68 -0
- package/commonjs/src/scribe-pino.js +132 -31
- package/es6/src/index.js +1 -2
- package/es6/src/pino.adapater.js +131 -8
- package/es6/src/pino.recipe.js +96 -0
- package/es6/src/pino.transport-guard.js +61 -0
- package/es6/src/scribe-pino.js +114 -28
- package/package.json +16 -4
- package/types/src/index.d.ts +1 -0
- package/types/src/pino.adapater.d.ts +8 -0
- package/types/src/pino.recipe.d.ts +45 -0
- package/types/src/pino.transport-guard.d.ts +10 -0
- package/types/src/scribe-pino.d.ts +56 -8
package/es6/src/pino.adapater.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { LogAdapter } from "@decohere/scribe";
|
|
1
|
+
import { formatError, LogAdapter } from "@decohere/scribe";
|
|
2
2
|
import pino from "pino";
|
|
3
3
|
export class PinoAdapter extends LogAdapter {
|
|
4
4
|
_pino;
|
|
5
5
|
options;
|
|
6
6
|
stream;
|
|
7
|
+
disposed = false;
|
|
8
|
+
retired = false;
|
|
7
9
|
constructor(options, stream){
|
|
8
10
|
super();
|
|
9
11
|
this.options = options;
|
|
@@ -33,18 +35,136 @@ export class PinoAdapter extends LogAdapter {
|
|
|
33
35
|
return logger;
|
|
34
36
|
}
|
|
35
37
|
flush(cb) {
|
|
38
|
+
if (this.retired || !this._pino) {
|
|
39
|
+
cb?.();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
36
42
|
this._pino.flush(cb);
|
|
37
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Immediately stop accepting writes into the destination (ThreadStream).
|
|
46
|
+
* Does not wait for flush — use `stop()` for async drain after retire.
|
|
47
|
+
*/ retire() {
|
|
48
|
+
if (this.retired) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.retired = true;
|
|
52
|
+
const guarded = this.stream;
|
|
53
|
+
if (typeof guarded?.retire === "function") {
|
|
54
|
+
guarded.retire("adapter retired");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
destroyDestination(this.stream);
|
|
58
|
+
}
|
|
59
|
+
async [Symbol.asyncDispose]() {
|
|
60
|
+
if (this.disposed) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this.disposed = true;
|
|
64
|
+
if (!this.retired) {
|
|
65
|
+
const dest = this.stream;
|
|
66
|
+
if (dest != null && typeof dest.end === "function") {
|
|
67
|
+
// pino.transport() is a ThreadStream: pino.flush() → Atomics.waitAsync can
|
|
68
|
+
// hang forever after concurrent writes. end() flushSyncs then signals
|
|
69
|
+
// finish/close (it does not accept a Node Writable callback).
|
|
70
|
+
await endDestination(dest);
|
|
71
|
+
} else if (this._pino) {
|
|
72
|
+
await new Promise((resolve, reject)=>{
|
|
73
|
+
try {
|
|
74
|
+
this._pino.flush((err)=>{
|
|
75
|
+
if (err != null) {
|
|
76
|
+
reject(err);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
resolve();
|
|
80
|
+
});
|
|
81
|
+
} catch (err) {
|
|
82
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// Mark dead after graceful end (or immediately if already force-retired).
|
|
88
|
+
this.retire();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function destroyDestination(stream) {
|
|
92
|
+
const destroyable = stream;
|
|
93
|
+
if (typeof destroyable?.destroy === "function") {
|
|
94
|
+
try {
|
|
95
|
+
destroyable.destroy();
|
|
96
|
+
} catch {
|
|
97
|
+
// already dead
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Drain a pino destination.
|
|
103
|
+
*
|
|
104
|
+
* Node `Writable.end(cb)` invokes `cb`. `pino.transport()` returns a
|
|
105
|
+
* ThreadStream whose `end()` takes no callback and signals via `finish`/`close`
|
|
106
|
+
* instead — waiting only on `end(cb)` hangs forever and blocks Flux HMR.
|
|
107
|
+
*/ function endDestination(stream) {
|
|
108
|
+
if (typeof stream.end !== "function") {
|
|
109
|
+
return Promise.resolve();
|
|
110
|
+
}
|
|
111
|
+
if (stream.destroyed || stream.closed || stream.writableFinished) {
|
|
112
|
+
return Promise.resolve();
|
|
113
|
+
}
|
|
114
|
+
return new Promise((resolve, reject)=>{
|
|
115
|
+
let settled = false;
|
|
116
|
+
const settle = (err)=>{
|
|
117
|
+
if (settled) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
settled = true;
|
|
121
|
+
stream.off?.("finish", onFinish);
|
|
122
|
+
stream.off?.("close", onClose);
|
|
123
|
+
stream.off?.("error", onError);
|
|
124
|
+
if (err != null) {
|
|
125
|
+
reject(err);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
resolve();
|
|
129
|
+
};
|
|
130
|
+
const onFinish = ()=>settle();
|
|
131
|
+
const onClose = ()=>settle();
|
|
132
|
+
const onError = (err)=>{
|
|
133
|
+
// ThreadStream may emit error then close during teardown; closed = done.
|
|
134
|
+
if (stream.destroyed || stream.closed) {
|
|
135
|
+
settle();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
settle(err);
|
|
139
|
+
};
|
|
140
|
+
stream.once?.("finish", onFinish);
|
|
141
|
+
stream.once?.("close", onClose);
|
|
142
|
+
stream.once?.("error", onError);
|
|
143
|
+
try {
|
|
144
|
+
stream.end((err)=>settle(err));
|
|
145
|
+
} catch (err) {
|
|
146
|
+
settle(err instanceof Error ? err : new Error(String(err)));
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// ThreadStream.end() is a no-op when already destroyed (no events).
|
|
150
|
+
if (stream.destroyed || stream.closed || stream.writableFinished) {
|
|
151
|
+
settle();
|
|
152
|
+
}
|
|
153
|
+
});
|
|
38
154
|
}
|
|
39
155
|
function logMethod(args, method) {
|
|
40
156
|
let message = args.at(0);
|
|
41
157
|
const data = args.at(1);
|
|
158
|
+
if (message instanceof Error) {
|
|
159
|
+
method.call(this, {
|
|
160
|
+
err: message
|
|
161
|
+
}, formatError(message));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
42
164
|
if (data instanceof Error) {
|
|
43
165
|
message = {
|
|
44
166
|
msg: message,
|
|
45
|
-
|
|
46
|
-
error: data
|
|
47
|
-
}
|
|
167
|
+
error: data
|
|
48
168
|
};
|
|
49
169
|
} else if (typeof data === "object") {
|
|
50
170
|
message = {
|
|
@@ -59,15 +179,18 @@ function logMethod(args, method) {
|
|
|
59
179
|
}
|
|
60
180
|
method.call(this, message);
|
|
61
181
|
}
|
|
62
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Add Scribe's logger members to a Pino logger. `spawn` is Pino's own `child`:
|
|
184
|
+
* a Pino child inherits its parent's members through the prototype, so
|
|
185
|
+
* `logger.child` would be the parent's override.
|
|
186
|
+
*/ function pinoMix(logger, spawn = logger.child) {
|
|
63
187
|
return Object.assign(logger, {
|
|
64
188
|
get currentLevel () {
|
|
65
189
|
return logger.level;
|
|
66
190
|
},
|
|
67
191
|
get logLevels () {
|
|
68
192
|
return logger.levels.values;
|
|
69
|
-
}
|
|
193
|
+
},
|
|
194
|
+
child: (fields)=>pinoMix(spawn.call(logger, fields), spawn)
|
|
70
195
|
});
|
|
71
196
|
}
|
|
72
|
-
|
|
73
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9ob21lL3J1bm5lci93b3JrL2RlY29oZXJlL2RlY29oZXJlL3Byb2plY3RzL3NjcmliZS9zY3JpYmUtcGluby9zcmMvcGluby5hZGFwYXRlci50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJpbXB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TUFBTTtRQUM1QjtJQUNEO0FBQ0QifQ==
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Environments } from "@decohere/core/system";
|
|
2
|
+
const DEFAULT_ROLL_SIZE = "10m";
|
|
3
|
+
const DEFAULT_ROLL_FREQUENCY = "daily";
|
|
4
|
+
const DEFAULT_ROLL_KEEP = 14;
|
|
5
|
+
const ENV_LOG_FILE = "SCRIBE_LOG_FILE";
|
|
6
|
+
const ENV_LOG_SIZE = "SCRIBE_LOG_SIZE";
|
|
7
|
+
const ENV_LOG_FREQUENCY = "SCRIBE_LOG_FREQUENCY";
|
|
8
|
+
const ENV_LOG_KEEP = "SCRIBE_LOG_KEEP";
|
|
9
|
+
function currentEnvironment() {
|
|
10
|
+
return process.env.NODE_ENV === Environments.production ? Environments.production : Environments.development;
|
|
11
|
+
}
|
|
12
|
+
function isEnabled(when, environment) {
|
|
13
|
+
return (when ?? "development") === "always" || environment === Environments.development;
|
|
14
|
+
}
|
|
15
|
+
function envString(name) {
|
|
16
|
+
const value = process.env[name]?.trim();
|
|
17
|
+
return value ? value : undefined;
|
|
18
|
+
}
|
|
19
|
+
function envFrequency() {
|
|
20
|
+
const raw = envString(ENV_LOG_FREQUENCY);
|
|
21
|
+
if (raw == null) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (/^\d+$/.test(raw)) {
|
|
25
|
+
return Number(raw);
|
|
26
|
+
}
|
|
27
|
+
return raw;
|
|
28
|
+
}
|
|
29
|
+
function envKeep() {
|
|
30
|
+
const raw = envString(ENV_LOG_KEEP);
|
|
31
|
+
if (raw == null) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const keep = Number(raw);
|
|
35
|
+
return Number.isFinite(keep) && keep >= 0 ? keep : undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Resolve roll options. `SCRIBE_LOG_FILE` overrides `file` so ops can redirect
|
|
39
|
+
* without touching app code. Size / frequency / keep: explicit > env > default.
|
|
40
|
+
*/ function resolveRoll(options) {
|
|
41
|
+
return {
|
|
42
|
+
file: envString(ENV_LOG_FILE) ?? options.file,
|
|
43
|
+
size: options.size ?? envString(ENV_LOG_SIZE) ?? DEFAULT_ROLL_SIZE,
|
|
44
|
+
frequency: options.frequency ?? envFrequency() ?? DEFAULT_ROLL_FREQUENCY,
|
|
45
|
+
keep: options.keep ?? envKeep() ?? DEFAULT_ROLL_KEEP,
|
|
46
|
+
mkdir: options.mkdir ?? true,
|
|
47
|
+
symlink: options.symlink ?? false
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function prettyTarget(options) {
|
|
51
|
+
return {
|
|
52
|
+
target: "pino-pretty",
|
|
53
|
+
options: {
|
|
54
|
+
colorize: options.colorize ?? true
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function rollTarget(options) {
|
|
59
|
+
const roll = resolveRoll(options);
|
|
60
|
+
return {
|
|
61
|
+
target: "pino-roll",
|
|
62
|
+
options: {
|
|
63
|
+
file: roll.file,
|
|
64
|
+
mkdir: roll.mkdir,
|
|
65
|
+
size: roll.size,
|
|
66
|
+
frequency: roll.frequency,
|
|
67
|
+
limit: {
|
|
68
|
+
count: roll.keep
|
|
69
|
+
},
|
|
70
|
+
symlink: roll.symlink
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function transport(plan, environment = currentEnvironment()) {
|
|
75
|
+
const targets = [];
|
|
76
|
+
if (plan.pretty && isEnabled(plan.pretty.when, environment)) {
|
|
77
|
+
targets.push(prettyTarget(plan.pretty));
|
|
78
|
+
}
|
|
79
|
+
if (plan.roll && isEnabled(plan.roll.when, environment)) {
|
|
80
|
+
targets.push(rollTarget(plan.roll));
|
|
81
|
+
}
|
|
82
|
+
if (targets.length === 0) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
if (targets.length === 1) {
|
|
86
|
+
return targets[0];
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
targets
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
export const pinoRecipe = {
|
|
93
|
+
transport,
|
|
94
|
+
currentEnvironment,
|
|
95
|
+
resolveRoll
|
|
96
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wrap a pino worker transport so a dead ThreadStream cannot spam or crash.
|
|
3
|
+
* On first error / write throw: report once, destroy, no-op further writes.
|
|
4
|
+
*/ export function guardTransport(transport) {
|
|
5
|
+
const stream = transport;
|
|
6
|
+
let retired = false;
|
|
7
|
+
let reported = false;
|
|
8
|
+
const retire = (reason = "retired")=>{
|
|
9
|
+
if (retired) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
retired = true;
|
|
13
|
+
if (!reported) {
|
|
14
|
+
reported = true;
|
|
15
|
+
process.stderr.write(`[scribe-pino] transport retired: ${reason}\n`);
|
|
16
|
+
}
|
|
17
|
+
if (typeof stream.destroy === "function") {
|
|
18
|
+
try {
|
|
19
|
+
stream.destroy();
|
|
20
|
+
} catch {
|
|
21
|
+
// already dead
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
if (typeof stream.on === "function") {
|
|
26
|
+
stream.on("error", (err)=>{
|
|
27
|
+
retire(err?.message ?? String(err));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
if (typeof stream.write === "function") {
|
|
31
|
+
const originalWrite = stream.write.bind(stream);
|
|
32
|
+
stream.write = (chunk, encodingOrCb, cb)=>{
|
|
33
|
+
if (retired) {
|
|
34
|
+
completeWrite(encodingOrCb, cb);
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
return originalWrite(chunk, encodingOrCb, cb);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
retire(err instanceof Error ? err.message : String(err));
|
|
41
|
+
completeWrite(encodingOrCb, cb);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
Object.defineProperty(stream, "retired", {
|
|
47
|
+
get: ()=>retired,
|
|
48
|
+
enumerable: true
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(stream, "retire", {
|
|
51
|
+
value: retire,
|
|
52
|
+
enumerable: true
|
|
53
|
+
});
|
|
54
|
+
return stream;
|
|
55
|
+
}
|
|
56
|
+
function completeWrite(encodingOrCb, cb) {
|
|
57
|
+
const done = typeof encodingOrCb === "function" ? encodingOrCb : typeof cb === "function" ? cb : undefined;
|
|
58
|
+
if (typeof done === "function") {
|
|
59
|
+
done();
|
|
60
|
+
}
|
|
61
|
+
}
|
package/es6/src/scribe-pino.js
CHANGED
|
@@ -7,33 +7,42 @@ function _ts_decorate(decorators, target, key, desc) {
|
|
|
7
7
|
function _ts_metadata(k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
}
|
|
10
|
-
import { extension } from "@decohere/
|
|
11
|
-
import { DefaultEnvironment } from "@decohere/
|
|
10
|
+
import { extension } from "@decohere/core/extensibility";
|
|
11
|
+
import { DefaultEnvironment } from "@decohere/core/system";
|
|
12
12
|
import { LoggingConfigurationError, scribe } from "@decohere/scribe";
|
|
13
|
+
import pino from "pino";
|
|
13
14
|
import { PinoAdapter } from "./pino.adapater.js";
|
|
15
|
+
import { pinoRecipe } from "./pino.recipe.js";
|
|
16
|
+
import { guardTransport } from "./pino.transport-guard.js";
|
|
17
|
+
export { guardTransport } from "./pino.transport-guard.js";
|
|
14
18
|
export class PinoExtensions {
|
|
15
19
|
static withPino(factory, config, logLevel) {
|
|
16
|
-
return configureScribe(factory, {
|
|
20
|
+
return configureScribe(factory, logLevel != null ? {
|
|
17
21
|
logLevel
|
|
18
|
-
}, config);
|
|
22
|
+
} : undefined, config);
|
|
19
23
|
}
|
|
20
|
-
static withPinoPretty(factory,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
static withPinoPretty(factory, logLevelOrOptions) {
|
|
25
|
+
const pretty = parsePrettyArg(logLevelOrOptions);
|
|
26
|
+
return attachRecipe(factory, {
|
|
27
|
+
pretty,
|
|
28
|
+
logLevel: pretty.logLevel
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
static withPinoRoll(factory, options) {
|
|
32
|
+
return attachRecipe(factory, {
|
|
33
|
+
roll: options,
|
|
34
|
+
logLevel: options.logLevel
|
|
35
|
+
});
|
|
29
36
|
}
|
|
30
37
|
static withPinoCustom(factory, options) {
|
|
31
|
-
const { scribe, transports } = options;
|
|
38
|
+
const { scribe, transports } = options ?? {};
|
|
32
39
|
return configureScribe(factory, scribe, transports);
|
|
33
40
|
}
|
|
34
41
|
}
|
|
35
42
|
_ts_decorate([
|
|
36
|
-
extension(scribe
|
|
43
|
+
extension(scribe, {
|
|
44
|
+
static: true
|
|
45
|
+
}),
|
|
37
46
|
_ts_metadata("design:type", Function),
|
|
38
47
|
_ts_metadata("design:paramtypes", [
|
|
39
48
|
typeof scribe === "undefined" ? Object : scribe,
|
|
@@ -43,7 +52,9 @@ _ts_decorate([
|
|
|
43
52
|
_ts_metadata("design:returntype", void 0)
|
|
44
53
|
], PinoExtensions, "withPino", null);
|
|
45
54
|
_ts_decorate([
|
|
46
|
-
extension(scribe
|
|
55
|
+
extension(scribe, {
|
|
56
|
+
static: true
|
|
57
|
+
}),
|
|
47
58
|
_ts_metadata("design:type", Function),
|
|
48
59
|
_ts_metadata("design:paramtypes", [
|
|
49
60
|
typeof scribe === "undefined" ? Object : scribe,
|
|
@@ -52,7 +63,20 @@ _ts_decorate([
|
|
|
52
63
|
_ts_metadata("design:returntype", void 0)
|
|
53
64
|
], PinoExtensions, "withPinoPretty", null);
|
|
54
65
|
_ts_decorate([
|
|
55
|
-
extension(scribe
|
|
66
|
+
extension(scribe, {
|
|
67
|
+
static: true
|
|
68
|
+
}),
|
|
69
|
+
_ts_metadata("design:type", Function),
|
|
70
|
+
_ts_metadata("design:paramtypes", [
|
|
71
|
+
typeof scribe === "undefined" ? Object : scribe,
|
|
72
|
+
typeof PinoRollRecipeOptions === "undefined" ? Object : PinoRollRecipeOptions
|
|
73
|
+
]),
|
|
74
|
+
_ts_metadata("design:returntype", void 0)
|
|
75
|
+
], PinoExtensions, "withPinoRoll", null);
|
|
76
|
+
_ts_decorate([
|
|
77
|
+
extension(scribe, {
|
|
78
|
+
static: true
|
|
79
|
+
}),
|
|
56
80
|
_ts_metadata("design:type", Function),
|
|
57
81
|
_ts_metadata("design:paramtypes", [
|
|
58
82
|
typeof scribe === "undefined" ? Object : scribe,
|
|
@@ -60,23 +84,85 @@ _ts_decorate([
|
|
|
60
84
|
]),
|
|
61
85
|
_ts_metadata("design:returntype", void 0)
|
|
62
86
|
], PinoExtensions, "withPinoCustom", null);
|
|
87
|
+
function parsePrettyArg(logLevelOrOptions) {
|
|
88
|
+
if (logLevelOrOptions == null) {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
if (typeof logLevelOrOptions === "string") {
|
|
92
|
+
return {
|
|
93
|
+
logLevel: logLevelOrOptions
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return logLevelOrOptions;
|
|
97
|
+
}
|
|
98
|
+
function attachRecipe(factory, plan) {
|
|
99
|
+
const loggerFactory = applyRecipe(factory, plan);
|
|
100
|
+
const chain = {
|
|
101
|
+
withPinoPretty (logLevelOrOptions) {
|
|
102
|
+
const pretty = parsePrettyArg(logLevelOrOptions);
|
|
103
|
+
return attachRecipe(factory, {
|
|
104
|
+
...plan,
|
|
105
|
+
pretty: {
|
|
106
|
+
...plan.pretty,
|
|
107
|
+
...pretty
|
|
108
|
+
},
|
|
109
|
+
logLevel: pretty.logLevel ?? plan.logLevel
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
withPinoRoll (options) {
|
|
113
|
+
return attachRecipe(factory, {
|
|
114
|
+
...plan,
|
|
115
|
+
roll: options,
|
|
116
|
+
logLevel: options.logLevel ?? plan.logLevel
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
return Object.assign(loggerFactory, chain, {
|
|
121
|
+
and: chain
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Guard a worker transport so failures (roll EEXIST, worker exit) neither
|
|
126
|
+
* crash the process nor spam stderr on every subsequent log write.
|
|
127
|
+
*/ function createSafeTransport(options) {
|
|
128
|
+
return guardTransport(pino.transport(options));
|
|
129
|
+
}
|
|
130
|
+
function adapterForTransport(transport) {
|
|
131
|
+
if (transport == null) {
|
|
132
|
+
return new PinoAdapter({});
|
|
133
|
+
}
|
|
134
|
+
return new PinoAdapter({}, createSafeTransport(transport));
|
|
135
|
+
}
|
|
136
|
+
function applyRecipe(factory, plan) {
|
|
137
|
+
const adapter = adapterForTransport(pinoRecipe.transport(plan));
|
|
138
|
+
// Omit logLevel when unset so mergeDefaults can read LOG_LEVEL / SCRIBE_LOG_LEVEL.
|
|
139
|
+
return factory.configure({
|
|
140
|
+
options: plan.logLevel != null ? {
|
|
141
|
+
logLevel: plan.logLevel
|
|
142
|
+
} : {},
|
|
143
|
+
adapter
|
|
144
|
+
});
|
|
145
|
+
}
|
|
63
146
|
function configureScribe(factory, options, config) {
|
|
64
147
|
const environment = process.env.NODE_ENV ?? DefaultEnvironment;
|
|
65
|
-
const configs = Array.isArray(config) ? config : [
|
|
148
|
+
const configs = (Array.isArray(config) ? config : config != null ? [
|
|
66
149
|
config
|
|
67
|
-
];
|
|
68
|
-
|
|
69
|
-
if (transports.length > 1) {
|
|
150
|
+
] : []).filter((item)=>item.environment == null || item.environment === environment);
|
|
151
|
+
if (configs.length > 1) {
|
|
70
152
|
throw new LoggingConfigurationError(`There should only be one configuration per environment for logging`);
|
|
71
153
|
}
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
transport
|
|
75
|
-
});
|
|
154
|
+
const adapter = adapterForTransport(configs.at(0)?.transports);
|
|
155
|
+
// Do not force DefaultLogLevel — that would shadow env resolution in mergeDefaults.
|
|
76
156
|
return factory.configure({
|
|
77
|
-
options,
|
|
157
|
+
options: options ?? {},
|
|
78
158
|
adapter
|
|
79
159
|
});
|
|
80
160
|
}
|
|
81
|
-
|
|
82
|
-
|
|
161
|
+
/** @deprecated Use {@link guardTransport} — kept for callers/tests from the silence-only era. */ function silenceErrors(transport) {
|
|
162
|
+
return guardTransport(transport);
|
|
163
|
+
}
|
|
164
|
+
export const pinoTransport = {
|
|
165
|
+
createSafeTransport,
|
|
166
|
+
guardTransport,
|
|
167
|
+
silenceErrors
|
|
168
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decohere/scribe-pino",
|
|
3
|
-
"version": "0.0.1-dev.
|
|
3
|
+
"version": "0.0.1-dev.120",
|
|
4
4
|
"description": "Pino adapter for @decohere/scribe",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -23,9 +23,21 @@
|
|
|
23
23
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@decohere/
|
|
27
|
-
"@decohere/scribe": "0.0.1-dev.
|
|
26
|
+
"@decohere/core": "0.0.1-dev.120",
|
|
27
|
+
"@decohere/scribe": "0.0.1-dev.120",
|
|
28
28
|
"pino": "^10.3.0"
|
|
29
29
|
},
|
|
30
|
-
"devDependencies": {}
|
|
30
|
+
"devDependencies": {},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"pino-pretty": "^13.0.0",
|
|
33
|
+
"pino-roll": "^4.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependenciesMeta": {
|
|
36
|
+
"pino-pretty": {
|
|
37
|
+
"optional": true
|
|
38
|
+
},
|
|
39
|
+
"pino-roll": {
|
|
40
|
+
"optional": true
|
|
41
|
+
}
|
|
42
|
+
}
|
|
31
43
|
}
|
package/types/src/index.d.ts
CHANGED
|
@@ -8,8 +8,16 @@ export declare class PinoAdapter extends LogAdapter {
|
|
|
8
8
|
private _pino;
|
|
9
9
|
private readonly options;
|
|
10
10
|
private readonly stream;
|
|
11
|
+
private disposed;
|
|
12
|
+
private retired;
|
|
11
13
|
constructor(options: pino.LoggerOptions, stream?: pino.DestinationStream);
|
|
12
14
|
configure<T extends string = never, CustomOnly extends true | false = false>(scribeOptions: ScribeOptions<T, CustomOnly>): void;
|
|
13
15
|
getLogger<T extends string = never, CustomOnly extends true | false = false>(name: string): Logger<T, CustomOnly>;
|
|
14
16
|
flush(cb?: () => void): void;
|
|
17
|
+
/**
|
|
18
|
+
* Immediately stop accepting writes into the destination (ThreadStream).
|
|
19
|
+
* Does not wait for flush — use `stop()` for async drain after retire.
|
|
20
|
+
*/
|
|
21
|
+
retire(): void;
|
|
22
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
15
23
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type Environment } from "@decohere/core/system";
|
|
2
|
+
import type { TransportMultiOptions, TransportSingleOptions } from "pino";
|
|
3
|
+
/** Pretty and roll are local-dev conveniences. Production ships JSON on stdout. */
|
|
4
|
+
export type PinoWhen = "development" | "always";
|
|
5
|
+
export type PinoPrettyRecipeOptions = {
|
|
6
|
+
colorize?: boolean;
|
|
7
|
+
logLevel?: string;
|
|
8
|
+
when?: PinoWhen;
|
|
9
|
+
};
|
|
10
|
+
export type PinoRollRecipeOptions = {
|
|
11
|
+
/** Default roll path. Overridden at runtime by `SCRIBE_LOG_FILE` when set. */
|
|
12
|
+
file: string;
|
|
13
|
+
size?: string;
|
|
14
|
+
frequency?: string | number;
|
|
15
|
+
keep?: number;
|
|
16
|
+
mkdir?: boolean;
|
|
17
|
+
symlink?: boolean;
|
|
18
|
+
logLevel?: string;
|
|
19
|
+
when?: PinoWhen;
|
|
20
|
+
};
|
|
21
|
+
export type PinoRecipePlan = {
|
|
22
|
+
pretty?: PinoPrettyRecipeOptions;
|
|
23
|
+
roll?: PinoRollRecipeOptions;
|
|
24
|
+
logLevel?: string;
|
|
25
|
+
};
|
|
26
|
+
declare function currentEnvironment(): Environment;
|
|
27
|
+
/**
|
|
28
|
+
* Resolve roll options. `SCRIBE_LOG_FILE` overrides `file` so ops can redirect
|
|
29
|
+
* without touching app code. Size / frequency / keep: explicit > env > default.
|
|
30
|
+
*/
|
|
31
|
+
declare function resolveRoll(options: PinoRollRecipeOptions): {
|
|
32
|
+
file: string;
|
|
33
|
+
size: string;
|
|
34
|
+
frequency: string | number;
|
|
35
|
+
keep: number;
|
|
36
|
+
mkdir: boolean;
|
|
37
|
+
symlink: boolean;
|
|
38
|
+
};
|
|
39
|
+
declare function transport(plan: PinoRecipePlan, environment?: Environment): TransportSingleOptions | TransportMultiOptions | undefined;
|
|
40
|
+
export declare const pinoRecipe: {
|
|
41
|
+
transport: typeof transport;
|
|
42
|
+
currentEnvironment: typeof currentEnvironment;
|
|
43
|
+
resolveRoll: typeof resolveRoll;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DestinationStream } from "pino";
|
|
2
|
+
export type GuardedTransport = DestinationStream & {
|
|
3
|
+
readonly retired: boolean;
|
|
4
|
+
retire(reason?: string): void;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Wrap a pino worker transport so a dead ThreadStream cannot spam or crash.
|
|
8
|
+
* On first error / write throw: report once, destroy, no-op further writes.
|
|
9
|
+
*/
|
|
10
|
+
export declare function guardTransport(transport: DestinationStream): GuardedTransport;
|