@holz/console-backend 0.3.0 → 0.5.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=1e3,c=60*a,u=60*c,l=24*u,f=(s,r=s)=>{const e=s.getTime()-r.getTime(),t=Math.abs(e),n=e<0?"":"+";return t>=l?`${n}${Math.round(e/l)}d`:t>=u?`${n}${Math.round(e/u)}h`:t>=c?`${n}${Math.round(e/c)}m`:t>=a?`${n}${Math.round(e/a)}s`:`${n}${e}ms`};function $(s={}){const r=s.console??console;let e;return t=>{const n=new Date,i=[{include:!0,format:"%s",values:[t.message]},{include:Object.keys(t.context).length>0,format:"%o",values:[t.context]},{include:!0,format:"%c%s",values:["color: gray",f(n,e)]},{include:t.origin.length>0,format:"%c%s",values:["color: rgba(128, 128, 128, 0.6); font-style: italic",t.origin.join(":")]}].filter(o=>o.include),m=i.map(o=>o.format).join(" "),d=i.flatMap(o=>o.values);r[t.level](m,...d),e=n}}exports.createConsoleBackend=$;
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
const i = (
|
|
2
|
-
const e =
|
|
3
|
-
return
|
|
1
|
+
const i = (s, r = s) => {
|
|
2
|
+
const e = s.getTime() - r.getTime(), t = Math.abs(e), n = e < 0 ? "" : "+";
|
|
3
|
+
return t >= 864e5 ? `${n}${Math.round(e / 864e5)}d` : t >= 36e5 ? `${n}${Math.round(e / 36e5)}h` : t >= 6e4 ? `${n}${Math.round(e / 6e4)}m` : t >= 1e3 ? `${n}${Math.round(e / 1e3)}s` : `${n}${e}ms`;
|
|
4
4
|
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const e = this.stamp, n = new Date();
|
|
11
|
-
this.stamp = n;
|
|
12
|
-
const s = [
|
|
5
|
+
function l(s = {}) {
|
|
6
|
+
const r = s.console ?? console;
|
|
7
|
+
let e;
|
|
8
|
+
return (t) => {
|
|
9
|
+
const n = new Date(), c = [
|
|
13
10
|
{
|
|
14
11
|
include: !0,
|
|
15
12
|
format: "%s",
|
|
@@ -34,11 +31,10 @@ class u {
|
|
|
34
31
|
t.origin.join(":")
|
|
35
32
|
]
|
|
36
33
|
}
|
|
37
|
-
].filter((o) => o.include), a =
|
|
38
|
-
|
|
39
|
-
}
|
|
34
|
+
].filter((o) => o.include), a = c.map((o) => o.format).join(" "), u = c.flatMap((o) => o.values);
|
|
35
|
+
r[t.level](a, ...u), e = n;
|
|
36
|
+
};
|
|
40
37
|
}
|
|
41
38
|
export {
|
|
42
|
-
|
|
43
|
-
u as default
|
|
39
|
+
l as createConsoleBackend
|
|
44
40
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@holz/console-backend",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A console backend for Holz",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/holz-console-backend.cjs",
|
|
@@ -39,10 +39,10 @@
|
|
|
39
39
|
"test:types": "tsc"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@holz/core": "0.
|
|
42
|
+
"@holz/core": "^0.5.0",
|
|
43
43
|
"@types/node": "^18.14.0",
|
|
44
|
-
"@vitest/coverage-c8": "0.28.5",
|
|
45
|
-
"typescript": "4.9.5",
|
|
44
|
+
"@vitest/coverage-c8": "^0.28.5",
|
|
45
|
+
"typescript": "^4.9.5",
|
|
46
46
|
"vite": "^4.0.0",
|
|
47
47
|
"vitest": "^0.28.5"
|
|
48
48
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { format } from 'util';
|
|
2
2
|
import { createLogger } from '@holz/core';
|
|
3
3
|
import type { MinimalConsole } from '../console-backend';
|
|
4
|
-
import
|
|
4
|
+
import { createConsoleBackend } from '../console-backend';
|
|
5
5
|
|
|
6
6
|
class MockConsole implements MinimalConsole {
|
|
7
7
|
private fmt = (level: string, ...strings: Array<unknown>) => {
|
|
@@ -30,7 +30,7 @@ describe('Console backend', () => {
|
|
|
30
30
|
|
|
31
31
|
it('prints messages to the console', () => {
|
|
32
32
|
const output = new MockConsole();
|
|
33
|
-
const backend =
|
|
33
|
+
const backend = createConsoleBackend({ console: output });
|
|
34
34
|
|
|
35
35
|
const logger = createLogger(backend);
|
|
36
36
|
logger.info('hello world');
|
|
@@ -42,7 +42,7 @@ describe('Console backend', () => {
|
|
|
42
42
|
|
|
43
43
|
it('includes the log namespace', () => {
|
|
44
44
|
const output = new MockConsole();
|
|
45
|
-
const backend =
|
|
45
|
+
const backend = createConsoleBackend({ console: output });
|
|
46
46
|
const logger = createLogger(backend)
|
|
47
47
|
.namespace('my-lib')
|
|
48
48
|
.namespace('MyClass');
|
|
@@ -56,7 +56,7 @@ describe('Console backend', () => {
|
|
|
56
56
|
|
|
57
57
|
it('includes the log context', () => {
|
|
58
58
|
const output = new MockConsole();
|
|
59
|
-
const backend =
|
|
59
|
+
const backend = createConsoleBackend({ console: output });
|
|
60
60
|
const logger = createLogger(backend);
|
|
61
61
|
|
|
62
62
|
logger.info('creating session', { sessionId: 3109 });
|
|
@@ -71,7 +71,7 @@ describe('Console backend', () => {
|
|
|
71
71
|
|
|
72
72
|
it('does not include the log context if it is empty', () => {
|
|
73
73
|
const output = new MockConsole();
|
|
74
|
-
const backend =
|
|
74
|
+
const backend = createConsoleBackend({ console: output });
|
|
75
75
|
const logger = createLogger(backend);
|
|
76
76
|
|
|
77
77
|
logger.warn('activating death ray', {});
|
|
@@ -84,7 +84,7 @@ describe('Console backend', () => {
|
|
|
84
84
|
|
|
85
85
|
it('prints the time since the last log', () => {
|
|
86
86
|
const output = new MockConsole();
|
|
87
|
-
const backend =
|
|
87
|
+
const backend = createConsoleBackend({ console: output });
|
|
88
88
|
const logger = createLogger(backend);
|
|
89
89
|
|
|
90
90
|
logger.info('first message');
|
|
@@ -108,7 +108,7 @@ describe('Console backend', () => {
|
|
|
108
108
|
],
|
|
109
109
|
])('avoids changing %s messages', (method, message, namespace, pipe) => {
|
|
110
110
|
const output = new MockConsole();
|
|
111
|
-
const backend =
|
|
111
|
+
const backend = createConsoleBackend({ console: output });
|
|
112
112
|
const logger = namespace.reduce(
|
|
113
113
|
(logger, ns) => logger.namespace(ns),
|
|
114
114
|
createLogger(backend)
|
package/src/console-backend.ts
CHANGED
|
@@ -5,21 +5,13 @@ import { timeDelta } from './time-delta';
|
|
|
5
5
|
* A backend that pretty-prints logs to a browser console, or any
|
|
6
6
|
* remote-attached console.
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export function createConsoleBackend(options: Options = {}): LogProcessor {
|
|
9
|
+
const output = options.console ?? console;
|
|
10
|
+
let lastTimestamp: Date;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
this.console = options.console ?? console;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
processLog(log: Log) {
|
|
17
|
-
const lastTimestamp = this.stamp;
|
|
12
|
+
return (log: Log) => {
|
|
18
13
|
const now = new Date();
|
|
19
14
|
|
|
20
|
-
// Track the time spent between logs.
|
|
21
|
-
this.stamp = now;
|
|
22
|
-
|
|
23
15
|
const segments = [
|
|
24
16
|
{
|
|
25
17
|
include: true,
|
|
@@ -50,8 +42,11 @@ export default class ConsoleBackend implements LogProcessor {
|
|
|
50
42
|
const values = segments.flatMap<unknown>((segment) => segment.values);
|
|
51
43
|
|
|
52
44
|
// Browsers have UIs for filtering by log level. Leverage that.
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
output[log.level](format, ...values);
|
|
46
|
+
|
|
47
|
+
// Track the time spent between logs.
|
|
48
|
+
lastTimestamp = now;
|
|
49
|
+
};
|
|
55
50
|
}
|
|
56
51
|
|
|
57
52
|
interface Options {
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { createConsoleBackend } from './console-backend';
|