Skip to content

Commit 3a072aa

Browse files
authored
chore(logger): set default UTC timezone (#1775)
* chore(parameters): add export types * chore(logger): set default utc timezone * chore(logger): pass down envvarsservice to log formatter
1 parent 38edc36 commit 3a072aa

File tree

7 files changed

+76
-13
lines changed

7 files changed

+76
-13
lines changed

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,9 @@ class Logger extends Utility implements LoggerInterface {
930930
* @returns {void}
931931
*/
932932
private setLogFormatter(logFormatter?: LogFormatterInterface): void {
933-
this.logFormatter = logFormatter ?? new PowertoolsLogFormatter();
933+
this.logFormatter =
934+
logFormatter ??
935+
new PowertoolsLogFormatter({ envVarsService: this.getEnvVarsService() });
934936
}
935937

936938
/**
@@ -965,8 +967,8 @@ class Logger extends Utility implements LoggerInterface {
965967
environment,
966968
} = options;
967969

970+
// order is important, EnvVarsService() is used by other methods
968971
this.setEnvVarsService();
969-
// order is important, it uses EnvVarsService()
970972
this.setConsole();
971973
this.setCustomConfigService(customConfigService);
972974
this.setInitialLogLevel(logLevel);
@@ -975,7 +977,6 @@ class Logger extends Utility implements LoggerInterface {
975977
this.setInitialSampleRate(sampleRateValue);
976978
this.setLogEvent();
977979
this.setLogIndentation();
978-
979980
this.addPersistentLogAttributes(persistentLogAttributes);
980981

981982
return this;

Diff for: packages/logger/src/config/EnvironmentVariablesService.ts

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class EnvironmentVariablesService
2929
private logLevelVariable = 'LOG_LEVEL';
3030
private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE';
3131
private sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE';
32+
private tzVariable = 'TZ';
3233

3334
/**
3435
* It returns the value of the AWS_REGION environment variable.
@@ -108,6 +109,17 @@ class EnvironmentVariablesService
108109
return value && value.length > 0 ? Number(value) : undefined;
109110
}
110111

112+
/**
113+
* It returns the value of the `TZ` environment variable or `UTC` if it is not set.
114+
*
115+
* @returns {string}
116+
*/
117+
public getTimezone(): string {
118+
const value = this.get(this.tzVariable);
119+
120+
return value.length > 0 ? value : 'UTC';
121+
}
122+
111123
/**
112124
* It returns true if the POWERTOOLS_DEV environment variable is set to truthy value.
113125
*

Diff for: packages/logger/src/formatter/LogFormatter.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import type { LogAttributes, LogFormatterInterface } from '../types/Log.js';
1+
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
2+
import type {
3+
LogAttributes,
4+
LogFormatterInterface,
5+
LogFormatterOptions,
6+
} from '../types/Log.js';
27
import type { UnformattedAttributes } from '../types/Logger.js';
38
import { LogItem } from './LogItem.js';
49

@@ -10,6 +15,16 @@ import { LogItem } from './LogItem.js';
1015
* @implements {LogFormatterInterface}
1116
*/
1217
abstract class LogFormatter implements LogFormatterInterface {
18+
/**
19+
* EnvironmentVariablesService instance.
20+
* If set, it allows to access environment variables.
21+
*/
22+
protected envVarsService?: EnvironmentVariablesService;
23+
24+
public constructor(options?: LogFormatterOptions) {
25+
this.envVarsService = options?.envVarsService;
26+
}
27+
1328
/**
1429
* It formats key-value pairs of log attributes.
1530
*

Diff for: packages/logger/src/types/Log.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
12
import type { LogItem } from '../formatter/LogItem.js';
23
import type { UnformattedAttributes } from './Logger.js';
34

@@ -125,6 +126,14 @@ interface LogItemInterface {
125126
setAttributes(attributes: LogAttributes): void;
126127
}
127128

129+
type LogFormatterOptions = {
130+
/**
131+
* EnvironmentVariablesService instance.
132+
* If set, it gives the LogFormatter access to environment variables.
133+
*/
134+
envVarsService?: EnvironmentVariablesService;
135+
};
136+
128137
/**
129138
* @interface
130139
*/
@@ -175,5 +184,6 @@ export type {
175184
LogLevel,
176185
PowertoolsLog,
177186
LogItemInterface,
187+
LogFormatterOptions,
178188
LogFormatterInterface,
179189
};

Diff for: packages/logger/tests/unit/EnvironmentVariablesService.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,31 @@ describe('Class: EnvironmentVariablesService', () => {
153153
});
154154
});
155155

156+
describe('Method: getTimezone', () => {
157+
it('returns the value of the TZ environment variable when set', () => {
158+
// Prepare
159+
process.env.TZ = 'Europe/London';
160+
const service = new EnvironmentVariablesService();
161+
162+
// Act
163+
const value = service.getTimezone();
164+
165+
// Assess
166+
expect(value).toEqual('Europe/London');
167+
});
168+
169+
it('returns the default UTC value when no TZ is set', () => {
170+
// Prepare
171+
const service = new EnvironmentVariablesService();
172+
173+
// Act
174+
const value = service.getTimezone();
175+
176+
// Assess
177+
expect(value).toEqual('UTC');
178+
});
179+
});
180+
156181
describe('Method: isDevMode', () => {
157182
test('it returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
158183
// Prepare

Diff for: packages/logger/tests/unit/Logger.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('Class: Logger', () => {
222222
envVarsService: expect.any(EnvironmentVariablesService),
223223
customConfigService: undefined,
224224
logLevel: 8,
225-
logFormatter: {},
225+
logFormatter: expect.any(PowertoolsLogFormatter),
226226
})
227227
);
228228
});
@@ -344,7 +344,7 @@ describe('Class: Logger', () => {
344344
envVarsService: expect.any(EnvironmentVariablesService),
345345
customConfigService: undefined,
346346
logLevel: 8,
347-
logFormatter: {},
347+
logFormatter: expect.any(PowertoolsLogFormatter),
348348
})
349349
);
350350
});
@@ -398,7 +398,7 @@ describe('Class: Logger', () => {
398398
envVarsService: expect.any(EnvironmentVariablesService),
399399
customConfigService: configService,
400400
logLevel: 12,
401-
logFormatter: {},
401+
logFormatter: expect.any(PowertoolsLogFormatter),
402402
})
403403
);
404404
});
@@ -440,7 +440,7 @@ describe('Class: Logger', () => {
440440
envVarsService: expect.any(EnvironmentVariablesService),
441441
customConfigService: undefined,
442442
logLevel: 8,
443-
logFormatter: {},
443+
logFormatter: expect.any(PowertoolsLogFormatter),
444444
})
445445
);
446446
});
@@ -468,7 +468,7 @@ describe('Class: Logger', () => {
468468
envVarsService: expect.any(EnvironmentVariablesService),
469469
customConfigService: undefined,
470470
logLevel: 8,
471-
logFormatter: {},
471+
logFormatter: expect.any(PowertoolsLogFormatter),
472472
})
473473
);
474474
});

Diff for: packages/tracer/tests/helpers/tracesUtils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ export {
314314
getInvocationSubsegment,
315315
splitSegmentsByName,
316316
invokeAllTestCases,
317-
ParsedDocument,
318-
ParsedSegment,
319-
ParsedTrace,
320-
AssertAnnotationParams,
317+
type ParsedDocument,
318+
type ParsedSegment,
319+
type ParsedTrace,
320+
type AssertAnnotationParams,
321321
};

0 commit comments

Comments
 (0)