@travetto/log 3.4.4 → 4.0.0-rc.1

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 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/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.
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#L11), and by default will leverage the [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L36) 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#L11) provides two configuration variables that allows for switching out [LineLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/line.ts#L36) 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#L10) depending on the value of `CommonLoggerConfig.output`.
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#L36) 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#L36) for the [JsonLogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/formatter/json.ts#L15), 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#L11) can be extended by providing an implementation of either a [LogFormatter](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L38) or [LogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L30), with the declared symbol of `LogCommonⲐ`.
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#L38) or [LogAppender](https://github.com/travetto/travetto/tree/main/module/log/src/types.ts#L30), with the declared symbol of `LogCommonⲐ`.
33
33
 
34
34
  **Code: Sample Common Formatter**
35
35
  ```typescript
@@ -125,7 +125,7 @@ export interface LogDecorator {
125
125
 
126
126
  **Code: Custom Logger**
127
127
  ```typescript
128
- import os from 'os';
128
+ import os from 'node:os';
129
129
 
130
130
  import { Injectable } from '@travetto/di';
131
131
  import { LogDecorator, LogEvent } from '@travetto/log';
package/__index__.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference path="./src/trv.d.ts" />
1
2
  export * from './src/service';
2
3
  export * from './src/formatter/json';
3
4
  export * from './src/formatter/line';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/log",
3
- "version": "3.4.4",
3
+ "version": "4.0.0-rc.1",
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.4.4",
27
- "@travetto/di": "^3.4.2",
28
- "@travetto/terminal": "^3.4.0"
26
+ "@travetto/config": "^4.0.0-rc.1",
27
+ "@travetto/di": "^4.0.0-rc.1",
28
+ "@travetto/terminal": "^4.0.0-rc.1"
29
29
  },
30
30
  "travetto": {
31
31
  "displayName": "Logging"
@@ -1,21 +1,22 @@
1
- import { createWriteStream, WriteStream, mkdirSync, openSync, appendFileSync } from 'fs';
1
+ import { createWriteStream, WriteStream, mkdirSync, openSync, appendFileSync } from 'node:fs';
2
2
 
3
+ import { Env } from '@travetto/base';
3
4
  import { Injectable } from '@travetto/di';
4
5
  import { Config, EnvVar } from '@travetto/config';
5
- import { ManifestFileUtil, path, RootIndex } from '@travetto/manifest';
6
+ import { path, RuntimeContext } from '@travetto/manifest';
6
7
 
7
8
  import { LogAppender, LogEvent } from '../types';
8
9
 
9
10
  @Config('log')
10
11
  export class FileLogAppenderConfig {
11
- @EnvVar('TRV_LOG_OUTPUT')
12
+ @EnvVar(Env.TRV_LOG_OUTPUT.key)
12
13
  output?: 'file' | string;
13
14
 
14
15
  writeSync = false;
15
16
 
16
17
  postConstruct(): void {
17
18
  if (!this.output || this.output === 'file' || this.output === 'console') {
18
- this.output = ManifestFileUtil.toolPath(RootIndex, 'output.log', true);
19
+ this.output = RuntimeContext.toolPath('@', 'output.log');
19
20
  }
20
21
  }
21
22
  }
package/src/common.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Env } from '@travetto/base';
1
2
  import { Config, EnvVar } from '@travetto/config';
2
3
  import { DependencyRegistry, Inject, Injectable } from '@travetto/di';
3
4
 
@@ -9,10 +10,10 @@ import { LogAppender, LogFormatter, LogEvent, LogCommonⲐ, Logger } from './typ
9
10
 
10
11
  @Config('log')
11
12
  export class CommonLoggerConfig {
12
- @EnvVar('TRV_LOG_FORMAT')
13
+ @EnvVar(Env.TRV_LOG_FORMAT.key)
13
14
  format: 'line' | 'json' = 'line';
14
15
 
15
- @EnvVar('TRV_LOG_OUTPUT')
16
+ @EnvVar(Env.TRV_LOG_OUTPUT.key)
16
17
  output: 'console' | 'file' | string = 'console';
17
18
  }
18
19
 
@@ -1,4 +1,4 @@
1
- import util from 'util';
1
+ import util from 'node:util';
2
2
 
3
3
  import { Injectable } from '@travetto/di';
4
4
 
@@ -1,11 +1,10 @@
1
1
  import { Injectable } from '@travetto/di';
2
- import { Config, EnvVar } from '@travetto/config';
2
+ import { Config } from '@travetto/config';
3
3
 
4
4
  import { LogEvent, LogFormatter } from '../types';
5
5
 
6
6
  @Config('log')
7
7
  export class JSONLogFormatterConfig {
8
- @EnvVar('TRV_LOG_JSON_INDENT')
9
8
  jsonIndent?: number;
10
9
  }
11
10
 
@@ -1,23 +1,23 @@
1
- import util from 'util';
1
+ import util from 'node:util';
2
2
 
3
- import { GlobalTerminal } from '@travetto/terminal';
3
+ import { Env } from '@travetto/base';
4
4
  import { Injectable } from '@travetto/di';
5
5
  import { Config, EnvVar } from '@travetto/config';
6
- import { GlobalEnv } from '@travetto/base';
7
6
  import { Ignore } from '@travetto/schema';
7
+ import { StyleUtil } from '@travetto/terminal';
8
8
 
9
9
  import { LogEvent, LogFormatter } from '../types';
10
10
 
11
11
  /**
12
12
  * Level coloring
13
13
  */
14
- export const STYLES = GlobalTerminal.palette({
15
- info: ['yellow', 'goldenrod'],
16
- debug: ['lightGray', '#555555'],
17
- warn: ['darkOrange', 'brightMagenta'],
18
- error: ['darkRed', { text: 'brightCyan', inverse: true }],
19
- timestamp: ['white', 'black'],
20
- location: ['lightBlue', 'purple']
14
+ export const STYLES = StyleUtil.getPalette({
15
+ info: ['#ffff00', '#ff5733'], // Yellow / goldenrod
16
+ debug: ['#d3d3d3', '#555555'], // Light gray / dark gray
17
+ warn: ['#ff8c00', '#ff00ff'], // Dark orange / bright magenta
18
+ error: ['#8b0000', { text: '#00ffff', inverse: true }], // Dark red / bright cyan inverted
19
+ timestamp: ['#e5e5e5', '#000000'], // White /black
20
+ location: ['#add8e6', '#800080'] // Light blue / purple
21
21
  });
22
22
 
23
23
  /**
@@ -34,10 +34,10 @@ export interface LineFormatterOpts {
34
34
 
35
35
  @Config('log')
36
36
  export class LineLogFormatterConfig {
37
- @EnvVar('TRV_LOG_PLAIN')
37
+ @EnvVar(Env.TRV_LOG_PLAIN.key)
38
38
  plain?: boolean;
39
39
 
40
- @EnvVar('TRV_LOG_TIME')
40
+ @EnvVar(Env.TRV_LOG_TIME.key)
41
41
  time?: 's' | 'ms' | string;
42
42
 
43
43
  colorize?: boolean;
@@ -49,12 +49,8 @@ export class LineLogFormatterConfig {
49
49
  timestamp?: 's' | 'ms';
50
50
 
51
51
  postConstruct(): void {
52
- if (GlobalEnv.test) {
53
- this.plain = true;
54
- this.time = undefined;
55
- }
56
52
  this.time ??= (!this.plain ? 'ms' : undefined);
57
- this.plain ??= GlobalTerminal.colorLevel === 0;
53
+ this.plain ??= !StyleUtil.enabled;
58
54
  this.colorize ??= !this.plain;
59
55
  this.location ??= !this.plain;
60
56
  this.level ??= !this.plain;
@@ -62,6 +58,10 @@ export class LineLogFormatterConfig {
62
58
  if (this.time !== undefined && this.time === 'ms' || this.time === 's') {
63
59
  this.timestamp = this.time;
64
60
  }
61
+ Object.assign(util.inspect.defaultOptions, {
62
+ breakLength: Math.max(util.inspect.defaultOptions.breakLength ?? 0, 100),
63
+ depth: Math.max(util.inspect.defaultOptions.depth ?? 0, 4)
64
+ });
65
65
  }
66
66
  }
67
67
 
@@ -80,9 +80,7 @@ export class LineLogFormatter implements LogFormatter {
80
80
  pretty(ev: LogEvent, o: unknown): string {
81
81
  return util.inspect(o, {
82
82
  showHidden: ev.level === 'debug',
83
- depth: 4,
84
83
  colors: this.opts.colorize !== false,
85
- breakLength: 100
86
84
  });
87
85
  }
88
86
 
package/src/service.ts CHANGED
@@ -1,6 +1,5 @@
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';
4
3
 
5
4
  import { LogDecorator, LogEvent, Logger } from './types';
6
5
  import { LogDecoratorTarget, LoggerTarget } from './internal/types';
@@ -23,8 +22,6 @@ export class LogService implements ConsoleListener, AutoCreate {
23
22
  #decorators: LogDecorator[] = [];
24
23
 
25
24
  async postConstruct(): Promise<void> {
26
- await GlobalTerminal.init();
27
-
28
25
  this.#listeners = await DependencyRegistry.getCandidateInstances<Logger>(LoggerTarget, c => c.class !== CommonLogger);
29
26
  if (!this.#listeners.length) {
30
27
  this.#listeners = [await DependencyRegistry.getInstance(CommonLogger)];
package/src/trv.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import '@travetto/base';
2
+
3
+ declare global {
4
+ interface TravettoEnv {
5
+ /**
6
+ * Determines whether or not to augment console log information
7
+ * @default false
8
+ */
9
+ TRV_LOG_PLAIN: boolean;
10
+ /**
11
+ * Determines if we should log time when logging,
12
+ * @default ms
13
+ */
14
+ TRV_LOG_TIME: false | 'ms' | 's';
15
+ /**
16
+ * Determines desired log format
17
+ * @default text
18
+ */
19
+ TRV_LOG_FORMAT: 'json' | 'text';
20
+ /**
21
+ * Log output location
22
+ * @default console
23
+ */
24
+ TRV_LOG_OUTPUT: 'console' | 'file' | string;
25
+ }
26
+ }
package/src/types.ts CHANGED
@@ -17,7 +17,7 @@ export interface LogEvent extends ConsoleEvent {
17
17
  }
18
18
 
19
19
  /**
20
- * @concrete ./internal/types:LogDecoratorTarget
20
+ * @concrete ./internal/types#LogDecoratorTarget
21
21
  */
22
22
  export interface LogDecorator {
23
23
  decorate(ev: LogEvent): LogEvent;
@@ -25,7 +25,7 @@ export interface LogDecorator {
25
25
 
26
26
  /**
27
27
  * Output appender for the logger
28
- * @concrete ./internal/types:LogAppenderTarget
28
+ * @concrete ./internal/types#LogAppenderTarget
29
29
  */
30
30
  export interface LogAppender {
31
31
  append(ev: LogEvent, formatted: string): void;
@@ -33,14 +33,14 @@ export interface LogAppender {
33
33
 
34
34
  /**
35
35
  * Output formatter
36
- * @concrete ./internal/types:LogFormatterTarget
36
+ * @concrete ./internal/types#LogFormatterTarget
37
37
  */
38
38
  export interface LogFormatter {
39
39
  format(e: LogEvent): string;
40
40
  }
41
41
 
42
42
  /**
43
- * @concrete ./internal/types:LoggerTarget
43
+ * @concrete ./internal/types#LoggerTarget
44
44
  */
45
45
  export interface Logger {
46
46
  onLog(ev: LogEvent): unknown;