Skip to content

Commit 26cfc6a

Browse files
fix: fall back to 1d for unrecognized SF_LOG_ROTATION_PERIOD values @W-23050508@ (#1314)
1 parent 62b3d25 commit 26cfc6a

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

src/logger/logger.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as path from 'node:path';
99

1010
import { type Logger as PinoLogger, pino } from 'pino';
1111
import { Env } from '@salesforce/kit';
12-
import { ensureString, isKeyOf, isString } from '@salesforce/ts-types';
12+
import { isKeyOf, isString } from '@salesforce/ts-types';
1313
import { Global, Mode } from '../global';
1414
import { SfError } from '../sfError';
1515
import { unwrapArray } from '../util/unwrapArray';
@@ -451,7 +451,7 @@ export class Logger {
451451
}
452452

453453
/** return various streams that the logger could send data to, depending on the options and env */
454-
const getWriteStream = (level = 'warn'): pino.TransportSingleOptions => {
454+
export const getWriteStream = (level = 'warn'): pino.TransportSingleOptions => {
455455
const env = new Env();
456456
// used when debug mode, writes to stdout (colorized)
457457
if (process.env.DEBUG) {
@@ -474,14 +474,17 @@ const getWriteStream = (level = 'warn'): pino.TransportSingleOptions => {
474474
]);
475475
const logRotationPeriod = env.getString('SF_LOG_ROTATION_PERIOD') ?? '1d';
476476

477+
if (logRotationPeriod && !rotator.has(logRotationPeriod)) {
478+
process.stderr.write(
479+
`Warning: Unrecognized SF_LOG_ROTATION_PERIOD value "${logRotationPeriod}". Expected 1m, 1h, or 1d. Falling back to 1d.\n`
480+
);
481+
}
482+
477483
return {
478484
// write to a rotating file
479485
target: 'pino/file',
480486
options: {
481-
destination: path.join(
482-
Global.SF_DIR,
483-
`sf-${ensureString(rotator.get(logRotationPeriod)) ?? rotator.get('1d')}.log`
484-
),
487+
destination: path.join(Global.SF_DIR, `sf-${rotator.get(logRotationPeriod) ?? rotator.get('1d')}.log`),
485488
mkdir: true,
486489
level,
487490
},

test/unit/logger/logger.test.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import { expect, config as chaiConfig } from 'chai';
1212
import { isString } from '@salesforce/ts-types';
13-
import { Logger, LoggerLevel, computeLevel } from '../../../src/logger/logger';
13+
import { Logger, LoggerLevel, computeLevel, getWriteStream } from '../../../src/logger/logger';
1414
import { shouldThrowSync, TestContext } from '../../../src/testSetup';
1515

1616
// NOTE: These tests still use 'await' which is how it use to work and were left to make
@@ -236,4 +236,59 @@ describe('Logger', () => {
236236
expect(records[2]).to.have.property('level', LoggerLevel.INFO);
237237
});
238238
});
239+
240+
describe('getWriteStream', () => {
241+
let stderrOutput: string;
242+
let originalWrite: typeof process.stderr.write;
243+
244+
beforeEach(() => {
245+
stderrOutput = '';
246+
originalWrite = process.stderr.write;
247+
process.stderr.write = ((chunk: string) => {
248+
stderrOutput += chunk;
249+
return true;
250+
}) as typeof process.stderr.write;
251+
});
252+
253+
afterEach(() => {
254+
process.stderr.write = originalWrite;
255+
});
256+
257+
it('should not throw for unrecognized SF_LOG_ROTATION_PERIOD values and emit a warning', () => {
258+
process.env.SF_LOG_ROTATION_PERIOD = '2d';
259+
const result = getWriteStream();
260+
expect(result).to.have.property('target', 'pino/file');
261+
// falls back to 1d rotation format (YYYY-MM-DD)
262+
const expected1dSuffix = new Date().toISOString().split('T')[0];
263+
expect((result.options as { destination: string }).destination).to.include(`sf-${expected1dSuffix}.log`);
264+
expect(stderrOutput).to.include('Unrecognized SF_LOG_ROTATION_PERIOD');
265+
expect(stderrOutput).to.include('2d');
266+
expect(stderrOutput).to.include('Falling back to 1d');
267+
});
268+
269+
it('should treat empty string as unset and not warn', () => {
270+
process.env.SF_LOG_ROTATION_PERIOD = '';
271+
const result = getWriteStream();
272+
expect(result).to.have.property('target', 'pino/file');
273+
const expected1dSuffix = new Date().toISOString().split('T')[0];
274+
expect((result.options as { destination: string }).destination).to.include(`sf-${expected1dSuffix}.log`);
275+
expect(stderrOutput).to.equal('');
276+
});
277+
278+
it('should use the recognized period when valid and not warn', () => {
279+
process.env.SF_LOG_ROTATION_PERIOD = '1h';
280+
const result = getWriteStream();
281+
expect(result).to.have.property('target', 'pino/file');
282+
expect((result.options as { destination: string }).destination).to.include('.log');
283+
expect(stderrOutput).to.equal('');
284+
});
285+
286+
it('should default to 1d when SF_LOG_ROTATION_PERIOD is not set', () => {
287+
delete process.env.SF_LOG_ROTATION_PERIOD;
288+
const result = getWriteStream();
289+
expect(result).to.have.property('target', 'pino/file');
290+
expect((result.options as { destination: string }).destination).to.include('.log');
291+
expect(stderrOutput).to.equal('');
292+
});
293+
});
239294
});

0 commit comments

Comments
 (0)