Skip to content

Commit 7d30f4f

Browse files
committed
refactor(review): add private class property for indent size, move evaluation of this property to setOptions()
1 parent 19e8dac commit 7d30f4f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Diff for: packages/logger/src/Logger.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ class Logger extends Utility implements ClassThatLogs {
125125

126126
private logFormatter?: LogFormatterInterface;
127127

128+
private logIndentation: number = LogJsonIndent.COMPACT;
129+
128130
private logLevel?: LogLevel;
129131

130132
private readonly logLevelThresholds: LogLevelThresholds = {
@@ -570,10 +572,7 @@ class Logger extends Utility implements ClassThatLogs {
570572

571573
const consoleMethod = logLevel.toLowerCase() as keyof ClassThatLogs;
572574

573-
const isDevMode = this.getEnvVarsService().getDevMode();
574-
const INDENTATION: LogJsonIndent = isDevMode ? LogJsonIndent.PRETTY : LogJsonIndent.COMPACT;
575-
576-
this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies(), INDENTATION));
575+
this.console[consoleMethod](JSON.stringify(log.getAttributes(), this.removeCircularDependencies(), this.logIndentation));
577576
}
578577

579578
/**
@@ -666,6 +665,19 @@ class Logger extends Utility implements ClassThatLogs {
666665
this.logFormatter = logFormatter || new PowertoolLogFormatter();
667666
}
668667

668+
/**
669+
* If the `POWERTOOLS_DEV' env variable is set,
670+
* it adds JSON indentation for pretty printing logs.
671+
*
672+
* @private
673+
* @returns {void}
674+
*/
675+
private setLogIndentation(): void {
676+
if (this.getEnvVarsService().getDevMode()) {
677+
this.logIndentation = LogJsonIndent.PRETTY;
678+
}
679+
}
680+
669681
/**
670682
* It sets the Logger's instance log level.
671683
*
@@ -735,6 +747,7 @@ class Logger extends Utility implements ClassThatLogs {
735747
this.setLogFormatter(logFormatter);
736748
this.setPowertoolLogData(serviceName, environment);
737749
this.setLogEvent();
750+
this.setLogIndentation();
738751

739752
this.addPersistentLogAttributes(persistentLogAttributes);
740753

0 commit comments

Comments
 (0)