Skip to content

Commit a8dde6b

Browse files
committed
feat: pass the logRecordOrder option to LogFormatter
1 parent 3a78c9a commit a8dde6b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

packages/logger/src/Logger.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
LogItemMessage,
2525
LoggerInterface,
2626
PowertoolsLogData,
27+
UnformattedAttributes,
2728
} from './types/Logger.js';
2829

2930
/**
@@ -1080,12 +1081,19 @@ class Logger extends Utility implements LoggerInterface {
10801081
*
10811082
* @private
10821083
* @param {LogFormatterInterface} logFormatter
1084+
* @param {Array<keyof UnformattedAttributes>} logRecordOrder
10831085
* @returns {void}
10841086
*/
1085-
private setLogFormatter(logFormatter?: LogFormatterInterface): void {
1087+
private setLogFormatter(
1088+
logFormatter?: LogFormatterInterface,
1089+
logRecordOrder?: Array<keyof UnformattedAttributes>
1090+
): void {
10861091
this.logFormatter =
10871092
logFormatter ??
1088-
new PowertoolsLogFormatter({ envVarsService: this.getEnvVarsService() });
1093+
new PowertoolsLogFormatter({
1094+
envVarsService: this.getEnvVarsService(),
1095+
logRecordOrder,
1096+
});
10891097
}
10901098

10911099
/**
@@ -1119,6 +1127,7 @@ class Logger extends Utility implements LoggerInterface {
11191127
persistentKeys,
11201128
persistentLogAttributes, // deprecated in favor of persistentKeys
11211129
environment,
1130+
logRecordOrder,
11221131
} = options;
11231132

11241133
if (persistentLogAttributes && persistentKeys) {
@@ -1140,7 +1149,7 @@ class Logger extends Utility implements LoggerInterface {
11401149
this.setInitialSampleRate(sampleRateValue);
11411150

11421151
// configurations that affect how logs are printed
1143-
this.setLogFormatter(logFormatter);
1152+
this.setLogFormatter(logFormatter, logRecordOrder);
11441153
this.setConsole();
11451154
this.setLogIndentation();
11461155

packages/logger/src/formatter/LogFormatter.ts

+6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ abstract class LogFormatter implements LogFormatterInterface {
2121
*/
2222
protected envVarsService?: EnvironmentVariablesService;
2323

24+
/**
25+
* An array of keys that defines the order of the log record.
26+
*/
27+
protected logRecordOrder?: Array<keyof UnformattedAttributes>;
28+
2429
public constructor(options?: LogFormatterOptions) {
2530
this.envVarsService = options?.envVarsService;
31+
this.logRecordOrder = options?.logRecordOrder;
2632
}
2733

2834
/**

0 commit comments

Comments
 (0)