@travetto/log 4.0.0-rc.7 → 4.0.0-rc.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/log",
3
- "version": "4.0.0-rc.7",
3
+ "version": "4.0.0-rc.8",
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": "^4.0.0-rc.7",
27
- "@travetto/di": "^4.0.0-rc.7",
28
- "@travetto/terminal": "^4.0.0-rc.7"
26
+ "@travetto/config": "^4.0.0-rc.8",
27
+ "@travetto/di": "^4.0.0-rc.8",
28
+ "@travetto/terminal": "^4.0.0-rc.8"
29
29
  },
30
30
  "travetto": {
31
31
  "displayName": "Logging"
@@ -11,16 +11,25 @@ import { LogFormatter, LogEvent } from '../types';
11
11
  */
12
12
  @Injectable()
13
13
  export class GoogleLogFormatter implements LogFormatter {
14
+ #inspectOptions = { colors: false, showHidden: false, depth: 5, breakLength: 200 };
15
+
14
16
  format({
15
17
  source: file, line, scope, level, message, timestamp, module, args,
16
18
  context: { method, path, statusCode, ...context } = {},
17
19
  }: LogEvent): string {
20
+ const final: unknown[] = [...args];
21
+ if (message) {
22
+ args.unshift(message);
23
+ }
24
+ if (Object.keys(context).length) {
25
+ args.push(context);
26
+ }
18
27
  return JSON.stringify({
19
28
  context,
20
29
  'logging.googleapis.com/sourceLocation': { file, line },
21
30
  'logging.googleapis.com/labels': { module, scope },
22
31
  severity: level,
23
- message: util.format(message, ...args, ...Object.entries(context).map(([k, v]) => util.format('%s=%s', k, v))),
32
+ message: util.formatWithOptions(this.#inspectOptions, final),
24
33
  timestamp,
25
34
  ...(method ? {
26
35
  httpRequest: {
@@ -48,6 +48,13 @@ export class LineLogFormatterConfig {
48
48
  @Ignore()
49
49
  timestamp?: 's' | 'ms';
50
50
 
51
+ @Ignore()
52
+ inspectOptions: {
53
+ breakLength: number;
54
+ depth: number;
55
+ colors: boolean;
56
+ };
57
+
51
58
  postConstruct(): void {
52
59
  this.time ??= (!this.plain ? 'ms' : undefined);
53
60
  this.plain ??= !StyleUtil.enabled;
@@ -58,10 +65,11 @@ export class LineLogFormatterConfig {
58
65
  if (this.time !== undefined && this.time === 'ms' || this.time === 's') {
59
66
  this.timestamp = this.time;
60
67
  }
61
- Object.assign(util.inspect.defaultOptions, {
68
+ this.inspectOptions = {
69
+ colors: this.colorize !== false,
62
70
  breakLength: Math.max(util.inspect.defaultOptions.breakLength ?? 0, 100),
63
- depth: Math.max(util.inspect.defaultOptions.depth ?? 0, 4)
64
- });
71
+ depth: Math.max(util.inspect.defaultOptions.depth ?? 0, 5)
72
+ };
65
73
  }
66
74
  }
67
75
 
@@ -79,8 +87,8 @@ export class LineLogFormatter implements LogFormatter {
79
87
 
80
88
  pretty(ev: LogEvent, o: unknown): string {
81
89
  return util.inspect(o, {
90
+ ...this.opts.inspectOptions,
82
91
  showHidden: ev.level === 'debug',
83
- colors: this.opts.colorize !== false,
84
92
  });
85
93
  }
86
94
 
@@ -125,14 +133,14 @@ export class LineLogFormatter implements LogFormatter {
125
133
  out.push(ev.message);
126
134
  }
127
135
 
128
- if (ev.context && Object.keys(ev.context).length) {
129
- out.push(this.pretty(ev, ev.context));
130
- }
131
-
132
136
  if (ev.args && ev.args.length) {
133
137
  out.push(...ev.args.map(a => this.pretty(ev, a)));
134
138
  }
135
139
 
140
+ if (ev.context && Object.keys(ev.context).length) {
141
+ out.push(this.pretty(ev, ev.context));
142
+ }
143
+
136
144
  return out.join(' ');
137
145
  }
138
146
  }
package/src/service.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ObjectUtil, ConsoleListener, ConsoleManager, ConsoleEvent } from '@travetto/base';
1
+ import { ConsoleListener, ConsoleManager, ConsoleEvent } from '@travetto/base';
2
2
  import { AutoCreate, DependencyRegistry, Injectable } from '@travetto/di';
3
3
 
4
4
  import { LogDecorator, LogEvent, Logger } from './types';
@@ -37,18 +37,22 @@ export class LogService implements ConsoleListener, AutoCreate {
37
37
  * Endpoint for listening, endpoint registered with ConsoleManager
38
38
  */
39
39
  onLog(ev: ConsoleEvent): void {
40
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions, prefer-const
41
- let [message, context, ...args] = ev.args as [string | undefined, Record<string, unknown>, ...unknown[]];
42
- if (!ObjectUtil.isPlainObject(context)) {
43
- if (context !== undefined) {
44
- args.unshift(context);
45
- }
46
- context = {};
40
+ const args = [...ev.args];
41
+ let context: Record<string, unknown> | undefined;
42
+ let message: string | undefined;
43
+ if (typeof args[0] === 'string') {
44
+ message = args[0];
45
+ args.shift(); // First arg is now the message
47
46
  }
48
47
 
49
- if (typeof message !== 'string' && message !== undefined) {
50
- args.unshift(message);
51
- message = undefined;
48
+ // More flexible on context
49
+ const last = args[args.length - 1];
50
+ if (last !== null && last !== undefined && typeof last === 'object') {
51
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
52
+ context = Object.fromEntries(
53
+ Object.entries(last).filter(x => typeof x[1] !== 'function')
54
+ );
55
+ args.pop();
52
56
  }
53
57
 
54
58
  // Allow for controlled order of event properties