@dash0/sdk-web 0.13.2 → 0.13.3

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.
@@ -1,9 +1,11 @@
1
1
  import { vars } from "../vars";
2
- import { noop, warn, fetch } from "../utils";
2
+ import { noop, warn, fetch, debug } from "../utils";
3
3
 
4
4
  const BEACON_BODY_SIZE_LIMIT = 60000;
5
5
 
6
6
  export async function send(path: string, body: unknown): Promise<void> {
7
+ debug("Transmitting telemetry to endpoints", body);
8
+
7
9
  const jsonString = JSON.stringify(body);
8
10
  let requestBody: ArrayBuffer | string = jsonString;
9
11
  let byteLength = jsonString.length;
@@ -1,25 +1,40 @@
1
1
  import { noop } from "./fn";
2
2
 
3
+ const logLevels = {
4
+ debug: 0,
5
+ info: 1,
6
+ warn: 2,
7
+ error: 3,
8
+ } as const;
9
+
10
+ export type LogLevel = keyof typeof logLevels;
11
+
12
+ let activeLogLevel: number = logLevels.warn;
13
+
14
+ /**
15
+ * Changes the logging verbosity of Dash0's web SDK. By default, only warnings and errors are logged.
16
+ */
17
+ export function setActiveLogLevel(level: LogLevel) {
18
+ activeLogLevel = logLevels[level] ?? logLevels.warn;
19
+ }
20
+
3
21
  type Logger = (...args: any[]) => void;
4
22
 
5
- export const log: Logger = createLogger("log");
6
23
  export const info: Logger = createLogger("info");
7
24
  export const warn: Logger = createLogger("warn");
8
25
  export const error: Logger = createLogger("error");
9
26
  export const debug: Logger = createLogger("debug");
10
27
 
11
- function createLogger(method: Extract<keyof Console, "log" | "info" | "warn" | "error" | "debug">): Logger {
12
- if (typeof console === "undefined" || typeof console.log !== "function" || typeof console.log.apply !== "function") {
13
- return noop;
14
- }
28
+ function createLogger(logLevel: LogLevel): Logger {
29
+ if (typeof console !== "undefined" && console[logLevel] && typeof console[logLevel].apply === "function") {
30
+ const numericLogLevel = logLevels[logLevel];
15
31
 
16
- if (console[method] && typeof console[method].apply === "function") {
17
32
  return function () {
18
- console[method].apply(console, arguments as any);
33
+ if (numericLogLevel >= activeLogLevel) {
34
+ console[logLevel].apply(console, arguments as any);
35
+ }
19
36
  };
20
37
  }
21
38
 
22
- return function () {
23
- console.log.apply(console, arguments as any);
24
- };
39
+ return noop;
25
40
  }