@holz/log-collector 0.8.0-rc.1 → 0.8.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@holz/log-collector",
3
- "version": "0.8.0-rc.1",
3
+ "version": "0.8.1",
4
4
  "description": "Send all logs to a central collector",
5
5
  "type": "module",
6
6
  "repository": {
@@ -22,8 +22,7 @@
22
22
  "license": "MIT",
23
23
  "sideEffects": false,
24
24
  "files": [
25
- "dist",
26
- "src"
25
+ "dist"
27
26
  ],
28
27
  "keywords": [
29
28
  "holz",
@@ -37,10 +36,10 @@
37
36
  "test:types": "tsc"
38
37
  },
39
38
  "peerDependencies": {
40
- "@holz/core": "^0.8.0-rc.2"
39
+ "@holz/core": "^0.8.0"
41
40
  },
42
41
  "devDependencies": {
43
- "@holz/core": "^0.8.0-rc.2",
42
+ "@holz/core": "^0.8.0",
44
43
  "@types/node": "^22.0.0",
45
44
  "@vitest/coverage-v8": "^3.0.8",
46
45
  "typescript": "^5.8.2",
@@ -48,6 +47,5 @@
48
47
  "vite-plugin-dts": "^4.5.3",
49
48
  "vite-tsconfig-paths": "^5.1.4",
50
49
  "vitest": "^3.0.8"
51
- },
52
- "stableVersion": "0.7.0"
50
+ }
53
51
  }
@@ -1,9 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`JSON backend > prints the logs to the writable stream 1`] = `
4
- "{"level":"debug","time":"2020-06-15T12:00:00.000Z","msg":"shout"}
5
- {"level":"info","time":"2020-06-15T12:00:00.000Z","msg":"normal"}
6
- {"level":"warn","time":"2020-06-15T12:00:00.000Z","msg":"hmmmm"}
7
- {"level":"error","time":"2020-06-15T12:00:00.000Z","msg":"oh no"}
8
- "
9
- `;
@@ -1,76 +0,0 @@
1
- import { createLogger, level } from '@holz/core';
2
- import {
3
- createLogCollector,
4
- unsetGlobalLogCollector,
5
- setGlobalLogCollector,
6
- } from '../index';
7
-
8
- const CURRENT_TIME = new Date('2010-10-01T00:00:00Z').getTime();
9
-
10
- vi.setSystemTime(CURRENT_TIME);
11
-
12
- describe('log collector', () => {
13
- afterEach(() => {
14
- unsetGlobalLogCollector();
15
- });
16
-
17
- describe('createLogInterceptor', () => {
18
- it('sends logs to the default backend', () => {
19
- const fallback = vi.fn();
20
- const logger = createLogger(createLogCollector({ fallback }));
21
-
22
- logger.info('test message');
23
- expect(fallback).toHaveBeenCalledOnce();
24
- expect(fallback).toHaveBeenCalledWith({
25
- timestamp: CURRENT_TIME,
26
- message: 'test message',
27
- level: level.info,
28
- origin: [],
29
- context: {},
30
- });
31
- });
32
-
33
- it('sends logs to the interceptor if defined', () => {
34
- const interceptor = vi.fn();
35
- const fallback = vi.fn();
36
- const logger = createLogger(createLogCollector({ fallback }));
37
-
38
- setGlobalLogCollector({
39
- processor: interceptor,
40
- });
41
-
42
- logger.info('test message');
43
- expect(fallback).not.toHaveBeenCalled();
44
- expect(interceptor).toHaveBeenCalledOnce();
45
- expect(interceptor).toHaveBeenCalledWith({
46
- timestamp: CURRENT_TIME,
47
- message: 'test message',
48
- level: level.info,
49
- origin: [],
50
- context: {},
51
- });
52
- });
53
-
54
- it('uses the default backend if the interceptor does not match', () => {
55
- const interceptor = vi.fn();
56
- const fallback = vi.fn();
57
- const logger = createLogger(createLogCollector({ fallback }));
58
-
59
- setGlobalLogCollector({
60
- processor: interceptor,
61
- condition: () => false,
62
- });
63
-
64
- logger.info('test message');
65
- expect(interceptor).not.toHaveBeenCalled();
66
- expect(fallback).toHaveBeenCalledOnce();
67
- expect(fallback).toHaveBeenCalledWith({
68
- timestamp: CURRENT_TIME,
69
- message: 'test message',
70
- level: level.info,
71
- origin: [],
72
- context: {},
73
- });
74
- });
75
- });
76
- });
package/src/global.ts DELETED
@@ -1,45 +0,0 @@
1
- import type { Log, LogProcessor } from '@holz/core';
2
-
3
- const MATCH_ALL = () => true;
4
-
5
- /**
6
- * Overrides the default log destination for all loggers in the app. Call this
7
- * early on startup. If logs are sent before registering the collector, they
8
- * will be lost.
9
- */
10
- export const setGlobalLogCollector = ({
11
- processor,
12
- condition: match = MATCH_ALL,
13
- }: InterceptorConfig) => {
14
- globalScope[COLLECTOR_SYMBOL] = { processor, condition: match };
15
- };
16
-
17
- /**
18
- * Remove the global log collector restoring defaults. Safe to call regardless
19
- * of whether one is set.
20
- */
21
- export const unsetGlobalLogCollector = () => {
22
- delete globalScope[COLLECTOR_SYMBOL];
23
- };
24
-
25
- interface InterceptorConfig {
26
- /** Plugin handling all logs captured by the interceptor. */
27
- processor: LogProcessor;
28
-
29
- /**
30
- * Only capture logs matching this function. Return `false` to use the
31
- * default log destination. Defaults to capture all logs.
32
- */
33
- condition?: (log: Log) => boolean;
34
- }
35
-
36
- // As of 2025-04-12 there is no way to declare a symbol on `globalThis`.
37
- export const globalScope = globalThis as unknown as GlobalContext;
38
-
39
- // Stored in the global scope. Uses the symbol registry in case multiple
40
- // versions of Holz are loaded in the same context.
41
- export const COLLECTOR_SYMBOL = Symbol.for('@holz/log-collector');
42
-
43
- interface GlobalContext {
44
- [COLLECTOR_SYMBOL]?: Required<InterceptorConfig>;
45
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { createLogCollector } from './log-collector';
2
- export { setGlobalLogCollector, unsetGlobalLogCollector } from './global';
@@ -1,22 +0,0 @@
1
- import type { LogProcessor } from '@holz/core';
2
- import { globalScope, COLLECTOR_SYMBOL } from './global';
3
-
4
- /**
5
- * Redirects logs to a global collector if one is configured. This is designed
6
- * with apps in mind, which may want to aggregate logs from multiple sources.
7
- */
8
- export const createLogCollector =
9
- (config: Config): LogProcessor =>
10
- (log) => {
11
- const collector = globalScope[COLLECTOR_SYMBOL];
12
-
13
- if (collector?.condition(log)) {
14
- collector.processor(log);
15
- } else {
16
- config.fallback(log);
17
- }
18
- };
19
-
20
- export interface Config {
21
- fallback: LogProcessor;
22
- }