Skip to content

Commit fc54db2

Browse files
francescastoccoAlex Bramhill
andauthored
feat(apim-538): add environment variable for log format (#337)
## 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>
1 parent c9e0418 commit fc54db2

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

.env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PORT=
33

44
LOG_LEVEL=debug
55
REDACT_LOGS=false
6+
SINGLE_LINE_LOG_FORMAT=true
67

78
# Swagger
89
SWAGGER_USER=

src/config/app.config.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,56 @@ describe('appConfig', () => {
129129
});
130130
});
131131

132+
describe('parsing SINGLE_LINE_LOG_FORMAT', () => {
133+
it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is true', () => {
134+
replaceEnvironmentVariables({
135+
SINGLE_LINE_LOG_FORMAT: 'true',
136+
});
137+
138+
const config = appConfig();
139+
140+
expect(config.singleLineLogFormat).toBe(true);
141+
});
142+
143+
it('sets singleLineLogFormat to false if SINGLE_LINE_LOG_FORMAT is false', () => {
144+
replaceEnvironmentVariables({
145+
SINGLE_LINE_LOG_FORMAT: 'false',
146+
});
147+
148+
const config = appConfig();
149+
150+
expect(config.singleLineLogFormat).toBe(false);
151+
});
152+
153+
it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is not specified', () => {
154+
replaceEnvironmentVariables({});
155+
156+
const config = appConfig();
157+
158+
expect(config.singleLineLogFormat).toBe(true);
159+
});
160+
161+
it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is the empty string', () => {
162+
replaceEnvironmentVariables({
163+
SINGLE_LINE_LOG_FORMAT: '',
164+
});
165+
166+
const config = appConfig();
167+
168+
expect(config.singleLineLogFormat).toBe(true);
169+
});
170+
171+
it('sets singleLineLogFormat to true if SINGLE_LINE_LOG_FORMAT is any string other than true or false', () => {
172+
replaceEnvironmentVariables({
173+
SINGLE_LINE_LOG_FORMAT: valueGenerator.string(),
174+
});
175+
176+
const config = appConfig();
177+
178+
expect(config.singleLineLogFormat).toBe(true);
179+
});
180+
});
181+
132182
const replaceEnvironmentVariables = (newEnvVariables: Record<string, string>): void => {
133183
process.env = newEnvVariables;
134184
};

src/config/app.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ export default registerAs('app', (): Record<string, any> => {
2424
apiKey: process.env.API_KEY,
2525
logLevel: process.env.LOG_LEVEL || 'info',
2626
redactLogs: process.env.REDACT_LOGS !== 'false',
27+
singleLineLogFormat: process.env.SINGLE_LINE_LOG_FORMAT !== 'false',
2728
};
2829
});

src/main.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { LoggingInterceptor } from './logging/logging-interceptor.helper';
2929
transport: {
3030
target: 'pino-pretty',
3131
options: {
32-
singleLine: true,
32+
singleLine: config.get<boolean>('app.singleLineLogLevel'),
3333
},
3434
},
3535
hooks: {

test/support/environment-variables.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const ENVIRONMENT_VARIABLES = Object.freeze({
66
NODE_ENV: 'test',
77
LOG_LEVEL: 'debug',
88
REDACT_LOGS: false,
9+
SINGLE_LINE_LOG_FORMAT: true,
910

1011
SWAGGER_USER: valueGenerator.string(),
1112
SWAGGER_PASSWORD: valueGenerator.string(),
@@ -22,6 +23,7 @@ export const ENVIRONMENT_VARIABLES = Object.freeze({
2223
export const getEnvironmentVariablesForProcessEnv = (): NodeJS.ProcessEnv => ({
2324
...ENVIRONMENT_VARIABLES,
2425
REDACT_LOGS: ENVIRONMENT_VARIABLES.REDACT_LOGS.toString(),
26+
SINGLE_LINE_LOG_FORMAT: ENVIRONMENT_VARIABLES.SINGLE_LINE_LOG_FORMAT.toString(),
2527
APIM_INFORMATICA_MAX_REDIRECTS: ENVIRONMENT_VARIABLES.APIM_INFORMATICA_MAX_REDIRECTS.toString(),
2628
APIM_INFORMATICA_TIMEOUT: ENVIRONMENT_VARIABLES.APIM_INFORMATICA_TIMEOUT.toString(),
2729
});

0 commit comments

Comments
 (0)