Skip to content

feat: disable prettifier for more prod env names #1373

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 1 commit into from
Mar 27, 2025
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
2 changes: 1 addition & 1 deletion packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ You can also use [built-in transforms](#built-in-transforms) to do things like m

#### `options.withPrettifier`

Whether to send prettified logs if available. This option has no effect if you have the `NODE_ENV` environment variable set to either `production` or if you have not installed [pino-pretty](https://github.com/pinojs/pino-pretty#readme). See [local development usage](#local-development-usage) for more information.
Whether to send prettified logs if available. This option has no effect if you have the `NODE_ENV` environment variable set to `production` (`prod` or `p` also work) or if you have not installed [pino-pretty](https://github.com/pinojs/pino-pretty#readme). See [local development usage](#local-development-usage) for more information.

Must be a `Boolean` and defaults to `true`.

Expand Down
3 changes: 2 additions & 1 deletion packages/logger/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const prettificationAvailable = (() => {
* @type {boolean}
*/
const prettificationAllowed =
appInfo.environment !== 'production' && appInfo.cloudProvider !== 'aws';
!['production', 'prod', 'p'].includes(appInfo.environment.toLowerCase()) &&
appInfo.cloudProvider !== 'aws';

/**
* Class representing a logger.
Expand Down
43 changes: 30 additions & 13 deletions packages/logger/test/unit/lib/logger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,7 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {

afterEach(() => {
jest.unmock('pino-pretty');
appInfo.environment = 'production';
});

it('configures the created Pino logger with prettification', () => {
Expand Down Expand Up @@ -1025,6 +1026,7 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {

afterEach(() => {
jest.unmock('pino-pretty');
appInfo.environment = 'production';
});

it('does not configure the created Pino logger with prettification', () => {
Expand Down Expand Up @@ -1052,6 +1054,7 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {

afterEach(() => {
jest.unmock('pino-pretty');
delete process.env.LOG_DISABLE_PRETTIFIER;
});

it('does not configure the created Pino logger with prettification', () => {
Expand All @@ -1060,29 +1063,35 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {
});
});

describe('when pino-pretty is installed and the environment is "production"', () => {
describe('when pino-pretty is installed and the environment is "production", "prod", or "p"', () => {
beforeEach(() => {
jest.mock('pino-pretty', () => 'mock pino pretty');
appInfo.environment = 'production';

// We have to reset all modules because the checks for pino-pretty are done
// on module load for performance reasons. This resets the cache and reloads
// everything with a new environment.
jest.isolateModules(() => {
Logger = require('../../../lib/logger');
});

pino.mockClear();
logger = new Logger();
for (const environment of ['production', 'prod', 'p']) {
let Logger;

appInfo.environment = environment;
// We have to reset all modules because the checks for pino-pretty are done
// on module load for performance reasons. This resets the cache and reloads
// everything with a new environment.
jest.isolateModules(() => {
Logger = require('../../../lib/logger');
});

logger = new Logger();
}
});

afterEach(() => {
jest.unmock('pino-pretty');
appInfo.environment = 'production';
});

it('does not configure the created Pino logger with prettification', () => {
const pinoOptions = pino.mock.calls[0][0];
expect(pinoOptions.transport).toBeUndefined();
expect(pino).toHaveBeenCalledTimes(3);
for (const call of pino.mock.calls) {
expect(call[0].transport).toBeUndefined();
}
});
});

Expand All @@ -1104,6 +1113,7 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {

afterEach(() => {
jest.unmock('pino-pretty');
appInfo.cloudProvider = null;
});

it('does not configure the created Pino logger with prettification', () => {
Expand All @@ -1114,6 +1124,9 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {

describe('when pino-pretty is not installed and the environment is "development"', () => {
beforeEach(() => {
jest.mock('pino-pretty', () => {
throw new Error('mock');
});
appInfo.environment = 'development';

// We have to reset all modules because the checks for pino-pretty are done
Expand All @@ -1127,6 +1140,10 @@ describe('@dotcom-reliability-kit/logger/lib/logger', () => {
logger = new Logger();
});

afterEach(() => {
appInfo.environment = 'production';
});

it('configures the created Pino logger without prettification', () => {
const pinoOptions = pino.mock.calls[0][0];
expect(pinoOptions.transport).toBeUndefined();
Expand Down