@holz/log-collector 0.8.0-rc.1 → 0.8.2-rc.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/LICENSE +21 -0
- package/package.json +6 -7
- package/src/__tests__/__snapshots__/json-backend.test.ts.snap +0 -9
- package/src/__tests__/log-collector.test.ts +0 -76
- package/src/global.ts +0 -45
- package/src/index.ts +0 -2
- package/src/log-collector.ts +0 -22
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Jesse Gibson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/log-collector",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2-rc.0",
|
|
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
|
|
39
|
+
"@holz/core": "^0.8.0"
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
|
-
"@holz/core": "^0.8.
|
|
42
|
+
"@holz/core": "^0.8.2-rc.0",
|
|
44
43
|
"@types/node": "^22.0.0",
|
|
45
44
|
"@vitest/coverage-v8": "^3.0.8",
|
|
46
45
|
"typescript": "^5.8.2",
|
|
@@ -49,5 +48,5 @@
|
|
|
49
48
|
"vite-tsconfig-paths": "^5.1.4",
|
|
50
49
|
"vitest": "^3.0.8"
|
|
51
50
|
},
|
|
52
|
-
"
|
|
53
|
-
}
|
|
51
|
+
"gitHead": "59a13334498d9c3f0952235ed6cb59e493558824"
|
|
52
|
+
}
|
|
@@ -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
package/src/log-collector.ts
DELETED
|
@@ -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
|
-
}
|