@athenna/logger 5.12.0 → 5.14.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/logger",
3
- "version": "5.12.0",
3
+ "version": "5.14.0",
4
4
  "description": "The Athenna logging solution. Log in stdout, files and buckets.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -11,6 +11,7 @@ import { Formatter } from '#src/formatters/Formatter';
11
11
  export class JsonFormatter extends Formatter {
12
12
  format(message) {
13
13
  const base = {
14
+ ...(this.configs.defaults || {}),
14
15
  level: this.level(),
15
16
  time: Date.now(),
16
17
  pid: this.pid(),
@@ -9,6 +9,9 @@
9
9
  import { Formatter } from '#src/formatters/Formatter';
10
10
  export class NoneFormatter extends Formatter {
11
11
  format(message) {
12
+ if (this.configs.withoutParsers) {
13
+ return message;
14
+ }
12
15
  return this.clean(this.applyColorsByChalk(this.toString(message)));
13
16
  }
14
17
  }
@@ -16,6 +16,16 @@ export declare class Logger extends Macroable {
16
16
  * Runtime configurations to be used inside the Drivers and Formatters.
17
17
  */
18
18
  private runtimeConfigs;
19
+ /**
20
+ * Default message content that should be included
21
+ * in every structured log emitted by this instance.
22
+ */
23
+ private messageDefaults;
24
+ /**
25
+ * Store the current logger strategy so create()
26
+ * can clone the logger preserving its behavior.
27
+ */
28
+ private selection;
19
29
  constructor();
20
30
  /**
21
31
  * Create a new standalone logger instance. Very
@@ -28,6 +38,11 @@ export declare class Logger extends Macroable {
28
38
  * formatters.
29
39
  */
30
40
  config(runtimeConfigs: any): Logger;
41
+ /**
42
+ * Create a new logger instance inheriting the current
43
+ * configuration and adding default message content.
44
+ */
45
+ create(defaults?: any): Logger;
31
46
  /**
32
47
  * Change the log channel.
33
48
  */
@@ -81,4 +96,9 @@ export declare class Logger extends Macroable {
81
96
  * Call drivers to transport the log.
82
97
  */
83
98
  private log;
99
+ /**
100
+ * Attach message defaults to formatter configs so
101
+ * structured formatters can enrich the output.
102
+ */
103
+ private withDefaultFormatterConfig;
84
104
  }
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import { Config } from '@athenna/config';
10
10
  import { Driver } from '#src/drivers/Driver';
11
- import { Color, Macroable } from '@athenna/common';
11
+ import { Color, Json, Macroable } from '@athenna/common';
12
12
  import { DriverFactory } from '#src/factories/DriverFactory';
13
13
  import { VANILLA_CHANNELS } from '#src/constants/VanillaChannels';
14
14
  export class Logger extends Macroable {
@@ -22,6 +22,19 @@ export class Logger extends Macroable {
22
22
  * Runtime configurations to be used inside the Drivers and Formatters.
23
23
  */
24
24
  this.runtimeConfigs = {};
25
+ /**
26
+ * Default message content that should be included
27
+ * in every structured log emitted by this instance.
28
+ */
29
+ this.messageDefaults = {};
30
+ /**
31
+ * Store the current logger strategy so create()
32
+ * can clone the logger preserving its behavior.
33
+ */
34
+ this.selection = {
35
+ type: 'vanilla',
36
+ values: []
37
+ };
25
38
  this.channelOrVanilla(Config.get('logging.default'));
26
39
  }
27
40
  /**
@@ -42,13 +55,31 @@ export class Logger extends Macroable {
42
55
  this.runtimeConfigs = runtimeConfigs;
43
56
  return this;
44
57
  }
58
+ /**
59
+ * Create a new logger instance inheriting the current
60
+ * configuration and adding default message content.
61
+ */
62
+ create(defaults = {}) {
63
+ const logger = new Logger();
64
+ logger.runtimeConfigs = Json.copy(this.runtimeConfigs);
65
+ logger.messageDefaults = {
66
+ ...Json.copy(this.messageDefaults),
67
+ ...Json.copy(defaults || {})
68
+ };
69
+ return logger[this.selection.type](...Json.copy(this.selection.values));
70
+ }
45
71
  /**
46
72
  * Change the log channel.
47
73
  */
48
74
  channel(...channels) {
49
75
  this.drivers = [];
76
+ this.selection = {
77
+ type: 'channel',
78
+ values: [...channels]
79
+ };
80
+ const runtimeConfigs = this.withDefaultFormatterConfig(this.runtimeConfigs);
50
81
  channels.forEach(channel => {
51
- this.drivers.push(DriverFactory.fabricate(channel, this.runtimeConfigs));
82
+ this.drivers.push(DriverFactory.fabricate(channel, runtimeConfigs));
52
83
  });
53
84
  return this;
54
85
  }
@@ -59,12 +90,16 @@ export class Logger extends Macroable {
59
90
  */
60
91
  vanilla(...configs) {
61
92
  this.drivers = [];
93
+ this.selection = {
94
+ type: 'vanilla',
95
+ values: Json.copy(configs)
96
+ };
62
97
  if (!configs.length) {
63
- this.drivers.push(DriverFactory.fabricateVanilla());
98
+ this.drivers.push(DriverFactory.fabricateVanilla(this.withDefaultFormatterConfig()));
64
99
  return this;
65
100
  }
66
101
  configs.forEach(config => {
67
- this.drivers.push(DriverFactory.fabricateVanilla(config));
102
+ this.drivers.push(DriverFactory.fabricateVanilla(this.withDefaultFormatterConfig(config)));
68
103
  });
69
104
  return this;
70
105
  }
@@ -139,4 +174,23 @@ export class Logger extends Macroable {
139
174
  const promises = this.drivers.map((driver) => driver.transport(level, message));
140
175
  return Promise.all(promises);
141
176
  }
177
+ /**
178
+ * Attach message defaults to formatter configs so
179
+ * structured formatters can enrich the output.
180
+ */
181
+ withDefaultFormatterConfig(configs = {}) {
182
+ if (!Object.keys(this.messageDefaults).length) {
183
+ return configs;
184
+ }
185
+ return {
186
+ ...configs,
187
+ formatterConfig: {
188
+ ...(configs.formatterConfig || {}),
189
+ defaults: {
190
+ ...((configs.formatterConfig || {}).defaults || {}),
191
+ ...this.messageDefaults
192
+ }
193
+ }
194
+ };
195
+ }
142
196
  }