@decohere/scribe-pino 0.0.1-dev.10 → 0.0.1-dev.100
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 +123 -5
- package/commonjs/src/pino.recipe.js +106 -0
- package/commonjs/src/pino.transport-guard.js +68 -0
- package/commonjs/src/scribe-pino.js +130 -29
- package/es6/src/index.js +1 -2
- package/es6/src/pino.adapater.js +124 -6
- package/es6/src/pino.recipe.js +96 -0
- package/es6/src/pino.transport-guard.js +61 -0
- package/es6/src/scribe-pino.js +112 -26
- 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 +55 -7
package/commonjs/src/index.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
5
|
_export_star(require("./pino.adapater.js"), exports);
|
|
6
|
+
_export_star(require("./pino.recipe.js"), exports);
|
|
6
7
|
_export_star(require("./scribe-pino.js"), exports);
|
|
7
8
|
function _export_star(from, to) {
|
|
8
9
|
Object.keys(from).forEach(function(k) {
|
|
@@ -17,5 +18,3 @@ function _export_star(from, to) {
|
|
|
17
18
|
});
|
|
18
19
|
return from;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9ob21lL3J1bm5lci93b3JrL2RlY29oZXJlL2RlY29oZXJlL3Byb2plY3RzL3NjcmliZS9zY3JpYmUtcGluby9zcmMvaW5kZXgudHMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0ICogZnJvbSBcIi4vcGluby5hZGFwYXRlci5qc1wiO1xuZXhwb3J0ICogZnJvbSBcIi4vc2NyaWJlLXBpbm8uanNcIjtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O3FCQUFjO3FCQUNBIn0=
|
|
@@ -19,6 +19,8 @@ class PinoAdapter extends _scribe.LogAdapter {
|
|
|
19
19
|
_pino;
|
|
20
20
|
options;
|
|
21
21
|
stream;
|
|
22
|
+
disposed = false;
|
|
23
|
+
retired = false;
|
|
22
24
|
constructor(options, stream){
|
|
23
25
|
super();
|
|
24
26
|
this.options = options;
|
|
@@ -48,18 +50,136 @@ class PinoAdapter extends _scribe.LogAdapter {
|
|
|
48
50
|
return logger;
|
|
49
51
|
}
|
|
50
52
|
flush(cb) {
|
|
53
|
+
if (this.retired || !this._pino) {
|
|
54
|
+
cb?.();
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
51
57
|
this._pino.flush(cb);
|
|
52
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Immediately stop accepting writes into the destination (ThreadStream).
|
|
61
|
+
* Does not wait for flush — use `stop()` for async drain after retire.
|
|
62
|
+
*/ retire() {
|
|
63
|
+
if (this.retired) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
this.retired = true;
|
|
67
|
+
const guarded = this.stream;
|
|
68
|
+
if (typeof guarded?.retire === "function") {
|
|
69
|
+
guarded.retire("adapter retired");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
destroyDestination(this.stream);
|
|
73
|
+
}
|
|
74
|
+
async [Symbol.asyncDispose]() {
|
|
75
|
+
if (this.disposed) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.disposed = true;
|
|
79
|
+
if (!this.retired) {
|
|
80
|
+
const dest = this.stream;
|
|
81
|
+
if (dest != null && typeof dest.end === "function") {
|
|
82
|
+
// pino.transport() is a ThreadStream: pino.flush() → Atomics.waitAsync can
|
|
83
|
+
// hang forever after concurrent writes. end() flushSyncs then signals
|
|
84
|
+
// finish/close (it does not accept a Node Writable callback).
|
|
85
|
+
await endDestination(dest);
|
|
86
|
+
} else if (this._pino) {
|
|
87
|
+
await new Promise((resolve, reject)=>{
|
|
88
|
+
try {
|
|
89
|
+
this._pino.flush((err)=>{
|
|
90
|
+
if (err != null) {
|
|
91
|
+
reject(err);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
resolve();
|
|
95
|
+
});
|
|
96
|
+
} catch (err) {
|
|
97
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Mark dead after graceful end (or immediately if already force-retired).
|
|
103
|
+
this.retire();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function destroyDestination(stream) {
|
|
107
|
+
const destroyable = stream;
|
|
108
|
+
if (typeof destroyable?.destroy === "function") {
|
|
109
|
+
try {
|
|
110
|
+
destroyable.destroy();
|
|
111
|
+
} catch {
|
|
112
|
+
// already dead
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Drain a pino destination.
|
|
118
|
+
*
|
|
119
|
+
* Node `Writable.end(cb)` invokes `cb`. `pino.transport()` returns a
|
|
120
|
+
* ThreadStream whose `end()` takes no callback and signals via `finish`/`close`
|
|
121
|
+
* instead — waiting only on `end(cb)` hangs forever and blocks Flux HMR.
|
|
122
|
+
*/ function endDestination(stream) {
|
|
123
|
+
if (typeof stream.end !== "function") {
|
|
124
|
+
return Promise.resolve();
|
|
125
|
+
}
|
|
126
|
+
if (stream.destroyed || stream.closed || stream.writableFinished) {
|
|
127
|
+
return Promise.resolve();
|
|
128
|
+
}
|
|
129
|
+
return new Promise((resolve, reject)=>{
|
|
130
|
+
let settled = false;
|
|
131
|
+
const settle = (err)=>{
|
|
132
|
+
if (settled) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
settled = true;
|
|
136
|
+
stream.off?.("finish", onFinish);
|
|
137
|
+
stream.off?.("close", onClose);
|
|
138
|
+
stream.off?.("error", onError);
|
|
139
|
+
if (err != null) {
|
|
140
|
+
reject(err);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
resolve();
|
|
144
|
+
};
|
|
145
|
+
const onFinish = ()=>settle();
|
|
146
|
+
const onClose = ()=>settle();
|
|
147
|
+
const onError = (err)=>{
|
|
148
|
+
// ThreadStream may emit error then close during teardown; closed = done.
|
|
149
|
+
if (stream.destroyed || stream.closed) {
|
|
150
|
+
settle();
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
settle(err);
|
|
154
|
+
};
|
|
155
|
+
stream.once?.("finish", onFinish);
|
|
156
|
+
stream.once?.("close", onClose);
|
|
157
|
+
stream.once?.("error", onError);
|
|
158
|
+
try {
|
|
159
|
+
stream.end((err)=>settle(err));
|
|
160
|
+
} catch (err) {
|
|
161
|
+
settle(err instanceof Error ? err : new Error(String(err)));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
// ThreadStream.end() is a no-op when already destroyed (no events).
|
|
165
|
+
if (stream.destroyed || stream.closed || stream.writableFinished) {
|
|
166
|
+
settle();
|
|
167
|
+
}
|
|
168
|
+
});
|
|
53
169
|
}
|
|
54
170
|
function logMethod(args, method) {
|
|
55
171
|
let message = args.at(0);
|
|
56
172
|
const data = args.at(1);
|
|
173
|
+
if (message instanceof Error) {
|
|
174
|
+
method.call(this, {
|
|
175
|
+
err: message
|
|
176
|
+
}, (0, _scribe.formatError)(message));
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
57
179
|
if (data instanceof Error) {
|
|
58
180
|
message = {
|
|
59
181
|
msg: message,
|
|
60
|
-
|
|
61
|
-
error: data
|
|
62
|
-
}
|
|
182
|
+
error: data
|
|
63
183
|
};
|
|
64
184
|
} else if (typeof data === "object") {
|
|
65
185
|
message = {
|
|
@@ -84,5 +204,3 @@ function pinoMix(logger) {
|
|
|
84
204
|
}
|
|
85
205
|
});
|
|
86
206
|
}
|
|
87
|
-
|
|
88
|
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BLFNBQVNOLFFBQ1JELE1BQW1CO0lBRW5CLE9BQU9hLE9BQU9DLE1BQU0sQ0FBMEJkLFFBQVE7UUFDckQsSUFBSWUsZ0JBQWU7WUFDbEIsT0FBT2YsT0FBT1gsS0FBSztRQUNwQjtRQUNBLElBQUkyQixhQUFZO1lBQ2YsT0FBT2hCLE9BQU9pQixNQUFNLENBQUNDLE1BQU07UUFDNUI7SUFDRDtBQUNEIn0=
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "pinoRecipe", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return pinoRecipe;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _system = require("@decohere/nexus/system");
|
|
12
|
+
const DEFAULT_ROLL_SIZE = "10m";
|
|
13
|
+
const DEFAULT_ROLL_FREQUENCY = "daily";
|
|
14
|
+
const DEFAULT_ROLL_KEEP = 14;
|
|
15
|
+
const ENV_LOG_FILE = "SCRIBE_LOG_FILE";
|
|
16
|
+
const ENV_LOG_SIZE = "SCRIBE_LOG_SIZE";
|
|
17
|
+
const ENV_LOG_FREQUENCY = "SCRIBE_LOG_FREQUENCY";
|
|
18
|
+
const ENV_LOG_KEEP = "SCRIBE_LOG_KEEP";
|
|
19
|
+
function currentEnvironment() {
|
|
20
|
+
return process.env.NODE_ENV === _system.Environments.production ? _system.Environments.production : _system.Environments.development;
|
|
21
|
+
}
|
|
22
|
+
function isEnabled(when, environment) {
|
|
23
|
+
return (when ?? "development") === "always" || environment === _system.Environments.development;
|
|
24
|
+
}
|
|
25
|
+
function envString(name) {
|
|
26
|
+
const value = process.env[name]?.trim();
|
|
27
|
+
return value ? value : undefined;
|
|
28
|
+
}
|
|
29
|
+
function envFrequency() {
|
|
30
|
+
const raw = envString(ENV_LOG_FREQUENCY);
|
|
31
|
+
if (raw == null) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
if (/^\d+$/.test(raw)) {
|
|
35
|
+
return Number(raw);
|
|
36
|
+
}
|
|
37
|
+
return raw;
|
|
38
|
+
}
|
|
39
|
+
function envKeep() {
|
|
40
|
+
const raw = envString(ENV_LOG_KEEP);
|
|
41
|
+
if (raw == null) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const keep = Number(raw);
|
|
45
|
+
return Number.isFinite(keep) && keep >= 0 ? keep : undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolve roll options. `SCRIBE_LOG_FILE` overrides `file` so ops can redirect
|
|
49
|
+
* without touching app code. Size / frequency / keep: explicit > env > default.
|
|
50
|
+
*/ function resolveRoll(options) {
|
|
51
|
+
return {
|
|
52
|
+
file: envString(ENV_LOG_FILE) ?? options.file,
|
|
53
|
+
size: options.size ?? envString(ENV_LOG_SIZE) ?? DEFAULT_ROLL_SIZE,
|
|
54
|
+
frequency: options.frequency ?? envFrequency() ?? DEFAULT_ROLL_FREQUENCY,
|
|
55
|
+
keep: options.keep ?? envKeep() ?? DEFAULT_ROLL_KEEP,
|
|
56
|
+
mkdir: options.mkdir ?? true,
|
|
57
|
+
symlink: options.symlink ?? false
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function prettyTarget(options) {
|
|
61
|
+
return {
|
|
62
|
+
target: "pino-pretty",
|
|
63
|
+
options: {
|
|
64
|
+
colorize: options.colorize ?? true
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function rollTarget(options) {
|
|
69
|
+
const roll = resolveRoll(options);
|
|
70
|
+
return {
|
|
71
|
+
target: "pino-roll",
|
|
72
|
+
options: {
|
|
73
|
+
file: roll.file,
|
|
74
|
+
mkdir: roll.mkdir,
|
|
75
|
+
size: roll.size,
|
|
76
|
+
frequency: roll.frequency,
|
|
77
|
+
limit: {
|
|
78
|
+
count: roll.keep
|
|
79
|
+
},
|
|
80
|
+
symlink: roll.symlink
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function transport(plan, environment = currentEnvironment()) {
|
|
85
|
+
const targets = [];
|
|
86
|
+
if (plan.pretty && isEnabled(plan.pretty.when, environment)) {
|
|
87
|
+
targets.push(prettyTarget(plan.pretty));
|
|
88
|
+
}
|
|
89
|
+
if (plan.roll && isEnabled(plan.roll.when, environment)) {
|
|
90
|
+
targets.push(rollTarget(plan.roll));
|
|
91
|
+
}
|
|
92
|
+
if (targets.length === 0) {
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
if (targets.length === 1) {
|
|
96
|
+
return targets[0];
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
targets
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
const pinoRecipe = {
|
|
103
|
+
transport,
|
|
104
|
+
currentEnvironment,
|
|
105
|
+
resolveRoll
|
|
106
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "guardTransport", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return guardTransport;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
function guardTransport(transport) {
|
|
12
|
+
const stream = transport;
|
|
13
|
+
let retired = false;
|
|
14
|
+
let reported = false;
|
|
15
|
+
const retire = (reason = "retired")=>{
|
|
16
|
+
if (retired) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
retired = true;
|
|
20
|
+
if (!reported) {
|
|
21
|
+
reported = true;
|
|
22
|
+
process.stderr.write(`[scribe-pino] transport retired: ${reason}\n`);
|
|
23
|
+
}
|
|
24
|
+
if (typeof stream.destroy === "function") {
|
|
25
|
+
try {
|
|
26
|
+
stream.destroy();
|
|
27
|
+
} catch {
|
|
28
|
+
// already dead
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
if (typeof stream.on === "function") {
|
|
33
|
+
stream.on("error", (err)=>{
|
|
34
|
+
retire(err?.message ?? String(err));
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (typeof stream.write === "function") {
|
|
38
|
+
const originalWrite = stream.write.bind(stream);
|
|
39
|
+
stream.write = (chunk, encodingOrCb, cb)=>{
|
|
40
|
+
if (retired) {
|
|
41
|
+
completeWrite(encodingOrCb, cb);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return originalWrite(chunk, encodingOrCb, cb);
|
|
46
|
+
} catch (err) {
|
|
47
|
+
retire(err instanceof Error ? err.message : String(err));
|
|
48
|
+
completeWrite(encodingOrCb, cb);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
Object.defineProperty(stream, "retired", {
|
|
54
|
+
get: ()=>retired,
|
|
55
|
+
enumerable: true
|
|
56
|
+
});
|
|
57
|
+
Object.defineProperty(stream, "retire", {
|
|
58
|
+
value: retire,
|
|
59
|
+
enumerable: true
|
|
60
|
+
});
|
|
61
|
+
return stream;
|
|
62
|
+
}
|
|
63
|
+
function completeWrite(encodingOrCb, cb) {
|
|
64
|
+
const done = typeof encodingOrCb === "function" ? encodingOrCb : typeof cb === "function" ? cb : undefined;
|
|
65
|
+
if (typeof done === "function") {
|
|
66
|
+
done();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -2,16 +2,35 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", {
|
|
3
3
|
value: true
|
|
4
4
|
});
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
get PinoExtensions () {
|
|
8
13
|
return PinoExtensions;
|
|
14
|
+
},
|
|
15
|
+
get guardTransport () {
|
|
16
|
+
return _pinotransportguard.guardTransport;
|
|
17
|
+
},
|
|
18
|
+
get pinoTransport () {
|
|
19
|
+
return pinoTransport;
|
|
9
20
|
}
|
|
10
21
|
});
|
|
11
22
|
const _extensibility = require("@decohere/nexus/extensibility");
|
|
12
23
|
const _system = require("@decohere/nexus/system");
|
|
13
24
|
const _scribe = require("@decohere/scribe");
|
|
25
|
+
const _pino = /*#__PURE__*/ _interop_require_default(require("pino"));
|
|
14
26
|
const _pinoadapater = require("./pino.adapater.js");
|
|
27
|
+
const _pinorecipe = require("./pino.recipe.js");
|
|
28
|
+
const _pinotransportguard = require("./pino.transport-guard.js");
|
|
29
|
+
function _interop_require_default(obj) {
|
|
30
|
+
return obj && obj.__esModule ? obj : {
|
|
31
|
+
default: obj
|
|
32
|
+
};
|
|
33
|
+
}
|
|
15
34
|
function _ts_decorate(decorators, target, key, desc) {
|
|
16
35
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
17
36
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -23,27 +42,32 @@ function _ts_metadata(k, v) {
|
|
|
23
42
|
}
|
|
24
43
|
class PinoExtensions {
|
|
25
44
|
static withPino(factory, config, logLevel) {
|
|
26
|
-
return configureScribe(factory, {
|
|
45
|
+
return configureScribe(factory, logLevel != null ? {
|
|
27
46
|
logLevel
|
|
28
|
-
}, config);
|
|
47
|
+
} : undefined, config);
|
|
29
48
|
}
|
|
30
|
-
static withPinoPretty(factory,
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
static withPinoPretty(factory, logLevelOrOptions) {
|
|
50
|
+
const pretty = parsePrettyArg(logLevelOrOptions);
|
|
51
|
+
return attachRecipe(factory, {
|
|
52
|
+
pretty,
|
|
53
|
+
logLevel: pretty.logLevel
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
static withPinoRoll(factory, options) {
|
|
57
|
+
return attachRecipe(factory, {
|
|
58
|
+
roll: options,
|
|
59
|
+
logLevel: options.logLevel
|
|
60
|
+
});
|
|
39
61
|
}
|
|
40
62
|
static withPinoCustom(factory, options) {
|
|
41
|
-
const { scribe, transports } = options;
|
|
63
|
+
const { scribe, transports } = options ?? {};
|
|
42
64
|
return configureScribe(factory, scribe, transports);
|
|
43
65
|
}
|
|
44
66
|
}
|
|
45
67
|
_ts_decorate([
|
|
46
|
-
(0, _extensibility.extension)(_scribe.scribe
|
|
68
|
+
(0, _extensibility.extension)(_scribe.scribe, {
|
|
69
|
+
static: true
|
|
70
|
+
}),
|
|
47
71
|
_ts_metadata("design:type", Function),
|
|
48
72
|
_ts_metadata("design:paramtypes", [
|
|
49
73
|
typeof _scribe.scribe === "undefined" ? Object : _scribe.scribe,
|
|
@@ -53,7 +77,9 @@ _ts_decorate([
|
|
|
53
77
|
_ts_metadata("design:returntype", void 0)
|
|
54
78
|
], PinoExtensions, "withPino", null);
|
|
55
79
|
_ts_decorate([
|
|
56
|
-
(0, _extensibility.extension)(_scribe.scribe
|
|
80
|
+
(0, _extensibility.extension)(_scribe.scribe, {
|
|
81
|
+
static: true
|
|
82
|
+
}),
|
|
57
83
|
_ts_metadata("design:type", Function),
|
|
58
84
|
_ts_metadata("design:paramtypes", [
|
|
59
85
|
typeof _scribe.scribe === "undefined" ? Object : _scribe.scribe,
|
|
@@ -62,7 +88,20 @@ _ts_decorate([
|
|
|
62
88
|
_ts_metadata("design:returntype", void 0)
|
|
63
89
|
], PinoExtensions, "withPinoPretty", null);
|
|
64
90
|
_ts_decorate([
|
|
65
|
-
(0, _extensibility.extension)(_scribe.scribe
|
|
91
|
+
(0, _extensibility.extension)(_scribe.scribe, {
|
|
92
|
+
static: true
|
|
93
|
+
}),
|
|
94
|
+
_ts_metadata("design:type", Function),
|
|
95
|
+
_ts_metadata("design:paramtypes", [
|
|
96
|
+
typeof _scribe.scribe === "undefined" ? Object : _scribe.scribe,
|
|
97
|
+
typeof PinoRollRecipeOptions === "undefined" ? Object : PinoRollRecipeOptions
|
|
98
|
+
]),
|
|
99
|
+
_ts_metadata("design:returntype", void 0)
|
|
100
|
+
], PinoExtensions, "withPinoRoll", null);
|
|
101
|
+
_ts_decorate([
|
|
102
|
+
(0, _extensibility.extension)(_scribe.scribe, {
|
|
103
|
+
static: true
|
|
104
|
+
}),
|
|
66
105
|
_ts_metadata("design:type", Function),
|
|
67
106
|
_ts_metadata("design:paramtypes", [
|
|
68
107
|
typeof _scribe.scribe === "undefined" ? Object : _scribe.scribe,
|
|
@@ -70,23 +109,85 @@ _ts_decorate([
|
|
|
70
109
|
]),
|
|
71
110
|
_ts_metadata("design:returntype", void 0)
|
|
72
111
|
], PinoExtensions, "withPinoCustom", null);
|
|
112
|
+
function parsePrettyArg(logLevelOrOptions) {
|
|
113
|
+
if (logLevelOrOptions == null) {
|
|
114
|
+
return {};
|
|
115
|
+
}
|
|
116
|
+
if (typeof logLevelOrOptions === "string") {
|
|
117
|
+
return {
|
|
118
|
+
logLevel: logLevelOrOptions
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return logLevelOrOptions;
|
|
122
|
+
}
|
|
123
|
+
function attachRecipe(factory, plan) {
|
|
124
|
+
const loggerFactory = applyRecipe(factory, plan);
|
|
125
|
+
const chain = {
|
|
126
|
+
withPinoPretty (logLevelOrOptions) {
|
|
127
|
+
const pretty = parsePrettyArg(logLevelOrOptions);
|
|
128
|
+
return attachRecipe(factory, {
|
|
129
|
+
...plan,
|
|
130
|
+
pretty: {
|
|
131
|
+
...plan.pretty,
|
|
132
|
+
...pretty
|
|
133
|
+
},
|
|
134
|
+
logLevel: pretty.logLevel ?? plan.logLevel
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
withPinoRoll (options) {
|
|
138
|
+
return attachRecipe(factory, {
|
|
139
|
+
...plan,
|
|
140
|
+
roll: options,
|
|
141
|
+
logLevel: options.logLevel ?? plan.logLevel
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
return Object.assign(loggerFactory, chain, {
|
|
146
|
+
and: chain
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Guard a worker transport so failures (roll EEXIST, worker exit) neither
|
|
151
|
+
* crash the process nor spam stderr on every subsequent log write.
|
|
152
|
+
*/ function createSafeTransport(options) {
|
|
153
|
+
return (0, _pinotransportguard.guardTransport)(_pino.default.transport(options));
|
|
154
|
+
}
|
|
155
|
+
function adapterForTransport(transport) {
|
|
156
|
+
if (transport == null) {
|
|
157
|
+
return new _pinoadapater.PinoAdapter({});
|
|
158
|
+
}
|
|
159
|
+
return new _pinoadapater.PinoAdapter({}, createSafeTransport(transport));
|
|
160
|
+
}
|
|
161
|
+
function applyRecipe(factory, plan) {
|
|
162
|
+
const adapter = adapterForTransport(_pinorecipe.pinoRecipe.transport(plan));
|
|
163
|
+
// Omit logLevel when unset so mergeDefaults can read LOG_LEVEL / SCRIBE_LOG_LEVEL.
|
|
164
|
+
return factory.configure({
|
|
165
|
+
options: plan.logLevel != null ? {
|
|
166
|
+
logLevel: plan.logLevel
|
|
167
|
+
} : {},
|
|
168
|
+
adapter
|
|
169
|
+
});
|
|
170
|
+
}
|
|
73
171
|
function configureScribe(factory, options, config) {
|
|
74
172
|
const environment = process.env.NODE_ENV ?? _system.DefaultEnvironment;
|
|
75
|
-
const configs = Array.isArray(config) ? config : [
|
|
173
|
+
const configs = (Array.isArray(config) ? config : config != null ? [
|
|
76
174
|
config
|
|
77
|
-
];
|
|
78
|
-
|
|
79
|
-
if (transports.length > 1) {
|
|
175
|
+
] : []).filter((item)=>item.environment == null || item.environment === environment);
|
|
176
|
+
if (configs.length > 1) {
|
|
80
177
|
throw new _scribe.LoggingConfigurationError(`There should only be one configuration per environment for logging`);
|
|
81
178
|
}
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
transport
|
|
85
|
-
});
|
|
179
|
+
const adapter = adapterForTransport(configs.at(0)?.transports);
|
|
180
|
+
// Do not force DefaultLogLevel — that would shadow env resolution in mergeDefaults.
|
|
86
181
|
return factory.configure({
|
|
87
|
-
options,
|
|
182
|
+
options: options ?? {},
|
|
88
183
|
adapter
|
|
89
184
|
});
|
|
90
185
|
}
|
|
91
|
-
|
|
92
|
-
|
|
186
|
+
/** @deprecated Use {@link guardTransport} — kept for callers/tests from the silence-only era. */ function silenceErrors(transport) {
|
|
187
|
+
return (0, _pinotransportguard.guardTransport)(transport);
|
|
188
|
+
}
|
|
189
|
+
const pinoTransport = {
|
|
190
|
+
createSafeTransport,
|
|
191
|
+
guardTransport: _pinotransportguard.guardTransport,
|
|
192
|
+
silenceErrors
|
|
193
|
+
};
|
package/es6/src/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
export * from "./pino.adapater.js";
|
|
2
|
+
export * from "./pino.recipe.js";
|
|
2
3
|
export * from "./scribe-pino.js";
|
|
3
|
-
|
|
4
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi9ob21lL3J1bm5lci93b3JrL2RlY29oZXJlL2RlY29oZXJlL3Byb2plY3RzL3NjcmliZS9zY3JpYmUtcGluby9zcmMvaW5kZXgudHMiXSwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0ICogZnJvbSBcIi4vcGluby5hZGFwYXRlci5qc1wiO1xuZXhwb3J0ICogZnJvbSBcIi4vc2NyaWJlLXBpbm8uanNcIjtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLHFCQUFxQjtBQUNuQyxjQUFjLG1CQUFtQiJ9
|
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 = {
|
|
@@ -69,5 +189,3 @@ function pinoMix(logger) {
|
|
|
69
189
|
}
|
|
70
190
|
});
|
|
71
191
|
}
|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CO0FBRUEsU0FBU04sUUFDUkQsTUFBbUI7SUFFbkIsT0FBT2EsT0FBT0MsTUFBTSxDQUEwQmQsUUFBUTtRQUNyRCxJQUFJZSxnQkFBZTtZQUNsQixPQUFPZixPQUFPVixLQUFLO1FBQ3BCO1FBQ0EsSUFBSTBCLGFBQVk7WUFDZixPQUFPaEIsT0FBT2lCLE1BQU0sQ0FBQ0MsTUFBTTtRQUM1QjtJQUNEO0FBQ0QifQ==
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { Environments } from "@decohere/nexus/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
|
@@ -10,30 +10,39 @@ function _ts_metadata(k, v) {
|
|
|
10
10
|
import { extension } from "@decohere/nexus/extensibility";
|
|
11
11
|
import { DefaultEnvironment } from "@decohere/nexus/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.100",
|
|
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/nexus": "0.0.1-dev.
|
|
27
|
-
"@decohere/scribe": "0.0.1-dev.
|
|
26
|
+
"@decohere/nexus": "0.0.1-dev.100",
|
|
27
|
+
"@decohere/scribe": "0.0.1-dev.100",
|
|
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/nexus/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;
|
|
@@ -1,24 +1,72 @@
|
|
|
1
1
|
import { type Environment } from "@decohere/nexus/system";
|
|
2
2
|
import { type DefaultLevels, type Logger, type ScribeOptions, scribe } from "@decohere/scribe";
|
|
3
|
-
import type
|
|
3
|
+
import { type DestinationStream, type TransportMultiOptions, type TransportPipelineOptions, type TransportSingleOptions } from "pino";
|
|
4
|
+
import { type PinoPrettyRecipeOptions, type PinoRollRecipeOptions } from "./pino.recipe.js";
|
|
5
|
+
import { type GuardedTransport, guardTransport } from "./pino.transport-guard.js";
|
|
6
|
+
export type { GuardedTransport } from "./pino.transport-guard.js";
|
|
7
|
+
export { guardTransport } from "./pino.transport-guard.js";
|
|
8
|
+
type TransportOptions = TransportSingleOptions | TransportMultiOptions | TransportPipelineOptions;
|
|
4
9
|
type TransportConfig = {
|
|
5
10
|
environment?: Environment;
|
|
6
|
-
transports:
|
|
11
|
+
transports: TransportOptions;
|
|
7
12
|
};
|
|
8
13
|
type CustomLevelOptions<T extends string = never, CustomOnly extends true = undefined> = {
|
|
9
14
|
scribe?: ScribeOptions<T, CustomOnly>;
|
|
10
15
|
transports?: TransportConfig | TransportConfig[];
|
|
11
16
|
};
|
|
17
|
+
export type PinoLoggerFactory = (name?: string) => Logger;
|
|
18
|
+
export type PinoRecipeChain = {
|
|
19
|
+
withPinoPretty(logLevelOrOptions?: DefaultLevels | string | PinoPrettyRecipeOptions): PinoRecipe;
|
|
20
|
+
withPinoRoll(options: PinoRollRecipeOptions): PinoRecipe;
|
|
21
|
+
};
|
|
22
|
+
export type PinoRecipe = PinoLoggerFactory & PinoRecipeChain & {
|
|
23
|
+
readonly and: PinoRecipeChain;
|
|
24
|
+
};
|
|
12
25
|
declare module "@decohere/scribe" {
|
|
13
26
|
interface scribe {
|
|
14
|
-
withPino(transports: TransportConfig | TransportConfig[], loglevel?: DefaultLevels | string):
|
|
15
|
-
|
|
16
|
-
|
|
27
|
+
withPino(transports: TransportConfig | TransportConfig[], loglevel?: DefaultLevels | string): PinoLoggerFactory;
|
|
28
|
+
/**
|
|
29
|
+
* Colorized pretty transport. Development only unless `{ when: "always" }`.
|
|
30
|
+
* Chain with `.and.withPinoRoll({ file })`. Production writes JSON to stdout
|
|
31
|
+
* so log shipping stays outside the process.
|
|
32
|
+
*/
|
|
33
|
+
withPinoPretty(logLevelOrOptions?: DefaultLevels | string | PinoPrettyRecipeOptions): PinoRecipe;
|
|
34
|
+
/**
|
|
35
|
+
* Rolling log files. Development only unless `{ when: "always" }`.
|
|
36
|
+
* Do not enable in production — ship stdout from a sidecar or platform agent.
|
|
37
|
+
* `symlink` defaults off; enabling it can race under multi-worker / HMR.
|
|
38
|
+
* Ops may override path/size/frequency/keep via `SCRIBE_LOG_FILE`,
|
|
39
|
+
* `SCRIBE_LOG_SIZE`, `SCRIBE_LOG_FREQUENCY`, and `SCRIBE_LOG_KEEP`
|
|
40
|
+
* (file env wins over `options.file`; other explicit options beat env).
|
|
41
|
+
*/
|
|
42
|
+
withPinoRoll(options: PinoRollRecipeOptions): PinoRecipe;
|
|
43
|
+
withPinoCustom<T extends string = never, CustomOnly extends true = undefined>(options?: CustomLevelOptions<T, CustomOnly>): PinoLoggerFactory;
|
|
44
|
+
}
|
|
45
|
+
namespace scribe {
|
|
46
|
+
function withPino(transports: TransportConfig | TransportConfig[], loglevel?: DefaultLevels | string): PinoLoggerFactory;
|
|
47
|
+
function withPinoPretty(logLevelOrOptions?: DefaultLevels | string | PinoPrettyRecipeOptions): PinoRecipe;
|
|
48
|
+
function withPinoRoll(options: PinoRollRecipeOptions): PinoRecipe;
|
|
49
|
+
function withPinoCustom<T extends string = never, CustomOnly extends true = undefined>(options?: CustomLevelOptions<T, CustomOnly>): PinoLoggerFactory;
|
|
17
50
|
}
|
|
18
51
|
}
|
|
19
52
|
export declare class PinoExtensions {
|
|
20
53
|
static withPino(factory: scribe, config: TransportConfig | TransportConfig[], logLevel?: DefaultLevels | string): (name?: string) => Logger<never, false>;
|
|
21
|
-
static withPinoPretty(factory: scribe,
|
|
54
|
+
static withPinoPretty(factory: scribe, logLevelOrOptions?: DefaultLevels | string | PinoPrettyRecipeOptions): PinoRecipe;
|
|
55
|
+
static withPinoRoll(factory: scribe, options: PinoRollRecipeOptions): PinoRecipe;
|
|
22
56
|
static withPinoCustom<T extends string = never, CustomOnly extends true = undefined>(factory: scribe, options?: CustomLevelOptions<T, CustomOnly>): (name?: string) => Logger<T, CustomOnly>;
|
|
23
57
|
}
|
|
24
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Guard a worker transport so failures (roll EEXIST, worker exit) neither
|
|
60
|
+
* crash the process nor spam stderr on every subsequent log write.
|
|
61
|
+
*/
|
|
62
|
+
declare function createSafeTransport(options: TransportOptions): DestinationStream;
|
|
63
|
+
/** @deprecated Use {@link guardTransport} — kept for callers/tests from the silence-only era. */
|
|
64
|
+
declare function silenceErrors(transport: {
|
|
65
|
+
on(event: "error", listener: (err: Error) => void): unknown;
|
|
66
|
+
write?: (...args: never[]) => unknown;
|
|
67
|
+
}): GuardedTransport;
|
|
68
|
+
export declare const pinoTransport: {
|
|
69
|
+
createSafeTransport: typeof createSafeTransport;
|
|
70
|
+
guardTransport: typeof guardTransport;
|
|
71
|
+
silenceErrors: typeof silenceErrors;
|
|
72
|
+
};
|