@travetto/log 2.1.2 → 2.2.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/README.md +6 -6
- package/package.json +3 -3
- package/src/appender/console.ts +1 -1
- package/src/appender/file.ts +1 -1
- package/src/formatter/json.ts +1 -1
- package/src/formatter/line.ts +2 -2
- package/src/service.ts +6 -5
- package/src/types.ts +1 -1
- package/src/util.ts +4 -4
- package/support/phase.init.ts +1 -1
- package/support/transformer.logger.ts +13 -9
package/README.md
CHANGED
|
@@ -48,8 +48,8 @@ export function work() {
|
|
|
48
48
|
|
|
49
49
|
try {
|
|
50
50
|
1 / 0;
|
|
51
|
-
} catch (
|
|
52
|
-
console.error('Divide by zero', { error:
|
|
51
|
+
} catch (err) {
|
|
52
|
+
console.error('Divide by zero', { error: err });
|
|
53
53
|
}
|
|
54
54
|
console.debug('End Work');
|
|
55
55
|
}
|
|
@@ -65,8 +65,8 @@ function work() {
|
|
|
65
65
|
try {
|
|
66
66
|
1 / 0;
|
|
67
67
|
}
|
|
68
|
-
catch (
|
|
69
|
-
ᚕlg("error", { file: ᚕsrc(__filename), line: 7, scope: "work" }, 'Divide by zero', { error:
|
|
68
|
+
catch (err) {
|
|
69
|
+
ᚕlg("error", { file: ᚕsrc(__filename), line: 7, scope: "work" }, 'Divide by zero', { error: err });
|
|
70
70
|
}
|
|
71
71
|
ᚕlg("debug", { file: ᚕsrc(__filename), line: 9, scope: "work" }, 'End Work');
|
|
72
72
|
}
|
|
@@ -85,8 +85,8 @@ function work() {
|
|
|
85
85
|
try {
|
|
86
86
|
1 / 0;
|
|
87
87
|
}
|
|
88
|
-
catch (
|
|
89
|
-
ᚕlg("error", { file: ᚕsrc(__filename), line: 5, scope: "work" }, 'Divide by Zero', { error:
|
|
88
|
+
catch (err) {
|
|
89
|
+
ᚕlg("error", { file: ᚕsrc(__filename), line: 5, scope: "work" }, 'Divide by Zero', { error: err });
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
exports.work = work;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/log",
|
|
3
3
|
"displayName": "Logging",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"description": "Logging framework that integrates at the console.log level.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"directory": "module/log"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/base": "^2.
|
|
31
|
-
"@travetto/transformer": "^2.
|
|
30
|
+
"@travetto/base": "^2.2.0",
|
|
31
|
+
"@travetto/transformer": "^2.2.0"
|
|
32
32
|
},
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
package/src/appender/console.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface ConsoleAppenderOpts {
|
|
|
13
13
|
export class ConsoleAppender implements Appender {
|
|
14
14
|
constructor(opts: ConsoleAppenderOpts = {}) { }
|
|
15
15
|
|
|
16
|
-
append(level: LogLevel, message: string) {
|
|
16
|
+
append(level: LogLevel, message: string): void {
|
|
17
17
|
console;
|
|
18
18
|
}
|
|
19
19
|
}
|
package/src/appender/file.ts
CHANGED
package/src/formatter/json.ts
CHANGED
package/src/formatter/line.ts
CHANGED
|
@@ -43,7 +43,7 @@ export class LineFormatter implements Formatter {
|
|
|
43
43
|
};
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
pretty(ev: LogEvent, o: unknown) {
|
|
46
|
+
pretty(ev: LogEvent, o: unknown): string {
|
|
47
47
|
return util.inspect(o, {
|
|
48
48
|
showHidden: ev.level === 'debug',
|
|
49
49
|
depth: 4,
|
|
@@ -55,7 +55,7 @@ export class LineFormatter implements Formatter {
|
|
|
55
55
|
/**
|
|
56
56
|
* Format an event into a single line
|
|
57
57
|
*/
|
|
58
|
-
format(ev: LogEvent) {
|
|
58
|
+
format(ev: LogEvent): string {
|
|
59
59
|
const opts = this.#opts;
|
|
60
60
|
const out = [];
|
|
61
61
|
|
package/src/service.ts
CHANGED
|
@@ -21,6 +21,7 @@ class $Logger {
|
|
|
21
21
|
/**
|
|
22
22
|
* Should we enrich the console by default
|
|
23
23
|
*/
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
24
25
|
readonly #logFormat: 'line' | 'json' = EnvUtil.get('TRV_LOG_FORMAT', 'line') as 'line';
|
|
25
26
|
|
|
26
27
|
readonly #logFile?: string = EnvUtil.get('TRV_LOG_FILE');
|
|
@@ -46,7 +47,7 @@ class $Logger {
|
|
|
46
47
|
/**
|
|
47
48
|
* Initialize
|
|
48
49
|
*/
|
|
49
|
-
init() {
|
|
50
|
+
init(): void {
|
|
50
51
|
if (AppManifest.env.debug.status !== false) {
|
|
51
52
|
delete this.#exclude.debug;
|
|
52
53
|
const filter = LogUtil.buildFilter(AppManifest.env.debug.value ?? '@app');
|
|
@@ -71,7 +72,7 @@ class $Logger {
|
|
|
71
72
|
/**
|
|
72
73
|
* Add log event listener
|
|
73
74
|
*/
|
|
74
|
-
listen(key: string | symbol, handler: (ev: LogEvent) => void) {
|
|
75
|
+
listen(key: string | symbol, handler: (ev: LogEvent) => void): void {
|
|
75
76
|
this.removeListener(key);
|
|
76
77
|
this.#listenerMap.set(key, handler);
|
|
77
78
|
this.#listeners.push(handler);
|
|
@@ -82,14 +83,14 @@ class $Logger {
|
|
|
82
83
|
* @param formatter
|
|
83
84
|
* @param appender Defaults to console appender unless specified
|
|
84
85
|
*/
|
|
85
|
-
listenDefault(formatter: Formatter, appender?: Appender) {
|
|
86
|
+
listenDefault(formatter: Formatter, appender?: Appender): void {
|
|
86
87
|
this.listen(DefaultLoggerⲐ, LogUtil.buildListener(formatter, appender ?? new ConsoleAppender()));
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
/**
|
|
90
91
|
* Clear all listeners
|
|
91
92
|
*/
|
|
92
|
-
removeAll() {
|
|
93
|
+
removeAll(): void {
|
|
93
94
|
this.#listenerMap.clear();
|
|
94
95
|
this.#listeners = [];
|
|
95
96
|
}
|
|
@@ -97,7 +98,7 @@ class $Logger {
|
|
|
97
98
|
/**
|
|
98
99
|
* Remove specific listener
|
|
99
100
|
*/
|
|
100
|
-
removeListener(key: string | symbol) {
|
|
101
|
+
removeListener(key: string | symbol): void {
|
|
101
102
|
const handler = this.#listenerMap.get(key);
|
|
102
103
|
if (handler) {
|
|
103
104
|
this.#listenerMap.delete(key);
|
package/src/types.ts
CHANGED
package/src/util.ts
CHANGED
|
@@ -9,7 +9,7 @@ export class LogUtil {
|
|
|
9
9
|
/**
|
|
10
10
|
* Produce an event listener
|
|
11
11
|
*/
|
|
12
|
-
static buildListener(formatter: Formatter, appender: Appender, filter?: (ev: LogEvent) => boolean) {
|
|
12
|
+
static buildListener(formatter: Formatter, appender: Appender, filter?: (ev: LogEvent) => boolean): (ev: LogEvent) => void {
|
|
13
13
|
if (filter) {
|
|
14
14
|
return (ev: LogEvent) => {
|
|
15
15
|
if (filter(ev)) {
|
|
@@ -24,7 +24,7 @@ export class LogUtil {
|
|
|
24
24
|
/**
|
|
25
25
|
* Build a filter element
|
|
26
26
|
*/
|
|
27
|
-
static buildFilterPart(p: string) {
|
|
27
|
+
static buildFilterPart(p: string): { key: 'exc' | 'inc', filter: string[] } {
|
|
28
28
|
const [, neg, prop] = p.match(/(-|[+])?(.*)/)!;
|
|
29
29
|
const cleaned = (/^.*:[^\/]*[^*]$/.test(prop) ? `${prop}/*` : prop).replace(/([\/.])/g, a => `\\${a}`);
|
|
30
30
|
const key: 'exc' | 'inc' = neg ? 'exc' : 'inc';
|
|
@@ -46,8 +46,8 @@ export class LogUtil {
|
|
|
46
46
|
/**
|
|
47
47
|
* Convert filter into test function for filtering
|
|
48
48
|
*/
|
|
49
|
-
static buildFilter(v: string) {
|
|
50
|
-
const config
|
|
49
|
+
static buildFilter(v: string): ((file: string) => boolean) | undefined {
|
|
50
|
+
const config: { inc: string[], exc: string[] } = { inc: [], exc: [] };
|
|
51
51
|
const { inc, exc } = config;
|
|
52
52
|
|
|
53
53
|
for (const p of v.split(/\s*,\s*/)) {
|
package/support/phase.init.ts
CHANGED
|
@@ -18,25 +18,25 @@ export class LoggerTransformer {
|
|
|
18
18
|
|
|
19
19
|
static [TransformerId] = '@trv:log';
|
|
20
20
|
|
|
21
|
-
static initState(state: CustomState) {
|
|
21
|
+
static initState(state: CustomState): void {
|
|
22
22
|
state.scope = state.scope ?? [];
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
@OnClass()
|
|
26
|
-
static onClass(state: CustomState, node: ts.ClassDeclaration) {
|
|
26
|
+
static onClass(state: CustomState, node: ts.ClassDeclaration): typeof node {
|
|
27
27
|
this.initState(state);
|
|
28
28
|
state.scope.push({ type: 'class', name: node.name?.text ?? 'unknown' });
|
|
29
29
|
return node;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
@AfterClass()
|
|
33
|
-
static afterClass(state: CustomState, node: ts.ClassDeclaration) {
|
|
33
|
+
static afterClass(state: CustomState, node: ts.ClassDeclaration): typeof node {
|
|
34
34
|
state.scope.pop();
|
|
35
35
|
return node;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
@OnMethod()
|
|
39
|
-
static onMethod(state: CustomState, node: ts.MethodDeclaration) {
|
|
39
|
+
static onMethod(state: CustomState, node: ts.MethodDeclaration): typeof node {
|
|
40
40
|
this.initState(state);
|
|
41
41
|
let name = 'unknown';
|
|
42
42
|
if (ts.isIdentifier(node.name)) {
|
|
@@ -47,33 +47,37 @@ export class LoggerTransformer {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
@AfterMethod()
|
|
50
|
-
static afterMethod(state: CustomState, node: ts.MethodDeclaration) {
|
|
50
|
+
static afterMethod(state: CustomState, node: ts.MethodDeclaration): typeof node {
|
|
51
51
|
state.scope.pop();
|
|
52
52
|
return node;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
@OnFunction()
|
|
56
|
-
static onFunction(state: CustomState, node: ts.FunctionDeclaration | ts.FunctionExpression) {
|
|
56
|
+
static onFunction(state: CustomState, node: ts.FunctionDeclaration | ts.FunctionExpression): typeof node {
|
|
57
57
|
this.initState(state);
|
|
58
58
|
state.scope.push({ type: 'function', name: node.name?.text ?? 'unknown' });
|
|
59
59
|
return node;
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
@AfterFunction()
|
|
63
|
-
static afterFunction(state: CustomState, node: ts.FunctionDeclaration | ts.FunctionExpression) {
|
|
63
|
+
static afterFunction(state: CustomState, node: ts.FunctionDeclaration | ts.FunctionExpression): typeof node {
|
|
64
64
|
state.scope.pop();
|
|
65
65
|
return node;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
@OnCall()
|
|
69
|
-
static onDebugCall(state: CustomState, node: ts.CallExpression) {
|
|
69
|
+
static onDebugCall(state: CustomState, node: ts.CallExpression): typeof node | ts.Identifier {
|
|
70
70
|
if (!ts.isIdentifier(node.expression) || node.expression.text !== ConsoleManager.key) {
|
|
71
71
|
return node;
|
|
72
72
|
}
|
|
73
73
|
const arg = CoreUtil.getArgument(node);
|
|
74
74
|
if (arg) {
|
|
75
75
|
// Okay since we create the object ourselves in ConsoleManager
|
|
76
|
-
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
77
|
+
if (!ts.isStringLiteral(arg)) {
|
|
78
|
+
return node;
|
|
79
|
+
}
|
|
80
|
+
const level = LiteralUtil.toLiteral(arg, false);
|
|
77
81
|
if (AppManifest.prod && level === 'debug') {
|
|
78
82
|
return state.createIdentifier('undefined'); // Lose debug logging if in prod
|
|
79
83
|
} else {
|