Skip to content

chore(logger): set default UTC timezone #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,9 @@ class Logger extends Utility implements LoggerInterface {
* @returns {void}
*/
private setLogFormatter(logFormatter?: LogFormatterInterface): void {
this.logFormatter = logFormatter ?? new PowertoolsLogFormatter();
this.logFormatter =
logFormatter ??
new PowertoolsLogFormatter({ envVarsService: this.getEnvVarsService() });
}

/**
Expand Down Expand Up @@ -965,8 +967,8 @@ class Logger extends Utility implements LoggerInterface {
environment,
} = options;

// order is important, EnvVarsService() is used by other methods
this.setEnvVarsService();
// order is important, it uses EnvVarsService()
this.setConsole();
this.setCustomConfigService(customConfigService);
this.setInitialLogLevel(logLevel);
Expand All @@ -975,7 +977,6 @@ class Logger extends Utility implements LoggerInterface {
this.setInitialSampleRate(sampleRateValue);
this.setLogEvent();
this.setLogIndentation();

this.addPersistentLogAttributes(persistentLogAttributes);

return this;
Expand Down
12 changes: 12 additions & 0 deletions packages/logger/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class EnvironmentVariablesService
private logLevelVariable = 'LOG_LEVEL';
private memoryLimitInMBVariable = 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE';
private sampleRateValueVariable = 'POWERTOOLS_LOGGER_SAMPLE_RATE';
private tzVariable = 'TZ';

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

/**
* It returns the value of the `TZ` environment variable or `UTC` if it is not set.
*
* @returns {string}
*/
public getTimezone(): string {
const value = this.get(this.tzVariable);

return value.length > 0 ? value : 'UTC';
}

/**
* It returns true if the POWERTOOLS_DEV environment variable is set to truthy value.
*
Expand Down
17 changes: 16 additions & 1 deletion packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { LogAttributes, LogFormatterInterface } from '../types/Log.js';
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type {
LogAttributes,
LogFormatterInterface,
LogFormatterOptions,
} from '../types/Log.js';
import type { UnformattedAttributes } from '../types/Logger.js';
import { LogItem } from './LogItem.js';

Expand All @@ -10,6 +15,16 @@ import { LogItem } from './LogItem.js';
* @implements {LogFormatterInterface}
*/
abstract class LogFormatter implements LogFormatterInterface {
/**
* EnvironmentVariablesService instance.
* If set, it allows to access environment variables.
*/
protected envVarsService?: EnvironmentVariablesService;

public constructor(options?: LogFormatterOptions) {
this.envVarsService = options?.envVarsService;
}

/**
* It formats key-value pairs of log attributes.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/logger/src/types/Log.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { LogItem } from '../formatter/LogItem.js';
import type { UnformattedAttributes } from './Logger.js';

Expand Down Expand Up @@ -125,6 +126,14 @@ interface LogItemInterface {
setAttributes(attributes: LogAttributes): void;
}

type LogFormatterOptions = {
/**
* EnvironmentVariablesService instance.
* If set, it gives the LogFormatter access to environment variables.
*/
envVarsService?: EnvironmentVariablesService;
};

/**
* @interface
*/
Expand Down Expand Up @@ -175,5 +184,6 @@ export type {
LogLevel,
PowertoolsLog,
LogItemInterface,
LogFormatterOptions,
LogFormatterInterface,
};
25 changes: 25 additions & 0 deletions packages/logger/tests/unit/EnvironmentVariablesService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ describe('Class: EnvironmentVariablesService', () => {
});
});

describe('Method: getTimezone', () => {
it('returns the value of the TZ environment variable when set', () => {
// Prepare
process.env.TZ = 'Europe/London';
const service = new EnvironmentVariablesService();

// Act
const value = service.getTimezone();

// Assess
expect(value).toEqual('Europe/London');
});

it('returns the default UTC value when no TZ is set', () => {
// Prepare
const service = new EnvironmentVariablesService();

// Act
const value = service.getTimezone();

// Assess
expect(value).toEqual('UTC');
});
});

describe('Method: isDevMode', () => {
test('it returns true if the environment variable POWERTOOLS_DEV is "true"', () => {
// Prepare
Expand Down
10 changes: 5 additions & 5 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('Class: Logger', () => {
envVarsService: expect.any(EnvironmentVariablesService),
customConfigService: undefined,
logLevel: 8,
logFormatter: {},
logFormatter: expect.any(PowertoolsLogFormatter),
})
);
});
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('Class: Logger', () => {
envVarsService: expect.any(EnvironmentVariablesService),
customConfigService: undefined,
logLevel: 8,
logFormatter: {},
logFormatter: expect.any(PowertoolsLogFormatter),
})
);
});
Expand Down Expand Up @@ -398,7 +398,7 @@ describe('Class: Logger', () => {
envVarsService: expect.any(EnvironmentVariablesService),
customConfigService: configService,
logLevel: 12,
logFormatter: {},
logFormatter: expect.any(PowertoolsLogFormatter),
})
);
});
Expand Down Expand Up @@ -440,7 +440,7 @@ describe('Class: Logger', () => {
envVarsService: expect.any(EnvironmentVariablesService),
customConfigService: undefined,
logLevel: 8,
logFormatter: {},
logFormatter: expect.any(PowertoolsLogFormatter),
})
);
});
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('Class: Logger', () => {
envVarsService: expect.any(EnvironmentVariablesService),
customConfigService: undefined,
logLevel: 8,
logFormatter: {},
logFormatter: expect.any(PowertoolsLogFormatter),
})
);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/tracer/tests/helpers/tracesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ export {
getInvocationSubsegment,
splitSegmentsByName,
invokeAllTestCases,
ParsedDocument,
ParsedSegment,
ParsedTrace,
AssertAnnotationParams,
type ParsedDocument,
type ParsedSegment,
type ParsedTrace,
type AssertAnnotationParams,
};