Skip to content

Commit

Permalink
chore(logger): set default UTC timezone (#1775)
Browse files Browse the repository at this point in the history
* chore(parameters): add export types

* chore(logger): set default utc timezone

* chore(logger): pass down envvarsservice to log formatter
  • Loading branch information
dreamorosi committed Feb 20, 2024
1 parent cf229ab commit bec45c5
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
7 changes: 4 additions & 3 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,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 @@ -998,8 +1000,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 @@ -1008,7 +1010,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 @@ -30,6 +30,7 @@ class EnvironmentVariablesService
private logLevelVariableLegacy = '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_LAMBDA_LOG_LEVEL` environment variable.
Expand Down Expand Up @@ -132,6 +133,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';
}
}

export { EnvironmentVariablesService };
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
7 changes: 0 additions & 7 deletions packages/logger/src/types/ConfigServiceInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ interface ConfigServiceInterface {
*/
getServiceName(): string;

/**
* It returns the value of the POWERTOOLS_DEV environment variable.
*
* @returns {boolean}
*/
isDevMode(): boolean;

/**
* It returns true if the string value represents a boolean true value.
*
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 @@ -193,4 +193,29 @@ describe('Class: EnvironmentVariablesService', () => {
expect(value).toEqual(0.01);
});
});

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');
});
});
});
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

0 comments on commit bec45c5

Please sign in to comment.