@travetto/log 6.0.1 → 7.0.0-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/README.md +3 -6
- package/package.json +4 -4
- package/src/common.ts +11 -6
- package/src/formatter/util.ts +4 -1
- package/src/service.ts +5 -5
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#
|
|
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#L37) 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#L37) for the [JsonLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/json.ts#L16), 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
|
|
@@ -29,7 +29,7 @@ export class CommonLoggerConfig {
|
|
|
29
29
|
}
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
In addition to these simple overrides, the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#
|
|
32
|
+
In addition to these simple overrides, the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L13) can be extended by providing an implementation of either a [LogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L36) or [LogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L28), with the declared symbol of `LogCommonSymbol`.
|
|
33
33
|
|
|
34
34
|
**Code: Sample Common Formatter**
|
|
35
35
|
```typescript
|
|
@@ -127,13 +127,10 @@ import { LogDecorator, LogEvent } from '@travetto/log';
|
|
|
127
127
|
@Injectable()
|
|
128
128
|
export class CustomDecorator implements LogDecorator {
|
|
129
129
|
decorate(ev: LogEvent): LogEvent {
|
|
130
|
-
|
|
131
|
-
// Add memory usage, and hostname
|
|
132
|
-
Object.assign(ev.context ??= {}, {
|
|
130
|
+
ev.args.push({
|
|
133
131
|
memory: process.memoryUsage,
|
|
134
132
|
hostname: os.hostname()
|
|
135
133
|
});
|
|
136
|
-
|
|
137
134
|
return ev;
|
|
138
135
|
}
|
|
139
136
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/log",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0-rc.0",
|
|
4
4
|
"description": "Logging framework that integrates at the console.log level.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
"directory": "module/log"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@travetto/config": "^
|
|
27
|
-
"@travetto/di": "^
|
|
28
|
-
"@travetto/terminal": "^
|
|
26
|
+
"@travetto/config": "^7.0.0-rc.0",
|
|
27
|
+
"@travetto/di": "^7.0.0-rc.0",
|
|
28
|
+
"@travetto/terminal": "^7.0.0-rc.0"
|
|
29
29
|
},
|
|
30
30
|
"travetto": {
|
|
31
31
|
"displayName": "Logging"
|
package/src/common.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Env } from '@travetto/runtime';
|
|
1
|
+
import { Class, Env } from '@travetto/runtime';
|
|
2
2
|
import { Config, EnvVar } from '@travetto/config';
|
|
3
|
-
import {
|
|
3
|
+
import { DependencyRegistryIndex, Inject, Injectable } from '@travetto/di';
|
|
4
|
+
import { Required } from '@travetto/schema';
|
|
4
5
|
|
|
5
6
|
import { ConsoleLogAppender } from './appender/console.ts';
|
|
6
7
|
import { FileLogAppender } from './appender/file.ts';
|
|
@@ -23,15 +24,19 @@ export class CommonLogger implements Logger {
|
|
|
23
24
|
@Inject()
|
|
24
25
|
config: CommonLoggerConfig;
|
|
25
26
|
|
|
26
|
-
@Inject(LogCommonSymbol
|
|
27
|
+
@Inject(LogCommonSymbol)
|
|
28
|
+
@Required(false)
|
|
27
29
|
formatter: LogFormatter;
|
|
28
30
|
|
|
29
|
-
@Inject(LogCommonSymbol
|
|
31
|
+
@Inject(LogCommonSymbol)
|
|
32
|
+
@Required(false)
|
|
30
33
|
appender: LogAppender;
|
|
31
34
|
|
|
32
35
|
async postConstruct(): Promise<void> {
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
const formatterCls: Class = this.config.format === 'line' ? LineLogFormatter : JsonLogFormatter;
|
|
37
|
+
const appenderCls: Class = this.config.output !== 'console' ? FileLogAppender : ConsoleLogAppender;
|
|
38
|
+
this.formatter ??= await DependencyRegistryIndex.getInstance(formatterCls);
|
|
39
|
+
this.appender ??= await DependencyRegistryIndex.getInstance(appenderCls);
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
log(ev: LogEvent): void {
|
package/src/formatter/util.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { inspect, type InspectOptions } from 'node:util';
|
|
2
|
+
|
|
2
3
|
import { DataUtil } from '@travetto/schema';
|
|
4
|
+
import { safeAssign } from '@travetto/runtime';
|
|
5
|
+
|
|
3
6
|
import { LogEvent } from '../types.ts';
|
|
4
7
|
|
|
5
8
|
const INSPECT_OPTIONS = { colors: false, showHidden: false, depth: 5, breakLength: 200 };
|
|
@@ -10,7 +13,7 @@ export class LogFormatUtil {
|
|
|
10
13
|
const out: Record<string, unknown> = {};
|
|
11
14
|
for (const o of ev.args ?? []) {
|
|
12
15
|
if (DataUtil.isPlainObject(o)) {
|
|
13
|
-
|
|
16
|
+
safeAssign(out, o);
|
|
14
17
|
}
|
|
15
18
|
}
|
|
16
19
|
return out && Object.keys(out).length > 0 ? out : undefined;
|
package/src/service.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConsoleListener, ConsoleManager, ConsoleEvent, toConcrete } from '@travetto/runtime';
|
|
2
|
-
import {
|
|
2
|
+
import { DependencyRegistryIndex, Injectable } from '@travetto/di';
|
|
3
3
|
|
|
4
4
|
import { LogDecorator, LogEvent, Logger } from './types.ts';
|
|
5
5
|
import { CommonLogger } from './common.ts';
|
|
@@ -7,7 +7,7 @@ import { CommonLogger } from './common.ts';
|
|
|
7
7
|
/**
|
|
8
8
|
* Logger service
|
|
9
9
|
*/
|
|
10
|
-
@Injectable({
|
|
10
|
+
@Injectable({ autoInject: true })
|
|
11
11
|
export class LogService implements ConsoleListener {
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -21,12 +21,12 @@ export class LogService implements ConsoleListener {
|
|
|
21
21
|
#decorators: LogDecorator[] = [];
|
|
22
22
|
|
|
23
23
|
async postConstruct(): Promise<void> {
|
|
24
|
-
this.#listeners = await
|
|
24
|
+
this.#listeners = await DependencyRegistryIndex.getInstances(toConcrete<Logger>(), c => c.class !== CommonLogger);
|
|
25
25
|
if (!this.#listeners.length) {
|
|
26
|
-
this.#listeners = [await
|
|
26
|
+
this.#listeners = [await DependencyRegistryIndex.getInstance(CommonLogger)];
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
this.#decorators = await
|
|
29
|
+
this.#decorators = await DependencyRegistryIndex.getInstances(toConcrete<LogDecorator>());
|
|
30
30
|
|
|
31
31
|
ConsoleManager.set(this);
|
|
32
32
|
}
|