@travetto/log 3.0.0-rc.9 → 3.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 +105 -23
- package/package.json +4 -4
- package/src/common.ts +10 -2
- package/src/service.ts +3 -0
- package/src/types.ts +1 -1
package/README.md
CHANGED
|
@@ -6,43 +6,125 @@
|
|
|
6
6
|
**Install: @travetto/log**
|
|
7
7
|
```bash
|
|
8
8
|
npm install @travetto/log
|
|
9
|
-
```
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
# or
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
yarn add @travetto/log
|
|
13
|
+
```
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
This module provides logging functionality, building upon [ConsoleManager](https://github.com/travetto/travetto/tree/main/module/base/src/console.ts) in the [Base](https://github.com/travetto/travetto/tree/main/module/base#readme "Environment config and common utilities 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
|
+
|
|
17
|
+
## Creating a Logger
|
|
18
|
+
The default pattern for logging is to create a [Logger](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L33) 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.
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
**Code: Logger Shape**
|
|
21
|
+
```typescript
|
|
22
|
+
export interface Logger {
|
|
23
|
+
onLog(ev: LogEvent): unknown;
|
|
24
|
+
}
|
|
25
|
+
```
|
|
19
26
|
|
|
20
|
-
|
|
27
|
+
**Code: Log Event**
|
|
28
|
+
```typescript
|
|
29
|
+
export interface LogEvent extends ConsoleEvent {
|
|
30
|
+
/**
|
|
31
|
+
* Log message
|
|
32
|
+
*/
|
|
33
|
+
message?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Log Message context
|
|
36
|
+
*/
|
|
37
|
+
context?: Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
```
|
|
21
40
|
|
|
22
|
-
**Code:
|
|
41
|
+
**Code: Console Event**
|
|
23
42
|
```typescript
|
|
24
|
-
export
|
|
25
|
-
|
|
43
|
+
export type ConsoleEvent = {
|
|
44
|
+
/** Time of event */
|
|
45
|
+
timestamp: Date;
|
|
46
|
+
/** The level of the console event */
|
|
47
|
+
level: LogLevel;
|
|
48
|
+
/** The source file of the event */
|
|
49
|
+
source: string;
|
|
50
|
+
/** The line number the console event was triggered from */
|
|
51
|
+
line: number;
|
|
52
|
+
/** The module name for the source file */
|
|
53
|
+
module: string;
|
|
54
|
+
/** The module path for the source file*/
|
|
55
|
+
modulePath: string;
|
|
56
|
+
/** The computed scope for the console. statement. */
|
|
57
|
+
scope?: string;
|
|
58
|
+
/** Arguments passed to the console call*/
|
|
59
|
+
args: unknown[];
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The [LogEvent](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L5) is an extension of the [ConsoleEvent](https://github.com/travetto/travetto/tree/main/module/base/src/types.ts#L12) with the addition of two fields:
|
|
26
64
|
|
|
27
|
-
|
|
65
|
+
|
|
66
|
+
* `message` - This is the primary argument passed to the console statement, if it happens to be a string, otherwise the field is left empty
|
|
67
|
+
* `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
|
|
28
68
|
|
|
29
|
-
|
|
69
|
+
**Code: Custom Logger**
|
|
70
|
+
```typescript
|
|
71
|
+
import fetch, { Headers } from 'node-fetch';
|
|
72
|
+
|
|
73
|
+
import { Injectable } from '@travetto/di';
|
|
74
|
+
import { LogEvent, Logger } from '@travetto/log';
|
|
30
75
|
|
|
31
|
-
|
|
76
|
+
@Injectable()
|
|
77
|
+
export class CustomLogger implements Logger {
|
|
78
|
+
onLog(ev: LogEvent): void {
|
|
79
|
+
const headers = new Headers();
|
|
80
|
+
headers.set('Content-Type', 'application/json');
|
|
81
|
+
const body = JSON.stringify(ev);
|
|
82
|
+
fetch('http://localhost:8080/log', { method: 'POST', headers, body, });
|
|
83
|
+
}
|
|
32
84
|
}
|
|
33
85
|
```
|
|
34
86
|
|
|
35
|
-
|
|
87
|
+
## Logging to External Systems
|
|
88
|
+
By default the logging functionality logs messages directly to the console, relying on the `util.inspect` method, as is the standard behavior. When building distributed systems, with multiple separate logs, it is useful to rely on structured logging for common consumption. The framework supports logging as [JSON](https://www.json.org), which is easily consumable by services like [elasticsearch](https://elastic.co) or [AWS Cloudwatch](https://aws.amazon.com/cloudwatch/) if running as a lambda or in a docker container.
|
|
36
89
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
90
|
+
The main caveat that comes with this, is that not all objects can be converted to JSON (specifically circular dependencies, and unsupported types). That end, the framework recommends logging with the following format, `message: string` `context: Record<string, Primitive>`. Here context can be recursive, but the general idea is to only pass in known data structures that will not break the [JSON](https://www.json.org) production.
|
|
91
|
+
|
|
92
|
+
## Environment Configuration
|
|
40
93
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
**Code: Standard Logging Config**
|
|
95
|
+
```typescript
|
|
96
|
+
export class CommonLoggerConfig {
|
|
97
|
+
/** Should we enrich the console by default */
|
|
98
|
+
@EnvVar('TRV_LOG_FORMAT')
|
|
99
|
+
format: 'line' | 'json' = 'line';
|
|
100
|
+
|
|
101
|
+
/** Log file, if needed */
|
|
102
|
+
@EnvVar('TRV_LOG_FILE')
|
|
103
|
+
file?: string;
|
|
104
|
+
|
|
105
|
+
@EnvVar('TRV_LOG_PLAIN')
|
|
106
|
+
plain?: boolean;
|
|
107
|
+
|
|
108
|
+
@EnvVar('TRV_LOG_TIME')
|
|
109
|
+
time: 's' | 'ms' | string = 'ms';
|
|
110
|
+
|
|
111
|
+
@Ignore()
|
|
112
|
+
get timestamp(): 's' | 'ms' | false {
|
|
113
|
+
return (this.time ?? 'ms') === 'ms' ? 'ms' : (this.time === 's' ? 's' : false);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
postConstruct(): void {
|
|
117
|
+
if (GlobalEnv.test) {
|
|
118
|
+
this.time = '';
|
|
119
|
+
}
|
|
120
|
+
this.plain ??= GlobalTerminal.colorLevel === 0;
|
|
121
|
+
}
|
|
44
122
|
}
|
|
45
|
-
2029-03-14T04:00:01.510Z info [@travetto/log:doc/output.ts:6] Woah!
|
|
46
|
-
2029-03-14T04:00:02.450Z debug [@travetto/log:doc/output.ts:8] Test
|
|
47
|
-
[s[r[u
|
|
48
123
|
```
|
|
124
|
+
|
|
125
|
+
The following environment variables have control over the default logging config:
|
|
126
|
+
|
|
127
|
+
* `TRV_LOG_FORMAT` - This determines whether or not the output is standard text lines, or is it output as a single line of [JSON](https://www.json.org)
|
|
128
|
+
* `TRV_LOG_FILE` - This determines whether or not the logging goes to the console or if it is written to a file
|
|
129
|
+
* `TRV_LOG_PLAIN` - Allows for an override of whether or not to log colored output, this defaults to values provided by the [Terminal](https://github.com/travetto/travetto/tree/main/module/terminal#readme "General terminal support") in response to `FORCE_COLOR` and `NO_COLOR`
|
|
130
|
+
* `TRV_LOG_TIME` - This represents what level of time logging is desired, the default is `ms` which is millisecond output. A value of `s` allows for second level logging, and `false` will disable the output. When ingesting the content into another logging, its generally desirable to suppress the initial time output as most other loggers will append as needed.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/log",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.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": "^3.0.0
|
|
27
|
-
"@travetto/di": "^3.0.0
|
|
28
|
-
"@travetto/terminal": "^3.0.0
|
|
26
|
+
"@travetto/config": "^3.0.0",
|
|
27
|
+
"@travetto/di": "^3.0.0",
|
|
28
|
+
"@travetto/terminal": "^3.0.0"
|
|
29
29
|
},
|
|
30
30
|
"travetto": {
|
|
31
31
|
"displayName": "Logging"
|
package/src/common.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { GlobalEnv } from '@travetto/base';
|
|
2
2
|
import { Config, EnvVar } from '@travetto/config';
|
|
3
3
|
import { Inject, Injectable } from '@travetto/di';
|
|
4
|
+
import { Ignore } from '@travetto/schema';
|
|
5
|
+
import { GlobalTerminal } from '@travetto/terminal';
|
|
4
6
|
|
|
5
7
|
import { ConsoleAppender } from './appender/console';
|
|
6
8
|
import { FileAppender } from './appender/file';
|
|
@@ -22,12 +24,18 @@ export class CommonLoggerConfig {
|
|
|
22
24
|
plain?: boolean;
|
|
23
25
|
|
|
24
26
|
@EnvVar('TRV_LOG_TIME')
|
|
25
|
-
|
|
27
|
+
time: 's' | 'ms' | string = 'ms';
|
|
28
|
+
|
|
29
|
+
@Ignore()
|
|
30
|
+
get timestamp(): 's' | 'ms' | false {
|
|
31
|
+
return (this.time ?? 'ms') === 'ms' ? 'ms' : (this.time === 's' ? 's' : false);
|
|
32
|
+
}
|
|
26
33
|
|
|
27
34
|
postConstruct(): void {
|
|
28
35
|
if (GlobalEnv.test) {
|
|
29
|
-
this.
|
|
36
|
+
this.time = '';
|
|
30
37
|
}
|
|
38
|
+
this.plain ??= GlobalTerminal.colorLevel === 0;
|
|
31
39
|
}
|
|
32
40
|
}
|
|
33
41
|
|
package/src/service.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ObjectUtil, ConsoleListener, ConsoleManager, ConsoleEvent } from '@travetto/base';
|
|
2
2
|
import { AutoCreate, DependencyRegistry, Injectable } from '@travetto/di';
|
|
3
|
+
import { GlobalTerminal } from '@travetto/terminal';
|
|
3
4
|
|
|
4
5
|
import { LogEvent, Logger } from './types';
|
|
5
6
|
import { LoggerTarget } from './internal/types';
|
|
@@ -17,6 +18,8 @@ export class LogService implements ConsoleListener, AutoCreate {
|
|
|
17
18
|
#listeners: Logger[] = [];
|
|
18
19
|
|
|
19
20
|
async postConstruct(): Promise<void> {
|
|
21
|
+
await GlobalTerminal.init();
|
|
22
|
+
|
|
20
23
|
const def = await DependencyRegistry.getInstance(CommonLogger);
|
|
21
24
|
this.#listeners.push(def);
|
|
22
25
|
|
package/src/types.ts
CHANGED