Skip to content

Commit

Permalink
feat(apim-538): add environment variable for log format (#337)
Browse files Browse the repository at this point in the history
## Introduction
Currently, all our log messages use the single line format which makes
them hard to read during local development but is good for the dev,
staging, and production environments.

## Resolution
- Added an environment variable that controls if we use single line or
multi-line logging format
- Use this to set the singleLine property in our logging config

## Misc
- Added tests for log level

---------

Co-authored-by: Alex Bramhill <abramhill@ukexportfinance.gov.uk>
  • Loading branch information
francescastocco and Alex Bramhill authored Jul 27, 2023
1 parent c9e0418 commit fc54db2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ PORT=

LOG_LEVEL=debug
REDACT_LOGS=false
SINGLE_LINE_LOG_FORMAT=true

# Swagger
SWAGGER_USER=
Expand Down
50 changes: 50 additions & 0 deletions src/config/app.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,56 @@ describe('appConfig', () => {
});
});

describe('parsing SINGLE_LINE_LOG_FORMAT', () => {
it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is true', () => {
replaceEnvironmentVariables({
SINGLE_LINE_LOG_FORMAT: 'true',
});

const config = appConfig();

expect(config.singleLineLogFormat).toBe(true);
});

it('sets singleLineLogFormat to false if SINGLE_LINE_LOG_FORMAT is false', () => {
replaceEnvironmentVariables({
SINGLE_LINE_LOG_FORMAT: 'false',
});

const config = appConfig();

expect(config.singleLineLogFormat).toBe(false);
});

it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is not specified', () => {
replaceEnvironmentVariables({});

const config = appConfig();

expect(config.singleLineLogFormat).toBe(true);
});

it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is the empty string', () => {
replaceEnvironmentVariables({
SINGLE_LINE_LOG_FORMAT: '',
});

const config = appConfig();

expect(config.singleLineLogFormat).toBe(true);
});

it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is any string other than true or false', () => {
replaceEnvironmentVariables({
SINGLE_LINE_LOG_FORMAT: valueGenerator.string(),
});

const config = appConfig();

expect(config.singleLineLogFormat).toBe(true);
});
});

const replaceEnvironmentVariables = (newEnvVariables: Record<string, string>): void => {
process.env = newEnvVariables;
};
Expand Down
1 change: 1 addition & 0 deletions src/config/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ export default registerAs('app', (): Record<string, any> => {
apiKey: process.env.API_KEY,
logLevel: process.env.LOG_LEVEL || 'info',
redactLogs: process.env.REDACT_LOGS !== 'false',
singleLineLogFormat: process.env.SINGLE_LINE_LOG_FORMAT !== 'false',
};
});
2 changes: 1 addition & 1 deletion src/main.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { LoggingInterceptor } from './logging/logging-interceptor.helper';
transport: {
target: 'pino-pretty',
options: {
singleLine: true,
singleLine: config.get<boolean>('app.singleLineLogLevel'),
},
},
hooks: {
Expand Down
2 changes: 2 additions & 0 deletions test/support/environment-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const ENVIRONMENT_VARIABLES = Object.freeze({
NODE_ENV: 'test',
LOG_LEVEL: 'debug',
REDACT_LOGS: false,
SINGLE_LINE_LOG_FORMAT: true,

SWAGGER_USER: valueGenerator.string(),
SWAGGER_PASSWORD: valueGenerator.string(),
Expand All @@ -22,6 +23,7 @@ export const ENVIRONMENT_VARIABLES = Object.freeze({
export const getEnvironmentVariablesForProcessEnv = (): NodeJS.ProcessEnv => ({
...ENVIRONMENT_VARIABLES,
REDACT_LOGS: ENVIRONMENT_VARIABLES.REDACT_LOGS.toString(),
SINGLE_LINE_LOG_FORMAT: ENVIRONMENT_VARIABLES.SINGLE_LINE_LOG_FORMAT.toString(),
APIM_INFORMATICA_MAX_REDIRECTS: ENVIRONMENT_VARIABLES.APIM_INFORMATICA_MAX_REDIRECTS.toString(),
APIM_INFORMATICA_TIMEOUT: ENVIRONMENT_VARIABLES.APIM_INFORMATICA_TIMEOUT.toString(),
});
Expand Down

0 comments on commit fc54db2

Please sign in to comment.