@reventlessdev/reventless-core 3.0.0-alpha.325 → 3.0.0-alpha.326
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/CHANGELOG.md +8 -0
- package/package.json +7 -7
- package/src/Message.res +46 -0
- package/src/Message.res.mjs +45 -0
- package/src/Projection.res +269 -131
- package/src/Projection.res.mjs +338 -224
- package/src/ViewChange.res +194 -0
- package/src/ViewChange.res.mjs +235 -0
- package/src/components/Aggregate/Aggregate_Callback.res +6 -1
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +6 -5
- package/src/components/AutomationSlice/AutomationSlice_Callback.res +21 -9
- package/src/components/AutomationSlice/AutomationSlice_Callback.res.mjs +19 -8
- package/src/components/CommandTopic/CommandTopic.res.mjs +5 -2
- package/src/components/CommandTopic/CommandTopic_Helpers.res +56 -26
- package/src/components/CommandTopic/CommandTopic_Helpers.res.mjs +35 -16
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Builder.res.mjs +6 -2
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res +25 -12
- package/src/components/OutboundTranslationSlice/OutboundTranslationSlice_Callback.res.mjs +20 -7
- package/src/components/ReadModel/ReadModel_Callback.res +23 -3
- package/src/components/ReadModel/ReadModel_Callback.res.mjs +14 -2
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +7 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +7 -7
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +14 -2
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +13 -2
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +29 -4
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +9 -2
- package/src/util/EffectLogger.res +30 -29
- package/src/util/EffectLogger.res.mjs +13 -22
- package/src/util/Logger.res +231 -35
- package/src/util/Logger.res.mjs +263 -42
- package/tests/commandgenerator/OwnerActingTest.res +3 -1
- package/tests/commandtopic/CommandOutcomeHookTest.res +74 -2
- package/tests/commandtopic/CommandOutcomeHookTest.res.mjs +92 -0
- package/tests/commandtopic/CommandTopicHelpersRejectionTest.res.mjs +4 -4
- package/tests/logger/LoggerSinkTest.res +205 -0
- package/tests/logger/LoggerSinkTest.res.mjs +209 -0
- package/tests/projection/ViewChangeTest.res +173 -0
- package/tests/projection/ViewChangeTest.res.mjs +292 -0
package/src/util/Logger.res
CHANGED
|
@@ -106,6 +106,79 @@ let fmtComp = Reventless.LogPrefix.fmtComp
|
|
|
106
106
|
let fmtPlainPrefix = Reventless.LogPrefix.fmtPlainPrefix
|
|
107
107
|
let resolvePlugin = Reventless.LogPrefix.resolvePlugin
|
|
108
108
|
|
|
109
|
+
// ─── The record every line is made from ──────────────────────────────────────
|
|
110
|
+
|
|
111
|
+
/** One log line before it is formatted: what the JSON line prints, field for
|
|
112
|
+
field, and what a registered sink receives. `detail` is already redacted and
|
|
113
|
+
truncated, so a sink can keep it as it is. */
|
|
114
|
+
type record = {
|
|
115
|
+
time: string,
|
|
116
|
+
level: level,
|
|
117
|
+
message: string,
|
|
118
|
+
service?: string,
|
|
119
|
+
plugin?: string,
|
|
120
|
+
comp?: string,
|
|
121
|
+
annotations: dict<string>,
|
|
122
|
+
detail?: JSON.t,
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The JSON line's fields, in the order the line prints them. Annotations are
|
|
126
|
+
top-level keys beside the fixed ones. */
|
|
127
|
+
let recordFields = (r: record): array<(string, JSON.t)> =>
|
|
128
|
+
[
|
|
129
|
+
("time", r.time->JSON.Encode.string),
|
|
130
|
+
("level", levelLabel(r.level)->JSON.Encode.string),
|
|
131
|
+
("message", r.message->JSON.Encode.string),
|
|
132
|
+
]
|
|
133
|
+
->Array.concat(r.service->Option.mapOr([], s => [("service", JSON.Encode.string(s))]))
|
|
134
|
+
->Array.concat(r.plugin->Option.mapOr([], p => [("plugin", JSON.Encode.string(p))]))
|
|
135
|
+
->Array.concat(r.comp->Option.mapOr([], c => [("comp", JSON.Encode.string(c))]))
|
|
136
|
+
->Array.concat(r.annotations->Dict.toArray->Array.map(((k, v)) => (k, JSON.Encode.string(v))))
|
|
137
|
+
->Array.concat(r.detail->Option.mapOr([], d => [("detail", d)]))
|
|
138
|
+
|
|
139
|
+
let recordToJson = (r: record): JSON.t => r->recordFields->Dict.fromArray->JSON.Encode.object
|
|
140
|
+
|
|
141
|
+
// ─── Sinks ───────────────────────────────────────────────────────────────────
|
|
142
|
+
// Code that keeps or forwards log records (a platform's recent-records ring, a
|
|
143
|
+
// feed to a watching tool) registers a sink and receives every record that
|
|
144
|
+
// passed its level, before it is formatted.
|
|
145
|
+
|
|
146
|
+
type sink = record => unit
|
|
147
|
+
|
|
148
|
+
type registeredSink = {
|
|
149
|
+
id: int,
|
|
150
|
+
sink: sink,
|
|
151
|
+
// A sink that throws is noted once, not on every line it fails.
|
|
152
|
+
mutable noted: bool,
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let sinks: ref<array<registeredSink>> = ref([])
|
|
156
|
+
let nextSinkId = ref(0)
|
|
157
|
+
|
|
158
|
+
/** Registers a sink and returns the function that removes it. */
|
|
159
|
+
let addSink = (sink: sink): (unit => unit) => {
|
|
160
|
+
nextSinkId := nextSinkId.contents + 1
|
|
161
|
+
let id = nextSinkId.contents
|
|
162
|
+
sinks := sinks.contents->Array.concat([{id, sink, noted: false}])
|
|
163
|
+
() => sinks := sinks.contents->Array.filter(s => s.id != id)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
let hasSinks = () => sinks.contents->Array.length > 0
|
|
167
|
+
|
|
168
|
+
// Written straight to stderr, never through `emit`: a note about a failing sink
|
|
169
|
+
// must not reach that sink again.
|
|
170
|
+
let callSinks = (r: record) =>
|
|
171
|
+
sinks.contents->Array.forEach(s =>
|
|
172
|
+
try s.sink(r) catch {
|
|
173
|
+
| exn =>
|
|
174
|
+
if !s.noted {
|
|
175
|
+
s.noted = true
|
|
176
|
+
let why = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("unknown")
|
|
177
|
+
Console.error(`log sink ${s.id->Int.toString} failed and is skipped for that line: ${why}`)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
)
|
|
181
|
+
|
|
109
182
|
// Central emit — all log output flows through here.
|
|
110
183
|
// ~detail: structured data shown only in CloudWatch (expandable on click).
|
|
111
184
|
// Terminal (in-memory): shows only the message line.
|
|
@@ -125,37 +198,35 @@ let emit = (
|
|
|
125
198
|
// Sink decision lives in Reventless.AnsiStyle (single source of truth, shared
|
|
126
199
|
// with fmtComp / AnsiStyle.bold). Non-TTY collectors (Lambda, Fargate, ECS,
|
|
127
200
|
// Azure, GCP, Docker, CI, piped stdout) all get JSON; a TTY stays text.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// stays clean human text. `detail` is the expandable structured payload.
|
|
201
|
+
let json = Reventless.AnsiStyle.isJsonSink()
|
|
202
|
+
// The record is built only when something reads it: the JSON line, or a sink.
|
|
203
|
+
let record = if json || hasSinks() {
|
|
132
204
|
// Explicit ~plugin wins; otherwise fall back to the LogPrefix registry.
|
|
133
205
|
let pluginField = switch plugin {
|
|
134
206
|
| Some(_) as p => p
|
|
135
207
|
| None => resolvePlugin(~comp?, ())
|
|
136
208
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
("message", msg->JSON.Encode.string),
|
|
146
|
-
]
|
|
147
|
-
->Array.concat(serviceName()->Option.mapOr([], s => [("service", JSON.Encode.string(s))]))
|
|
148
|
-
->Array.concat(pluginField->Option.mapOr([], p => [("plugin", JSON.Encode.string(p))]))
|
|
149
|
-
->Array.concat(comp->Option.mapOr([], c => [("comp", JSON.Encode.string(c))]))
|
|
150
|
-
->Array.concat(annotationFields)
|
|
209
|
+
Some({
|
|
210
|
+
time: iso(),
|
|
211
|
+
level,
|
|
212
|
+
message: msg,
|
|
213
|
+
service: ?serviceName(),
|
|
214
|
+
plugin: ?pluginField,
|
|
215
|
+
?comp,
|
|
216
|
+
annotations: annotations->Option.getOr(Dict.make()),
|
|
151
217
|
// Redacted before anything else: a personal value must not reach a log
|
|
152
218
|
// group, which cannot be swept for one person (`LogRedaction`).
|
|
153
|
-
->
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
219
|
+
detail: ?(detail->Option.map(d => d->Reventless.LogRedaction.redactJson->truncateDetail)),
|
|
220
|
+
})
|
|
221
|
+
} else {
|
|
222
|
+
None
|
|
223
|
+
}
|
|
224
|
+
record->Option.forEach(callSinks)
|
|
225
|
+
if json {
|
|
226
|
+
// Structured JSON for any log collector. Plugin/comp/annotations are promoted
|
|
227
|
+
// to top-level queryable keys instead of being baked into `message`; `message`
|
|
228
|
+
// stays clean human text. `detail` is the expandable structured payload.
|
|
229
|
+
record->Option.forEach(r => Console.log(r->recordToJson->JSON.stringify))
|
|
159
230
|
} else {
|
|
160
231
|
// Terminal — colored, human-readable, no detail
|
|
161
232
|
let t = hms()
|
|
@@ -182,9 +253,10 @@ let setDefaultMinLevel = (l: level) => defaultMinLevel := l
|
|
|
182
253
|
// Build a logger from a per-call predicate. `shouldLog` is evaluated on every
|
|
183
254
|
// emit, so a logger created at module-init time still reflects a platform
|
|
184
255
|
// default set later (and a live LOG_LEVEL) instead of baking the level in once.
|
|
185
|
-
|
|
256
|
+
// It is given the line's component, so a level set for one component applies.
|
|
257
|
+
let makeLoggerWith = (shouldLog: (level, option<string>) => bool): t => {
|
|
186
258
|
let log = (level, ~comp=?, ~data=?, msg) =>
|
|
187
|
-
if shouldLog(level) {
|
|
259
|
+
if shouldLog(level, comp) {
|
|
188
260
|
let dataStr =
|
|
189
261
|
data
|
|
190
262
|
->Option.map(d => ` ${d->Reventless.LogRedaction.redactJson->JSON.stringify}`)
|
|
@@ -197,7 +269,7 @@ let makeLoggerWith = (shouldLog: level => bool): t => {
|
|
|
197
269
|
warn: (~comp=?, ~data=?, msg) => log(Warn, ~comp?, ~data?, msg),
|
|
198
270
|
error: (~comp=?, ~data=?, msg) => log(Error, ~comp?, ~data?, msg),
|
|
199
271
|
debugLazy: (~comp=?, makeMsg) =>
|
|
200
|
-
if shouldLog(Debug) {
|
|
272
|
+
if shouldLog(Debug, comp) {
|
|
201
273
|
emit(~level=Debug, ~comp?, makeMsg())
|
|
202
274
|
},
|
|
203
275
|
}
|
|
@@ -205,7 +277,8 @@ let makeLoggerWith = (shouldLog: level => bool): t => {
|
|
|
205
277
|
|
|
206
278
|
// Fixed-level logger — the level is baked at creation. Used by tests that assert
|
|
207
279
|
// a specific minimum regardless of env/platform default.
|
|
208
|
-
let makeLogger = (~minLevel=Info): t =>
|
|
280
|
+
let makeLogger = (~minLevel=Info): t =>
|
|
281
|
+
makeLoggerWith((l, _) => levelToInt(l) >= levelToInt(minLevel))
|
|
209
282
|
|
|
210
283
|
let silent: t = {
|
|
211
284
|
debug: (~comp as _=?, ~data as _=?, _) => (),
|
|
@@ -229,16 +302,139 @@ let effectiveMin = (): option<level> =>
|
|
|
229
302
|
| None => Some(defaultMinLevel.contents)
|
|
230
303
|
}
|
|
231
304
|
|
|
305
|
+
// ─── Levels set while running ────────────────────────────────────────────────
|
|
306
|
+
// A running process can be told a level for everything, for one plugin, for one
|
|
307
|
+
// component, or for one component of one plugin. Consulted before LOG_LEVEL, and
|
|
308
|
+
// the narrowest one that is set wins: component of a plugin, then component, then
|
|
309
|
+
// plugin, then the default. Kept in memory only, so a level set this way lasts
|
|
310
|
+
// until the process stops.
|
|
311
|
+
|
|
312
|
+
type levelOverrides = {
|
|
313
|
+
plugins: dict<level>,
|
|
314
|
+
comps: dict<level>,
|
|
315
|
+
// Keyed `plugin + "\x00" + comp`.
|
|
316
|
+
pluginComps: dict<level>,
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let overrides: levelOverrides = {plugins: Dict.make(), comps: Dict.make(), pluginComps: Dict.make()}
|
|
320
|
+
// The level for every line not covered by a narrower one; `None` until set.
|
|
321
|
+
let defaultOverride: ref<option<level>> = ref(None)
|
|
322
|
+
|
|
323
|
+
let pluginCompKey = (~plugin, ~comp) => plugin ++ "\x00" ++ comp
|
|
324
|
+
|
|
325
|
+
let hasOverrides = () =>
|
|
326
|
+
defaultOverride.contents->Option.isSome ||
|
|
327
|
+
overrides.plugins->Dict.keysToArray->Array.length > 0 ||
|
|
328
|
+
overrides.comps->Dict.keysToArray->Array.length > 0 ||
|
|
329
|
+
overrides.pluginComps->Dict.keysToArray->Array.length > 0
|
|
330
|
+
|
|
331
|
+
/** Sets the level for the scope the arguments name — both: that component of
|
|
332
|
+
that plugin; only `~comp`: that component wherever it runs; only `~plugin`:
|
|
333
|
+
every line of that plugin; neither: the default. `None` removes it, so the
|
|
334
|
+
next wider scope applies again. */
|
|
335
|
+
let setLevel = (~plugin: option<string>=?, ~comp: option<string>=?, level: option<level>) => {
|
|
336
|
+
let put = (d: dict<level>, key) =>
|
|
337
|
+
switch level {
|
|
338
|
+
| Some(l) => d->Dict.set(key, l)
|
|
339
|
+
| None => d->Dict.delete(key)
|
|
340
|
+
}
|
|
341
|
+
switch (plugin, comp) {
|
|
342
|
+
| (Some(p), Some(c)) => put(overrides.pluginComps, pluginCompKey(~plugin=p, ~comp=c))
|
|
343
|
+
| (None, Some(c)) => put(overrides.comps, c)
|
|
344
|
+
| (Some(p), None) => put(overrides.plugins, p)
|
|
345
|
+
| (None, None) => defaultOverride := level
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Removes every level set while running. */
|
|
350
|
+
let clearLevels = () => {
|
|
351
|
+
defaultOverride := None
|
|
352
|
+
[overrides.plugins, overrides.comps, overrides.pluginComps]->Array.forEach(d =>
|
|
353
|
+
d->Dict.keysToArray->Array.forEach(k => d->Dict.delete(k))
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
type levelSetting = {
|
|
358
|
+
plugin?: string,
|
|
359
|
+
comp?: string,
|
|
360
|
+
level: level,
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Every level set while running, the default first. */
|
|
364
|
+
let levelSettings = (): array<levelSetting> => {
|
|
365
|
+
let out = []
|
|
366
|
+
defaultOverride.contents->Option.forEach(level => out->Array.push({level: level}))
|
|
367
|
+
overrides.plugins
|
|
368
|
+
->Dict.toArray
|
|
369
|
+
->Array.forEach(((plugin, level)) => out->Array.push({plugin, level}))
|
|
370
|
+
overrides.comps->Dict.toArray->Array.forEach(((comp, level)) => out->Array.push({comp, level}))
|
|
371
|
+
overrides.pluginComps
|
|
372
|
+
->Dict.toArray
|
|
373
|
+
->Array.forEach(((key, level)) =>
|
|
374
|
+
switch key->String.split("\x00") {
|
|
375
|
+
| [plugin, comp] => out->Array.push({plugin, comp, level})
|
|
376
|
+
| _ => ()
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
out
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/** The minimum level a line of this component (and plugin) is written at.
|
|
383
|
+
`None` ⇒ nothing is written. The plugin is resolved from the component when
|
|
384
|
+
not given, and only when a level was set for a plugin. */
|
|
385
|
+
let minLevelFor = (~plugin: option<string>=?, ~comp: option<string>=?, ()): option<level> =>
|
|
386
|
+
if !hasOverrides() {
|
|
387
|
+
effectiveMin()
|
|
388
|
+
} else {
|
|
389
|
+
let pluginOf = () =>
|
|
390
|
+
switch plugin {
|
|
391
|
+
| Some(_) as p => p
|
|
392
|
+
| None => resolvePlugin(~comp?, ())
|
|
393
|
+
}
|
|
394
|
+
let fromPluginComp = switch comp {
|
|
395
|
+
| Some(c) if overrides.pluginComps->Dict.keysToArray->Array.length > 0 =>
|
|
396
|
+
pluginOf()->Option.flatMap(p =>
|
|
397
|
+
overrides.pluginComps->Dict.get(pluginCompKey(~plugin=p, ~comp=c))
|
|
398
|
+
)
|
|
399
|
+
| _ => None
|
|
400
|
+
}
|
|
401
|
+
let fromComp = () => comp->Option.flatMap(c => overrides.comps->Dict.get(c))
|
|
402
|
+
let fromPlugin = () =>
|
|
403
|
+
if overrides.plugins->Dict.keysToArray->Array.length > 0 {
|
|
404
|
+
pluginOf()->Option.flatMap(p => overrides.plugins->Dict.get(p))
|
|
405
|
+
} else {
|
|
406
|
+
None
|
|
407
|
+
}
|
|
408
|
+
switch fromPluginComp {
|
|
409
|
+
| Some(_) as l => l
|
|
410
|
+
| None =>
|
|
411
|
+
switch fromComp() {
|
|
412
|
+
| Some(_) as l => l
|
|
413
|
+
| None =>
|
|
414
|
+
switch fromPlugin() {
|
|
415
|
+
| Some(_) as l => l
|
|
416
|
+
| None =>
|
|
417
|
+
switch defaultOverride.contents {
|
|
418
|
+
| Some(_) as l => l
|
|
419
|
+
| None => effectiveMin()
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** Whether a line at `level` of this component (and plugin) is written. */
|
|
427
|
+
let shouldLog = (level: level, ~plugin: option<string>=?, ~comp: option<string>=?, ()): bool =>
|
|
428
|
+
switch minLevelFor(~plugin?, ~comp?, ()) {
|
|
429
|
+
| None => false
|
|
430
|
+
| Some(min) => levelToInt(level) >= levelToInt(min)
|
|
431
|
+
}
|
|
432
|
+
|
|
232
433
|
// Reads LOG_LEVEL from process.env on every call and honors the platform default
|
|
233
434
|
// when unset. Safe to call at module top-level: the returned logger evaluates the
|
|
234
435
|
// level lazily, so a platform lowering the default afterwards (e.g.
|
|
235
436
|
// reventless-local → Debug) takes effect for loggers already constructed.
|
|
236
437
|
// LOG_LEVEL=silent disables all output (used by the reventless-gwt CLI so
|
|
237
438
|
// framework diagnostics don't interleave with NDJSON / TAP / JUnit streams).
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
switch effectiveMin() {
|
|
241
|
-
| None => false
|
|
242
|
-
| Some(min) => levelToInt(l) >= levelToInt(min)
|
|
243
|
-
}
|
|
244
|
-
)
|
|
439
|
+
// A level set while running (`setLevel`) is consulted first.
|
|
440
|
+
let fromEnv = (): t => makeLoggerWith((l, comp) => shouldLog(l, ~comp?, ()))
|
package/src/util/Logger.res.mjs
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
3
|
import * as Stdlib_Int from "@rescript/runtime/lib/es6/Stdlib_Int.js";
|
|
4
|
+
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
|
|
5
|
+
import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
|
|
4
6
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
5
7
|
import * as AnsiStyle$Reventless from "@reventlessdev/reventless-spec/src/AnsiStyle.res.mjs";
|
|
6
8
|
import * as LogPrefix$Reventless from "@reventlessdev/reventless-spec/src/LogPrefix.res.mjs";
|
|
9
|
+
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
|
|
7
10
|
import * as LogRedaction$Reventless from "@reventlessdev/reventless-spec/src/util/LogRedaction.res.mjs";
|
|
8
11
|
|
|
9
12
|
function serviceName() {
|
|
@@ -89,41 +92,107 @@ function truncateDetail(d) {
|
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
94
|
|
|
95
|
+
function recordFields(r) {
|
|
96
|
+
return [
|
|
97
|
+
[
|
|
98
|
+
"time",
|
|
99
|
+
r.time
|
|
100
|
+
],
|
|
101
|
+
[
|
|
102
|
+
"level",
|
|
103
|
+
levelLabel(r.level)
|
|
104
|
+
],
|
|
105
|
+
[
|
|
106
|
+
"message",
|
|
107
|
+
r.message
|
|
108
|
+
]
|
|
109
|
+
].concat(Stdlib_Option.mapOr(r.service, [], s => [[
|
|
110
|
+
"service",
|
|
111
|
+
s
|
|
112
|
+
]])).concat(Stdlib_Option.mapOr(r.plugin, [], p => [[
|
|
113
|
+
"plugin",
|
|
114
|
+
p
|
|
115
|
+
]])).concat(Stdlib_Option.mapOr(r.comp, [], c => [[
|
|
116
|
+
"comp",
|
|
117
|
+
c
|
|
118
|
+
]])).concat(Object.entries(r.annotations).map(param => [
|
|
119
|
+
param[0],
|
|
120
|
+
param[1]
|
|
121
|
+
])).concat(Stdlib_Option.mapOr(r.detail, [], d => [[
|
|
122
|
+
"detail",
|
|
123
|
+
d
|
|
124
|
+
]]));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function recordToJson(r) {
|
|
128
|
+
return Object.fromEntries(recordFields(r));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let sinks = {
|
|
132
|
+
contents: []
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
let nextSinkId = {
|
|
136
|
+
contents: 0
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
function addSink(sink) {
|
|
140
|
+
nextSinkId.contents = nextSinkId.contents + 1 | 0;
|
|
141
|
+
let id = nextSinkId.contents;
|
|
142
|
+
sinks.contents = sinks.contents.concat([{
|
|
143
|
+
id: id,
|
|
144
|
+
sink: sink,
|
|
145
|
+
noted: false
|
|
146
|
+
}]);
|
|
147
|
+
return () => {
|
|
148
|
+
sinks.contents = sinks.contents.filter(s => s.id !== id);
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function hasSinks() {
|
|
153
|
+
return sinks.contents.length !== 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function callSinks(r) {
|
|
157
|
+
sinks.contents.forEach(s => {
|
|
158
|
+
try {
|
|
159
|
+
return s.sink(r);
|
|
160
|
+
} catch (raw_exn) {
|
|
161
|
+
let exn = Primitive_exceptions.internalToException(raw_exn);
|
|
162
|
+
if (s.noted) {
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
s.noted = true;
|
|
166
|
+
let why = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(exn), Stdlib_JsExn.message), "unknown");
|
|
167
|
+
console.error(`log sink ` + s.id.toString() + ` failed and is skipped for that line: ` + why);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
92
173
|
function emit(level, comp, plugin, detail, annotations, msg) {
|
|
93
|
-
|
|
174
|
+
let json = AnsiStyle$Reventless.isJsonSink();
|
|
175
|
+
let record;
|
|
176
|
+
if (json || sinks.contents.length !== 0) {
|
|
94
177
|
let pluginField = plugin !== undefined ? plugin : LogPrefix$Reventless.resolvePlugin(comp, undefined);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
"service",
|
|
114
|
-
s
|
|
115
|
-
]])).concat(Stdlib_Option.mapOr(pluginField, [], p => [[
|
|
116
|
-
"plugin",
|
|
117
|
-
p
|
|
118
|
-
]])).concat(Stdlib_Option.mapOr(comp, [], c => [[
|
|
119
|
-
"comp",
|
|
120
|
-
c
|
|
121
|
-
]])).concat(annotationFields).concat(Stdlib_Option.mapOr(detail, [], d => [[
|
|
122
|
-
"detail",
|
|
123
|
-
truncateDetail(LogRedaction$Reventless.redactJson(d))
|
|
124
|
-
]]));
|
|
125
|
-
console.log(JSON.stringify(Object.fromEntries(fields)));
|
|
126
|
-
return;
|
|
178
|
+
record = {
|
|
179
|
+
time: new Date().toISOString(),
|
|
180
|
+
level: level,
|
|
181
|
+
message: msg,
|
|
182
|
+
service: serviceName(),
|
|
183
|
+
plugin: pluginField,
|
|
184
|
+
comp: comp,
|
|
185
|
+
annotations: Stdlib_Option.getOr(annotations, {}),
|
|
186
|
+
detail: Stdlib_Option.map(detail, d => truncateDetail(LogRedaction$Reventless.redactJson(d)))
|
|
187
|
+
};
|
|
188
|
+
} else {
|
|
189
|
+
record = undefined;
|
|
190
|
+
}
|
|
191
|
+
Stdlib_Option.forEach(record, callSinks);
|
|
192
|
+
if (json) {
|
|
193
|
+
return Stdlib_Option.forEach(record, r => {
|
|
194
|
+
console.log(JSON.stringify(Object.fromEntries(recordFields(r))));
|
|
195
|
+
});
|
|
127
196
|
}
|
|
128
197
|
let t = hms();
|
|
129
198
|
let c = LogPrefix$Reventless.fmtComp(comp, undefined);
|
|
@@ -153,7 +222,7 @@ function setDefaultMinLevel(l) {
|
|
|
153
222
|
|
|
154
223
|
function makeLoggerWith(shouldLog) {
|
|
155
224
|
let log = (level, comp, data, msg) => {
|
|
156
|
-
if (!shouldLog(level)) {
|
|
225
|
+
if (!shouldLog(level, comp)) {
|
|
157
226
|
return;
|
|
158
227
|
}
|
|
159
228
|
let dataStr = Stdlib_Option.getOr(Stdlib_Option.map(data, d => ` ` + JSON.stringify(LogRedaction$Reventless.redactJson(d))), "");
|
|
@@ -165,7 +234,7 @@ function makeLoggerWith(shouldLog) {
|
|
|
165
234
|
warn: (comp, data, msg) => log("Warn", comp, data, msg),
|
|
166
235
|
error: (comp, data, msg) => log("Error", comp, data, msg),
|
|
167
236
|
debugLazy: (comp, makeMsg) => {
|
|
168
|
-
if (shouldLog("Debug")) {
|
|
237
|
+
if (shouldLog("Debug", comp)) {
|
|
169
238
|
return emit("Debug", comp, undefined, undefined, undefined, makeMsg());
|
|
170
239
|
}
|
|
171
240
|
}
|
|
@@ -174,7 +243,7 @@ function makeLoggerWith(shouldLog) {
|
|
|
174
243
|
|
|
175
244
|
function makeLogger(minLevelOpt) {
|
|
176
245
|
let minLevel = minLevelOpt !== undefined ? minLevelOpt : "Info";
|
|
177
|
-
return makeLoggerWith(l => levelToInt(l) >= levelToInt(minLevel));
|
|
246
|
+
return makeLoggerWith((l, param) => levelToInt(l) >= levelToInt(minLevel));
|
|
178
247
|
}
|
|
179
248
|
|
|
180
249
|
function silent_debug(param, param$1, param$2) {
|
|
@@ -224,15 +293,151 @@ function effectiveMin() {
|
|
|
224
293
|
}
|
|
225
294
|
}
|
|
226
295
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
296
|
+
let overrides_plugins = {};
|
|
297
|
+
|
|
298
|
+
let overrides_comps = {};
|
|
299
|
+
|
|
300
|
+
let overrides_pluginComps = {};
|
|
301
|
+
|
|
302
|
+
let overrides = {
|
|
303
|
+
plugins: overrides_plugins,
|
|
304
|
+
comps: overrides_comps,
|
|
305
|
+
pluginComps: overrides_pluginComps
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
let defaultOverride = {
|
|
309
|
+
contents: undefined
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
function pluginCompKey(plugin, comp) {
|
|
313
|
+
return plugin + "\x00" + comp;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function hasOverrides() {
|
|
317
|
+
if (Stdlib_Option.isSome(defaultOverride.contents) || Object.keys(overrides_plugins).length !== 0 || Object.keys(overrides_comps).length !== 0) {
|
|
318
|
+
return true;
|
|
319
|
+
} else {
|
|
320
|
+
return Object.keys(overrides_pluginComps).length !== 0;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function setLevel(plugin, comp, level) {
|
|
325
|
+
let put = (d, key) => {
|
|
326
|
+
if (level !== undefined) {
|
|
327
|
+
d[key] = level;
|
|
328
|
+
return;
|
|
329
|
+
} else {
|
|
330
|
+
return Stdlib_Dict.$$delete(d, key);
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
if (plugin !== undefined) {
|
|
334
|
+
if (comp !== undefined) {
|
|
335
|
+
return put(overrides_pluginComps, pluginCompKey(plugin, comp));
|
|
232
336
|
} else {
|
|
233
|
-
return
|
|
337
|
+
return put(overrides_plugins, plugin);
|
|
338
|
+
}
|
|
339
|
+
} else if (comp !== undefined) {
|
|
340
|
+
return put(overrides_comps, comp);
|
|
341
|
+
} else {
|
|
342
|
+
defaultOverride.contents = level;
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function clearLevels() {
|
|
348
|
+
defaultOverride.contents = undefined;
|
|
349
|
+
[
|
|
350
|
+
overrides_plugins,
|
|
351
|
+
overrides_comps,
|
|
352
|
+
overrides_pluginComps
|
|
353
|
+
].forEach(d => {
|
|
354
|
+
Object.keys(d).forEach(k => Stdlib_Dict.$$delete(d, k));
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function levelSettings() {
|
|
359
|
+
let out = [];
|
|
360
|
+
Stdlib_Option.forEach(defaultOverride.contents, level => {
|
|
361
|
+
out.push({
|
|
362
|
+
level: level
|
|
363
|
+
});
|
|
364
|
+
});
|
|
365
|
+
Object.entries(overrides_plugins).forEach(param => {
|
|
366
|
+
out.push({
|
|
367
|
+
plugin: param[0],
|
|
368
|
+
level: param[1]
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
Object.entries(overrides_comps).forEach(param => {
|
|
372
|
+
out.push({
|
|
373
|
+
comp: param[0],
|
|
374
|
+
level: param[1]
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
Object.entries(overrides_pluginComps).forEach(param => {
|
|
378
|
+
let match = param[0].split("\x00");
|
|
379
|
+
if (match.length !== 2) {
|
|
380
|
+
return;
|
|
234
381
|
}
|
|
382
|
+
let plugin = match[0];
|
|
383
|
+
let comp = match[1];
|
|
384
|
+
out.push({
|
|
385
|
+
plugin: plugin,
|
|
386
|
+
comp: comp,
|
|
387
|
+
level: param[1]
|
|
388
|
+
});
|
|
235
389
|
});
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function minLevelFor(plugin, comp, param) {
|
|
394
|
+
if (!hasOverrides()) {
|
|
395
|
+
return effectiveMin();
|
|
396
|
+
}
|
|
397
|
+
let pluginOf = () => {
|
|
398
|
+
if (plugin !== undefined) {
|
|
399
|
+
return plugin;
|
|
400
|
+
} else {
|
|
401
|
+
return LogPrefix$Reventless.resolvePlugin(comp, undefined);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
let fromPluginComp = comp !== undefined && Object.keys(overrides_pluginComps).length !== 0 ? Stdlib_Option.flatMap(pluginOf(), p => overrides_pluginComps[pluginCompKey(p, comp)]) : undefined;
|
|
405
|
+
let fromComp = () => Stdlib_Option.flatMap(comp, c => overrides_comps[c]);
|
|
406
|
+
let fromPlugin = () => {
|
|
407
|
+
if (Object.keys(overrides_plugins).length !== 0) {
|
|
408
|
+
return Stdlib_Option.flatMap(pluginOf(), p => overrides_plugins[p]);
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
if (fromPluginComp !== undefined) {
|
|
412
|
+
return fromPluginComp;
|
|
413
|
+
}
|
|
414
|
+
let l = fromComp();
|
|
415
|
+
if (l !== undefined) {
|
|
416
|
+
return l;
|
|
417
|
+
}
|
|
418
|
+
let l$1 = fromPlugin();
|
|
419
|
+
if (l$1 !== undefined) {
|
|
420
|
+
return l$1;
|
|
421
|
+
}
|
|
422
|
+
let l$2 = defaultOverride.contents;
|
|
423
|
+
if (l$2 !== undefined) {
|
|
424
|
+
return l$2;
|
|
425
|
+
} else {
|
|
426
|
+
return effectiveMin();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function shouldLog(level, plugin, comp, param) {
|
|
431
|
+
let min = minLevelFor(plugin, comp, undefined);
|
|
432
|
+
if (min !== undefined) {
|
|
433
|
+
return levelToInt(level) >= levelToInt(min);
|
|
434
|
+
} else {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function fromEnv() {
|
|
440
|
+
return makeLoggerWith((l, comp) => shouldLog(l, undefined, comp, undefined));
|
|
236
441
|
}
|
|
237
442
|
|
|
238
443
|
let currentPluginName = LogPrefix$Reventless.currentPluginName;
|
|
@@ -263,6 +468,13 @@ export {
|
|
|
263
468
|
fmtComp,
|
|
264
469
|
fmtPlainPrefix,
|
|
265
470
|
resolvePlugin,
|
|
471
|
+
recordFields,
|
|
472
|
+
recordToJson,
|
|
473
|
+
sinks,
|
|
474
|
+
nextSinkId,
|
|
475
|
+
addSink,
|
|
476
|
+
hasSinks,
|
|
477
|
+
callSinks,
|
|
266
478
|
emit,
|
|
267
479
|
defaultMinLevel,
|
|
268
480
|
setDefaultMinLevel,
|
|
@@ -270,6 +482,15 @@ export {
|
|
|
270
482
|
makeLogger,
|
|
271
483
|
silent,
|
|
272
484
|
effectiveMin,
|
|
485
|
+
overrides,
|
|
486
|
+
defaultOverride,
|
|
487
|
+
pluginCompKey,
|
|
488
|
+
hasOverrides,
|
|
489
|
+
setLevel,
|
|
490
|
+
clearLevels,
|
|
491
|
+
levelSettings,
|
|
492
|
+
minLevelFor,
|
|
493
|
+
shouldLog,
|
|
273
494
|
fromEnv,
|
|
274
495
|
}
|
|
275
496
|
/* s Not a pure module */
|
|
@@ -404,7 +404,9 @@ let paths = [
|
|
|
404
404
|
let _ = beforeEach(() => {
|
|
405
405
|
Reventless.OwnerScope.setElevatedGroups(["Admin"])
|
|
406
406
|
observed := []
|
|
407
|
-
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
407
|
+
CommandTopic_Helpers.registerCommandOutcome(r =>
|
|
408
|
+
observed := observed.contents->Array.concat([r])
|
|
409
|
+
)->ignore
|
|
408
410
|
})
|
|
409
411
|
|
|
410
412
|
let _ = afterEach(() => CommandTopic_Helpers.clearCommandOutcome())
|