@travetto/log 7.1.4 → 8.0.0-alpha.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 +3 -2
- package/package.json +4 -4
- package/src/formatter/google.ts +2 -1
- package/src/formatter/json.ts +3 -2
- package/src/formatter/line.ts +6 -1
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ yarn add @travetto/log
|
|
|
16
16
|
This module provides logging functionality, building upon [ConsoleManager](https://github.com/travetto/travetto/tree/main/module/runtime/src/console.ts#L43) in the [Runtime](https://github.com/travetto/travetto/tree/main/module/runtime#readme "Runtime for travetto applications.") module. This is all ultimately built upon [console](https://nodejs.org/api/console.html) operations. The logging infrastructure is built upon the [Dependency Injection](https://github.com/travetto/travetto/tree/main/module/di#readme "Dependency registration/management and injection support.") system, and so new loggers can be created that rely upon dependency injected services and sources.
|
|
17
17
|
|
|
18
18
|
## Extending the Common Logger
|
|
19
|
-
By default, the system ships with the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L13), and by default will leverage the [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L39) and the [ConsoleLogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/appender/console.ts#L7). The configuration [CommonLoggerConfig](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L13) provides two configuration variables that allows for switching out [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L39) for the [JsonLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/json.ts#
|
|
19
|
+
By default, the system ships with the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L13), and by default will leverage the [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L39) and the [ConsoleLogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/appender/console.ts#L7). The configuration [CommonLoggerConfig](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L13) provides two configuration variables that allows for switching out [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L39) for the [JsonLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/json.ts#L17), depending on the value of `CommonLoggerConfig.format`. Additionally the [ConsoleLogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/appender/console.ts#L7) can be swapped out for the [FileLogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/appender/file.ts#L11) depending on the value of `CommonLoggerConfig.output`.
|
|
20
20
|
|
|
21
21
|
**Code: Standard Logging Config**
|
|
22
22
|
```typescript
|
|
@@ -95,13 +95,14 @@ The [LogEvent](https://github.com/travetto/travetto/tree/main/module/log/src/typ
|
|
|
95
95
|
```typescript
|
|
96
96
|
import { Injectable } from '@travetto/di';
|
|
97
97
|
import type { LogEvent, Logger } from '@travetto/log';
|
|
98
|
+
import { JSONUtil } from '@travetto/runtime';
|
|
98
99
|
|
|
99
100
|
@Injectable()
|
|
100
101
|
export class CustomLogger implements Logger {
|
|
101
102
|
log(event: LogEvent): void {
|
|
102
103
|
const headers = new Headers();
|
|
103
104
|
headers.set('Content-Type', 'application/json');
|
|
104
|
-
const body =
|
|
105
|
+
const body = JSONUtil.toUTF8(event);
|
|
105
106
|
fetch('http://localhost:8080/log', { method: 'POST', headers, body, });
|
|
106
107
|
}
|
|
107
108
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/log",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0-alpha.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Logging framework that integrates at the console.log level.",
|
|
6
6
|
"keywords": [
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"directory": "module/log"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@travetto/config": "^
|
|
28
|
-
"@travetto/di": "^
|
|
29
|
-
"@travetto/terminal": "^
|
|
27
|
+
"@travetto/config": "^8.0.0-alpha.0",
|
|
28
|
+
"@travetto/di": "^8.0.0-alpha.0",
|
|
29
|
+
"@travetto/terminal": "^8.0.0-alpha.0"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
|
32
32
|
"displayName": "Logging"
|
package/src/formatter/google.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { JSONUtil } from '@travetto/runtime';
|
|
1
2
|
import { Injectable } from '@travetto/di';
|
|
2
3
|
|
|
3
4
|
import type { LogFormatter, LogEvent } from '../types.ts';
|
|
@@ -26,7 +27,7 @@ export class GoogleLogFormatter implements LogFormatter {
|
|
|
26
27
|
delete context.statusCode;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
return
|
|
30
|
+
return JSONUtil.toUTF8({
|
|
30
31
|
context,
|
|
31
32
|
'logging.googleapis.com/sourceLocation': { file: `${event.module}/${event.modulePath}`, line: event.line },
|
|
32
33
|
'logging.googleapis.com/labels': { module: event.module, scope: event.scope },
|
package/src/formatter/json.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Injectable } from '@travetto/di';
|
|
2
2
|
import { Config } from '@travetto/config';
|
|
3
|
+
import { JSONUtil } from '@travetto/runtime';
|
|
3
4
|
|
|
4
5
|
import type { LogEvent, LogFormatter } from '../types.ts';
|
|
5
6
|
import { LogFormatUtil } from './util.ts';
|
|
@@ -24,10 +25,10 @@ export class JsonLogFormatter implements LogFormatter {
|
|
|
24
25
|
const { message: _m, args: _a, ...rest } = event;
|
|
25
26
|
const message = LogFormatUtil.getLogMessage(event);
|
|
26
27
|
const context = LogFormatUtil.getContext(event);
|
|
27
|
-
return
|
|
28
|
+
return JSONUtil.toUTF8({
|
|
28
29
|
...rest,
|
|
29
30
|
...(message ? { message } : {}),
|
|
30
31
|
...(context ? { context } : {}),
|
|
31
|
-
},
|
|
32
|
+
}, { indent: this.config.jsonIndent });
|
|
32
33
|
}
|
|
33
34
|
}
|
package/src/formatter/line.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import util from 'node:util';
|
|
2
2
|
|
|
3
|
-
import { Env } from '@travetto/runtime';
|
|
3
|
+
import { Env, RuntimeIndex } from '@travetto/runtime';
|
|
4
4
|
import { Injectable } from '@travetto/di';
|
|
5
5
|
import { Config, EnvVar } from '@travetto/config';
|
|
6
6
|
import { Ignore } from '@travetto/schema';
|
|
@@ -43,6 +43,7 @@ export class LineLogFormatterConfig {
|
|
|
43
43
|
@EnvVar(Env.TRV_LOG_TIME.key)
|
|
44
44
|
time?: 's' | 'ms' | string;
|
|
45
45
|
|
|
46
|
+
links?: boolean;
|
|
46
47
|
colorize?: boolean;
|
|
47
48
|
align?: boolean;
|
|
48
49
|
level?: boolean;
|
|
@@ -62,6 +63,7 @@ export class LineLogFormatterConfig {
|
|
|
62
63
|
this.time ??= (!this.plain ? 'ms' : undefined);
|
|
63
64
|
this.plain ??= !StyleUtil.enabled;
|
|
64
65
|
this.colorize ??= !this.plain;
|
|
66
|
+
this.links ??= !this.plain;
|
|
65
67
|
this.location ??= !this.plain;
|
|
66
68
|
this.level ??= !this.plain;
|
|
67
69
|
this.align ??= !this.plain;
|
|
@@ -122,6 +124,9 @@ export class LineLogFormatter implements LogFormatter {
|
|
|
122
124
|
if (this.config.colorize) {
|
|
123
125
|
location = STYLES.location(location);
|
|
124
126
|
}
|
|
127
|
+
if (this.config.links) {
|
|
128
|
+
location = StyleUtil.link(location, `file://${RuntimeIndex.getModule(event.module)?.sourcePath}/${event.modulePath}${event.line ? `#${event.line}` : ''}`);
|
|
129
|
+
}
|
|
125
130
|
out.push(`[${location}]`);
|
|
126
131
|
}
|
|
127
132
|
|