|
10 | 10 |
|
11 | 11 | import { expect, config as chaiConfig } from 'chai'; |
12 | 12 | 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'; |
14 | 14 | import { shouldThrowSync, TestContext } from '../../../src/testSetup'; |
15 | 15 |
|
16 | 16 | // NOTE: These tests still use 'await' which is how it use to work and were left to make |
@@ -236,4 +236,59 @@ describe('Logger', () => { |
236 | 236 | expect(records[2]).to.have.property('level', LoggerLevel.INFO); |
237 | 237 | }); |
238 | 238 | }); |
| 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 | + }); |
239 | 294 | }); |
0 commit comments