@loglayer/mixin-wide-events 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,40 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region ../../core/plugin/dist/index.js
3
- /**
4
- * List of plugin callbacks that can be called by the plugin manager.
5
- *
6
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#transformloglevel | transformLogLevel Docs}
7
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforedataout | onBeforeDataOut Docs}
8
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#shouldsendtologger | shouldSendToLogger Docs}
9
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onmetadatacalled | onMetadataCalled Docs}
10
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforemessageout | onBeforeMessageOut Docs}
11
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#oncontextcalled | onContextCalled Docs}
12
- */
13
- let PluginCallbackType = /* @__PURE__ */ function(PluginCallbackType) {
14
- PluginCallbackType["transformLogLevel"] = "transformLogLevel";
15
- PluginCallbackType["onBeforeDataOut"] = "onBeforeDataOut";
16
- PluginCallbackType["shouldSendToLogger"] = "shouldSendToLogger";
17
- PluginCallbackType["onMetadataCalled"] = "onMetadataCalled";
18
- PluginCallbackType["onBeforeMessageOut"] = "onBeforeMessageOut";
19
- PluginCallbackType["onContextCalled"] = "onContextCalled";
20
- return PluginCallbackType;
21
- }({});
22
- PluginCallbackType.onBeforeDataOut, PluginCallbackType.onMetadataCalled, PluginCallbackType.onBeforeMessageOut, PluginCallbackType.transformLogLevel, PluginCallbackType.shouldSendToLogger, PluginCallbackType.onContextCalled;
23
- /**
24
- * The class that the mixin extends
25
- */
26
- let LogLayerMixinAugmentType = /* @__PURE__ */ function(LogLayerMixinAugmentType) {
27
- /**
28
- * Mixin extends the LogBuilder prototype
29
- */
30
- LogLayerMixinAugmentType["LogBuilder"] = "LogBuilder";
31
- /**
32
- * Mixin extends the LogLayer prototype
33
- */
34
- LogLayerMixinAugmentType["LogLayer"] = "LogLayer";
35
- return LogLayerMixinAugmentType;
36
- }({});
37
- //#endregion
2
+ let loglayer = require("loglayer");
38
3
  //#region src/mixin.ts
39
4
  /**
40
5
  * Log levels that default to rate=1 (error/fatal) — can be overridden via `perLevel` or callback.
@@ -129,7 +94,8 @@ function createWideEventMixin(options) {
129
94
  rate: options.sampling.rate,
130
95
  perLevel: snapshotPerLevel,
131
96
  emitLevel: options.sampling.emitLevel,
132
- shouldEmit: options.sampling.shouldEmit
97
+ shouldEmit: options.sampling.shouldEmit,
98
+ forceKeep: options.sampling.forceKeep
133
99
  } : void 0;
134
100
  function defaultErrorSerializer(err) {
135
101
  if (err instanceof Error) return {
@@ -224,23 +190,59 @@ function createWideEventMixin(options) {
224
190
  return self;
225
191
  }
226
192
  /**
193
+ * Logs a thrown sampling callback via console.error, gated on `consoleDebug`.
194
+ * Matches loglayer's emit-path error convention (see LogLayer.ts:1457).
195
+ */
196
+ function logCallbackError(callbackName, err, self) {
197
+ if (self.getConfig?.()?.consoleDebug) console.error(`[LogLayer] wide-events ${callbackName} callback threw; falling back:`, err);
198
+ }
199
+ /**
200
+ * Single sampling decision for an emit. Only called when a `forceKeep` or
201
+ * `shouldEmit` callback is configured (the no-callback case is handled by the
202
+ * pre-build fast-path in emitWideEventImpl).
203
+ *
204
+ * - `forceKeep` runs first (OR logic): true → keep; throws → fail-safe (log +
205
+ * fall through); false → fall through.
206
+ * - Standard logic: rate AND `shouldEmit` (shouldEmit throws → fail-open, log
207
+ * + keep). Without `shouldEmit`, rate alone decides.
208
+ *
209
+ * Invokes `runRateSampling` at most once.
210
+ */
211
+ function evaluateSampling(wideData, level, self) {
212
+ if (samplingConfig?.forceKeep) try {
213
+ if (samplingConfig.forceKeep({
214
+ wideData,
215
+ level
216
+ })) return true;
217
+ } catch (err) {
218
+ logCallbackError("forceKeep", err, self);
219
+ }
220
+ if (samplingConfig?.shouldEmit) try {
221
+ const callbackOk = samplingConfig.shouldEmit({
222
+ wideData,
223
+ level
224
+ });
225
+ return runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel) && callbackOk;
226
+ } catch (err) {
227
+ logCallbackError("shouldEmit", err, self);
228
+ return true;
229
+ }
230
+ return runRateSampling(level, samplingConfig?.strategy, samplingConfig?.rate, samplingConfig?.perLevel);
231
+ }
232
+ /**
227
233
  * Implementation for emitWideEvent
228
234
  */
229
235
  function emitWideEventImpl(config, self) {
230
236
  const defaultLevel = samplingConfig?.emitLevel ?? "info";
231
237
  const level = config.level ?? defaultLevel;
232
- if (samplingConfig && !samplingConfig.shouldEmit && !runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel)) return;
238
+ if (samplingConfig && !samplingConfig.shouldEmit && !samplingConfig.forceKeep && !runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel)) return;
233
239
  const store = asyncContext.getStore();
234
240
  const wideEventData = {};
235
241
  if (includeContext && store?._llContext) Object.assign(wideEventData, store._llContext);
236
242
  if (store?._llWideEvents) Object.assign(wideEventData, store._llWideEvents);
237
- if (samplingConfig?.shouldEmit) try {
238
- const callbackOk = samplingConfig.shouldEmit({
239
- wideData: wideEventData,
240
- level
241
- });
242
- if (!runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel) || !callbackOk) return;
243
- } catch {}
243
+ if (samplingConfig && (samplingConfig.forceKeep || samplingConfig.shouldEmit)) {
244
+ if (!evaluateSampling(wideEventData, level, self)) return;
245
+ }
244
246
  const metadataToEmit = wideEventField ? { [wideEventField]: wideEventData } : wideEventData;
245
247
  self.raw({
246
248
  logLevel: level,
@@ -250,7 +252,7 @@ function createWideEventMixin(options) {
250
252
  }
251
253
  return {
252
254
  mixinsToAdd: [{
253
- augmentationType: LogLayerMixinAugmentType.LogLayer,
255
+ augmentationType: loglayer.LogLayerMixinAugmentType.LogLayer,
254
256
  augment: (prototype) => {
255
257
  prototype.withWideEvents = function(data) {
256
258
  return withWideEventsImpl(data, this);