@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.mjs CHANGED
@@ -1,39 +1,4 @@
1
- //#region ../../core/plugin/dist/index.js
2
- /**
3
- * List of plugin callbacks that can be called by the plugin manager.
4
- *
5
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#transformloglevel | transformLogLevel Docs}
6
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforedataout | onBeforeDataOut Docs}
7
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#shouldsendtologger | shouldSendToLogger Docs}
8
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onmetadatacalled | onMetadataCalled Docs}
9
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#onbeforemessageout | onBeforeMessageOut Docs}
10
- * @see {@link https://loglayer.dev/plugins/creating-plugins.html#oncontextcalled | onContextCalled Docs}
11
- */
12
- let PluginCallbackType = /* @__PURE__ */ function(PluginCallbackType) {
13
- PluginCallbackType["transformLogLevel"] = "transformLogLevel";
14
- PluginCallbackType["onBeforeDataOut"] = "onBeforeDataOut";
15
- PluginCallbackType["shouldSendToLogger"] = "shouldSendToLogger";
16
- PluginCallbackType["onMetadataCalled"] = "onMetadataCalled";
17
- PluginCallbackType["onBeforeMessageOut"] = "onBeforeMessageOut";
18
- PluginCallbackType["onContextCalled"] = "onContextCalled";
19
- return PluginCallbackType;
20
- }({});
21
- PluginCallbackType.onBeforeDataOut, PluginCallbackType.onMetadataCalled, PluginCallbackType.onBeforeMessageOut, PluginCallbackType.transformLogLevel, PluginCallbackType.shouldSendToLogger, PluginCallbackType.onContextCalled;
22
- /**
23
- * The class that the mixin extends
24
- */
25
- let LogLayerMixinAugmentType = /* @__PURE__ */ function(LogLayerMixinAugmentType) {
26
- /**
27
- * Mixin extends the LogBuilder prototype
28
- */
29
- LogLayerMixinAugmentType["LogBuilder"] = "LogBuilder";
30
- /**
31
- * Mixin extends the LogLayer prototype
32
- */
33
- LogLayerMixinAugmentType["LogLayer"] = "LogLayer";
34
- return LogLayerMixinAugmentType;
35
- }({});
36
- //#endregion
1
+ import { LogLayerMixinAugmentType } from "loglayer";
37
2
  //#region src/mixin.ts
38
3
  /**
39
4
  * Log levels that default to rate=1 (error/fatal) — can be overridden via `perLevel` or callback.
@@ -128,7 +93,8 @@ function createWideEventMixin(options) {
128
93
  rate: options.sampling.rate,
129
94
  perLevel: snapshotPerLevel,
130
95
  emitLevel: options.sampling.emitLevel,
131
- shouldEmit: options.sampling.shouldEmit
96
+ shouldEmit: options.sampling.shouldEmit,
97
+ forceKeep: options.sampling.forceKeep
132
98
  } : void 0;
133
99
  function defaultErrorSerializer(err) {
134
100
  if (err instanceof Error) return {
@@ -223,23 +189,59 @@ function createWideEventMixin(options) {
223
189
  return self;
224
190
  }
225
191
  /**
192
+ * Logs a thrown sampling callback via console.error, gated on `consoleDebug`.
193
+ * Matches loglayer's emit-path error convention (see LogLayer.ts:1457).
194
+ */
195
+ function logCallbackError(callbackName, err, self) {
196
+ if (self.getConfig?.()?.consoleDebug) console.error(`[LogLayer] wide-events ${callbackName} callback threw; falling back:`, err);
197
+ }
198
+ /**
199
+ * Single sampling decision for an emit. Only called when a `forceKeep` or
200
+ * `shouldEmit` callback is configured (the no-callback case is handled by the
201
+ * pre-build fast-path in emitWideEventImpl).
202
+ *
203
+ * - `forceKeep` runs first (OR logic): true → keep; throws → fail-safe (log +
204
+ * fall through); false → fall through.
205
+ * - Standard logic: rate AND `shouldEmit` (shouldEmit throws → fail-open, log
206
+ * + keep). Without `shouldEmit`, rate alone decides.
207
+ *
208
+ * Invokes `runRateSampling` at most once.
209
+ */
210
+ function evaluateSampling(wideData, level, self) {
211
+ if (samplingConfig?.forceKeep) try {
212
+ if (samplingConfig.forceKeep({
213
+ wideData,
214
+ level
215
+ })) return true;
216
+ } catch (err) {
217
+ logCallbackError("forceKeep", err, self);
218
+ }
219
+ if (samplingConfig?.shouldEmit) try {
220
+ const callbackOk = samplingConfig.shouldEmit({
221
+ wideData,
222
+ level
223
+ });
224
+ return runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel) && callbackOk;
225
+ } catch (err) {
226
+ logCallbackError("shouldEmit", err, self);
227
+ return true;
228
+ }
229
+ return runRateSampling(level, samplingConfig?.strategy, samplingConfig?.rate, samplingConfig?.perLevel);
230
+ }
231
+ /**
226
232
  * Implementation for emitWideEvent
227
233
  */
228
234
  function emitWideEventImpl(config, self) {
229
235
  const defaultLevel = samplingConfig?.emitLevel ?? "info";
230
236
  const level = config.level ?? defaultLevel;
231
- if (samplingConfig && !samplingConfig.shouldEmit && !runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel)) return;
237
+ if (samplingConfig && !samplingConfig.shouldEmit && !samplingConfig.forceKeep && !runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel)) return;
232
238
  const store = asyncContext.getStore();
233
239
  const wideEventData = {};
234
240
  if (includeContext && store?._llContext) Object.assign(wideEventData, store._llContext);
235
241
  if (store?._llWideEvents) Object.assign(wideEventData, store._llWideEvents);
236
- if (samplingConfig?.shouldEmit) try {
237
- const callbackOk = samplingConfig.shouldEmit({
238
- wideData: wideEventData,
239
- level
240
- });
241
- if (!runRateSampling(level, samplingConfig.strategy, samplingConfig.rate, samplingConfig.perLevel) || !callbackOk) return;
242
- } catch {}
242
+ if (samplingConfig && (samplingConfig.forceKeep || samplingConfig.shouldEmit)) {
243
+ if (!evaluateSampling(wideEventData, level, self)) return;
244
+ }
243
245
  const metadataToEmit = wideEventField ? { [wideEventField]: wideEventData } : wideEventData;
244
246
  self.raw({
245
247
  logLevel: level,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@loglayer/mixin-wide-events",
3
3
  "description": "Adds wide event logging functionality to LogLayer for comprehensive, self-contained log entries.",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "module": "./dist/index.mjs",
@@ -41,7 +41,9 @@
41
41
  "@loglayer/shared": "4.3.0",
42
42
  "loglayer": "9.3.0"
43
43
  },
44
- "peerDependencies": {},
44
+ "peerDependencies": {
45
+ "loglayer": ">=9.0.0"
46
+ },
45
47
  "bugs": "https://github.com/loglayer/loglayer/issues",
46
48
  "files": [
47
49
  "dist"