Skip to content

Commit f6d5804

Browse files
committed
fix: disable log prettification on AWS
pino-pretty doesn't seem to work at all when sending logs to AWS CloudWatch. We also never want prettified logs to be in production, so this protects us against the case where `NODE_ENV` isn't set in an AWS context.
1 parent 70c4f94 commit f6d5804

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/logger/lib/logger.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ const prettificationAvailable = (() => {
6969
/**
7070
* Whether log prettification is allowed. We never allow log
7171
* prettification if the `NODE_ENV` environment variable is
72-
* set to "production".
72+
* set to "production". We also disallow prettification if the
73+
* cloud provider is AWS, pino-pretty does not work well with
74+
* CloudWatch logs
7375
*
7476
* @type {boolean}
7577
*/
76-
const prettificationAllowed = appInfo.environment !== 'production';
78+
const prettificationAllowed =
79+
appInfo.environment !== 'production' && appInfo.cloudProvider !== 'aws';
7780

7881
/**
7982
* Class representing a logger.

packages/logger/test/unit/lib/logger.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jest.mock('pino-pretty', () => {
2828
});
2929

3030
jest.mock('@dotcom-reliability-kit/app-info', () => ({
31+
cloudProvider: null,
3132
environment: 'production'
3233
}));
3334
const appInfo = require('@dotcom-reliability-kit/app-info');
@@ -1085,6 +1086,32 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {
10851086
});
10861087
});
10871088

1089+
describe('when pino-pretty is installed and AWS is detected as a cloud provider', () => {
1090+
beforeEach(() => {
1091+
jest.mock('pino-pretty', () => 'mock pino pretty');
1092+
appInfo.cloudProvider = 'aws';
1093+
1094+
// We have to reset all modules because the checks for pino-pretty are done
1095+
// on module load for performance reasons. This resets the cache and reloads
1096+
// everything with a new environment.
1097+
jest.isolateModules(() => {
1098+
Logger = require('../../../lib/logger');
1099+
});
1100+
1101+
pino.mockClear();
1102+
logger = new Logger();
1103+
});
1104+
1105+
afterEach(() => {
1106+
jest.unmock('pino-pretty');
1107+
});
1108+
1109+
it('does not configure the created Pino logger with prettification', () => {
1110+
const pinoOptions = pino.mock.calls[0][0];
1111+
expect(pinoOptions.transport).toBeUndefined();
1112+
});
1113+
});
1114+
10881115
describe('when pino-pretty is not installed and the environment is "development"', () => {
10891116
beforeEach(() => {
10901117
appInfo.environment = 'development';

0 commit comments

Comments
 (0)