@travetto/log 6.0.0-rc.1 → 6.0.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 +7 -7
- package/__index__.ts +10 -10
- package/package.json +4 -4
- package/src/appender/console.ts +1 -1
- package/src/appender/file.ts +1 -1
- package/src/common.ts +5 -5
- package/src/formatter/google.ts +2 -2
- package/src/formatter/json.ts +2 -2
- package/src/formatter/line.ts +2 -2
- package/src/formatter/util.ts +1 -1
- package/src/service.ts +8 -9
- package/src/types.ts +7 -4
- package/src/internal/types.ts +0 -4
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @travetto/log
|
|
|
13
13
|
yarn add @travetto/log
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
This module provides logging functionality, building upon [ConsoleManager](https://github.com/travetto/travetto/tree/main/module/runtime/src/console.ts) 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.
|
|
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
19
|
By default, the system ships with the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L12), 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#L12) 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`.
|
|
@@ -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#L12) can be extended by providing an implementation of either a [LogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#
|
|
32
|
+
In addition to these simple overrides, the [CommonLogger](https://github.com/travetto/travetto/tree/main/module/log/src/common.ts#L12) 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
|
|
@@ -45,10 +45,10 @@ export class SampleFormatter implements LogFormatter {
|
|
|
45
45
|
}
|
|
46
46
|
```
|
|
47
47
|
|
|
48
|
-
As you can see, implementing [LogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#
|
|
48
|
+
As you can see, implementing [LogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L36)/[LogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L28) with the appropriate symbol is all that is necessary to customize the general logging functionality.
|
|
49
49
|
|
|
50
50
|
## Creating a Logger
|
|
51
|
-
The default pattern for logging is to create a [Logger](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#
|
|
51
|
+
The default pattern for logging is to create a [Logger](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L44) which simply consumes a logging event. The method is not asynchronous as ensuring the ordering of append calls will be the responsibility of the logger. The default logger uses `console.log` and that is synchronous by default.
|
|
52
52
|
|
|
53
53
|
**Code: Logger Shape**
|
|
54
54
|
```typescript
|
|
@@ -69,7 +69,7 @@ export interface LogEvent extends ConsoleEvent {
|
|
|
69
69
|
|
|
70
70
|
**Code: Console Event**
|
|
71
71
|
```typescript
|
|
72
|
-
export
|
|
72
|
+
export interface ConsoleEvent {
|
|
73
73
|
/** Time of event */
|
|
74
74
|
timestamp: Date;
|
|
75
75
|
/** The level of the console event */
|
|
@@ -87,7 +87,7 @@ export type ConsoleEvent = {
|
|
|
87
87
|
};
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
The [LogEvent](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#
|
|
90
|
+
The [LogEvent](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L9) is an extension of the [ConsoleEvent](https://github.com/travetto/travetto/tree/main/module/runtime/src/console.ts#L9) with the addition of two fields:
|
|
91
91
|
* `message` - This is the primary argument passed to the console statement, if it happens to be a string, otherwise the field is left empty
|
|
92
92
|
* `context` - This is the final argument passed to the console statement, if it happens to be a simple object. This is useful for external loggers that allow for searching/querying by complex data
|
|
93
93
|
|
|
@@ -108,7 +108,7 @@ export class CustomLogger implements Logger {
|
|
|
108
108
|
```
|
|
109
109
|
|
|
110
110
|
## Creating a Decorator
|
|
111
|
-
In addition to being able to control the entire logging experience, there are also scenarios in which the caller may want to only add information to the log event, without affecting control of the formatting or appending. The [
|
|
111
|
+
In addition to being able to control the entire logging experience, there are also scenarios in which the caller may want to only add information to the log event, without affecting control of the formatting or appending. The [LogDecorator](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L20) is an interface that provides a contract that allows transforming the [LogEvent](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L9) data. A common scenario for this would be to add additional metadata data (e.g. server name, ip, code revision, CPU usage, memory usage, etc) into the log messages.
|
|
112
112
|
|
|
113
113
|
**Code: Log Decorator Shape**
|
|
114
114
|
```typescript
|
package/__index__.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { } from './src/trv';
|
|
2
|
-
export * from './src/service';
|
|
3
|
-
export * from './src/formatter/json';
|
|
4
|
-
export * from './src/formatter/line';
|
|
5
|
-
export * from './src/formatter/google';
|
|
6
|
-
export * from './src/formatter/util';
|
|
7
|
-
export * from './src/appender/console';
|
|
8
|
-
export * from './src/appender/file';
|
|
9
|
-
export * from './src/types';
|
|
10
|
-
export * from './src/common';
|
|
1
|
+
import type { } from './src/trv.d.ts';
|
|
2
|
+
export * from './src/service.ts';
|
|
3
|
+
export * from './src/formatter/json.ts';
|
|
4
|
+
export * from './src/formatter/line.ts';
|
|
5
|
+
export * from './src/formatter/google.ts';
|
|
6
|
+
export * from './src/formatter/util.ts';
|
|
7
|
+
export * from './src/appender/console.ts';
|
|
8
|
+
export * from './src/appender/file.ts';
|
|
9
|
+
export * from './src/types.ts';
|
|
10
|
+
export * from './src/common.ts';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/log",
|
|
3
|
-
"version": "6.0.0
|
|
3
|
+
"version": "6.0.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": "^6.0.0
|
|
27
|
-
"@travetto/di": "^6.0.0
|
|
28
|
-
"@travetto/terminal": "^6.0.0
|
|
26
|
+
"@travetto/config": "^6.0.0",
|
|
27
|
+
"@travetto/di": "^6.0.0",
|
|
28
|
+
"@travetto/terminal": "^6.0.0"
|
|
29
29
|
},
|
|
30
30
|
"travetto": {
|
|
31
31
|
"displayName": "Logging"
|
package/src/appender/console.ts
CHANGED
package/src/appender/file.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Env, Runtime } from '@travetto/runtime';
|
|
|
5
5
|
import { Injectable } from '@travetto/di';
|
|
6
6
|
import { Config, EnvVar } from '@travetto/config';
|
|
7
7
|
|
|
8
|
-
import { LogAppender, LogEvent } from '../types';
|
|
8
|
+
import { LogAppender, LogEvent } from '../types.ts';
|
|
9
9
|
|
|
10
10
|
@Config('log')
|
|
11
11
|
export class FileLogAppenderConfig {
|
package/src/common.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { Env } from '@travetto/runtime';
|
|
|
2
2
|
import { Config, EnvVar } from '@travetto/config';
|
|
3
3
|
import { DependencyRegistry, Inject, Injectable } from '@travetto/di';
|
|
4
4
|
|
|
5
|
-
import { ConsoleLogAppender } from './appender/console';
|
|
6
|
-
import { FileLogAppender } from './appender/file';
|
|
7
|
-
import { JsonLogFormatter } from './formatter/json';
|
|
8
|
-
import { LineLogFormatter } from './formatter/line';
|
|
9
|
-
import { LogAppender, LogFormatter, LogEvent, LogCommonSymbol, Logger } from './types';
|
|
5
|
+
import { ConsoleLogAppender } from './appender/console.ts';
|
|
6
|
+
import { FileLogAppender } from './appender/file.ts';
|
|
7
|
+
import { JsonLogFormatter } from './formatter/json.ts';
|
|
8
|
+
import { LineLogFormatter } from './formatter/line.ts';
|
|
9
|
+
import { LogAppender, LogFormatter, LogEvent, LogCommonSymbol, Logger } from './types.ts';
|
|
10
10
|
|
|
11
11
|
@Config('log')
|
|
12
12
|
export class CommonLoggerConfig {
|
package/src/formatter/google.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Injectable } from '@travetto/di';
|
|
2
2
|
|
|
3
|
-
import { LogFormatter, LogEvent } from '../types';
|
|
4
|
-
import { LogFormatUtil } from './util';
|
|
3
|
+
import { LogFormatter, LogEvent } from '../types.ts';
|
|
4
|
+
import { LogFormatUtil } from './util.ts';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Google Logging Formatter
|
package/src/formatter/json.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Injectable } from '@travetto/di';
|
|
2
2
|
import { Config } from '@travetto/config';
|
|
3
3
|
|
|
4
|
-
import { LogEvent, LogFormatter } from '../types';
|
|
5
|
-
import { LogFormatUtil } from './util';
|
|
4
|
+
import { LogEvent, LogFormatter } from '../types.ts';
|
|
5
|
+
import { LogFormatUtil } from './util.ts';
|
|
6
6
|
|
|
7
7
|
@Config('log')
|
|
8
8
|
export class JSONLogFormatterConfig {
|
package/src/formatter/line.ts
CHANGED
|
@@ -6,8 +6,8 @@ import { Config, EnvVar } from '@travetto/config';
|
|
|
6
6
|
import { Ignore } from '@travetto/schema';
|
|
7
7
|
import { StyleUtil } from '@travetto/terminal';
|
|
8
8
|
|
|
9
|
-
import { LogEvent, LogFormatter } from '../types';
|
|
10
|
-
import { LogFormatUtil } from './util';
|
|
9
|
+
import { LogEvent, LogFormatter } from '../types.ts';
|
|
10
|
+
import { LogFormatUtil } from './util.ts';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* Level coloring
|
package/src/formatter/util.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { inspect, type InspectOptions } from 'node:util';
|
|
2
2
|
import { DataUtil } from '@travetto/schema';
|
|
3
|
-
import { LogEvent } from '../types';
|
|
3
|
+
import { LogEvent } from '../types.ts';
|
|
4
4
|
|
|
5
5
|
export class LogFormatUtil {
|
|
6
6
|
static #inspectOptions = { colors: false, showHidden: false, depth: 5, breakLength: 200 };
|
package/src/service.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { ConsoleListener, ConsoleManager, ConsoleEvent } from '@travetto/runtime';
|
|
2
|
-
import {
|
|
1
|
+
import { ConsoleListener, ConsoleManager, ConsoleEvent, toConcrete } from '@travetto/runtime';
|
|
2
|
+
import { DependencyRegistry, Injectable } from '@travetto/di';
|
|
3
3
|
|
|
4
|
-
import { LogDecorator, LogEvent, Logger } from './types';
|
|
5
|
-
import {
|
|
6
|
-
import { CommonLogger } from './common';
|
|
4
|
+
import { LogDecorator, LogEvent, Logger } from './types.ts';
|
|
5
|
+
import { CommonLogger } from './common.ts';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Logger service
|
|
10
9
|
*/
|
|
11
|
-
@Injectable()
|
|
12
|
-
export class LogService implements ConsoleListener
|
|
10
|
+
@Injectable({ autoCreate: true })
|
|
11
|
+
export class LogService implements ConsoleListener {
|
|
13
12
|
|
|
14
13
|
/**
|
|
15
14
|
* Log listeners
|
|
@@ -22,12 +21,12 @@ export class LogService implements ConsoleListener, AutoCreate {
|
|
|
22
21
|
#decorators: LogDecorator[] = [];
|
|
23
22
|
|
|
24
23
|
async postConstruct(): Promise<void> {
|
|
25
|
-
this.#listeners = await DependencyRegistry.getCandidateInstances<Logger>(
|
|
24
|
+
this.#listeners = await DependencyRegistry.getCandidateInstances(toConcrete<Logger>(), c => c.class !== CommonLogger);
|
|
26
25
|
if (!this.#listeners.length) {
|
|
27
26
|
this.#listeners = [await DependencyRegistry.getInstance(CommonLogger)];
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
this.#decorators = await DependencyRegistry.getCandidateInstances<LogDecorator>(
|
|
29
|
+
this.#decorators = await DependencyRegistry.getCandidateInstances(toConcrete<LogDecorator>());
|
|
31
30
|
|
|
32
31
|
ConsoleManager.set(this);
|
|
33
32
|
}
|
package/src/types.ts
CHANGED
|
@@ -4,6 +4,7 @@ export const LogCommonSymbol = Symbol.for('@travetto/log:common');
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Logging event
|
|
7
|
+
* @concrete
|
|
7
8
|
*/
|
|
8
9
|
export interface LogEvent extends ConsoleEvent {
|
|
9
10
|
/**
|
|
@@ -13,7 +14,8 @@ export interface LogEvent extends ConsoleEvent {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
|
-
*
|
|
17
|
+
* Log event decorator
|
|
18
|
+
* @concrete
|
|
17
19
|
*/
|
|
18
20
|
export interface LogDecorator {
|
|
19
21
|
decorate(ev: LogEvent): LogEvent;
|
|
@@ -21,7 +23,7 @@ export interface LogDecorator {
|
|
|
21
23
|
|
|
22
24
|
/**
|
|
23
25
|
* Output appender for the logger
|
|
24
|
-
* @concrete
|
|
26
|
+
* @concrete
|
|
25
27
|
*/
|
|
26
28
|
export interface LogAppender {
|
|
27
29
|
append(ev: LogEvent, formatted: string): void;
|
|
@@ -29,14 +31,15 @@ export interface LogAppender {
|
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
33
|
* Output formatter
|
|
32
|
-
* @concrete
|
|
34
|
+
* @concrete
|
|
33
35
|
*/
|
|
34
36
|
export interface LogFormatter {
|
|
35
37
|
format(e: LogEvent): string;
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
/**
|
|
39
|
-
*
|
|
41
|
+
* Basic logging contract
|
|
42
|
+
* @concrete
|
|
40
43
|
*/
|
|
41
44
|
export interface Logger {
|
|
42
45
|
log(ev: LogEvent): unknown;
|
package/src/internal/types.ts
DELETED