Skip to content

Commit b434aa5

Browse files
committed
test: add test for flushing buffers
1 parent 7e99f1f commit b434aa5

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

packages/logger/src/Logger.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -876,17 +876,20 @@ class Logger extends Utility implements LoggerInterface {
876876
extraInput: LogItemExtraInput
877877
): void {
878878
if (logLevel >= this.logLevel) {
879-
const xRayTraceId = this.envVarsService.getXrayTraceId() as string;
880-
881-
// Print all log items in the context
882-
if (this.#context[xRayTraceId]) {
883-
for (const contextItem of this.#context[xRayTraceId]) {
884-
this.printLog(...contextItem);
879+
// Only flush buffer when log level is higher than the configured log level
880+
if (logLevel > this.logLevel) {
881+
const xRayTraceId = this.envVarsService.getXrayTraceId() as string;
882+
883+
// Print all log items in the context
884+
if (this.#context[xRayTraceId]) {
885+
for (const contextItem of this.#context[xRayTraceId]) {
886+
this.printLog(...contextItem);
887+
}
888+
889+
// Clear the context after flushing
890+
// This also removes entries from other X-Ray trace IDs
891+
this.#context = {};
885892
}
886-
887-
// Clear the context after flushing
888-
// This also removes entries from other X-Ray trace IDs
889-
this.#context = {};
890893
}
891894

892895
if (this.#isInitialized) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { Logger } from '../../src/Logger.js';
3+
import { LogLevel, LogLevelThreshold } from '../../src/constants.js';
4+
import type { ConfigServiceInterface } from '../../src/types/ConfigServiceInterface.js';
5+
import type {
6+
LogFunction,
7+
LogLevel as LogLevelType,
8+
} from '../../src/types/Logger.js';
9+
10+
/**
11+
* Helper function to get the console method for a given log level, we use this
12+
* for properly mocking the console methods in the tests and account for the
13+
* fact that `critical` is not a valid console method, which we proxy to `error`,
14+
* and `trace` is internally proxied to `log`.
15+
*
16+
* @param method - The method to get the console method for
17+
*/
18+
const getConsoleMethod = (
19+
method: string
20+
): keyof Omit<LogFunction, 'critical'> | 'log' => {
21+
const lowercaseMethod = method.toLowerCase();
22+
switch (lowercaseMethod) {
23+
case 'trace':
24+
return 'log';
25+
case 'critical':
26+
return 'error';
27+
default:
28+
return lowercaseMethod as keyof Omit<LogFunction, 'critical'>;
29+
}
30+
};
31+
32+
describe('Log flush buffer', () => {
33+
const ENVIRONMENT_VARIABLES = process.env;
34+
35+
beforeEach(() => {
36+
process.env = { ...ENVIRONMENT_VARIABLES, POWERTOOLS_DEV: 'true' };
37+
vi.resetAllMocks();
38+
});
39+
40+
it("doesn't flush buffer when calling a log level lower than the configured log level", () => {
41+
// Prepare
42+
const logger = new Logger({ logLevel: 'WARN' });
43+
44+
// Act
45+
logger.debug('debug');
46+
logger.info('info');
47+
48+
// Assess
49+
expect(console.debug).not.toHaveBeenCalled();
50+
expect(console.info).not.toHaveBeenCalled();
51+
});
52+
53+
it("doesn't flush buffer when calling a log level equal to the configured log level", () => {
54+
// Prepare
55+
const logger = new Logger({ logLevel: 'WARN' });
56+
57+
// Act
58+
logger.debug('debug');
59+
logger.info('info');
60+
logger.warn('warn');
61+
62+
// Assess
63+
expect(console.debug).not.toHaveBeenCalled();
64+
expect(console.info).not.toHaveBeenCalled();
65+
expect(console.warn).toHaveBeenCalled();
66+
});
67+
68+
it('flushes buffer when calling a log level higher than the configured log level', () => {
69+
// Prepare
70+
const logger = new Logger({ logLevel: 'WARN' });
71+
72+
// Act
73+
logger.debug('debug');
74+
logger.info('info');
75+
logger.warn('warn');
76+
logger.error('error');
77+
78+
// Assess
79+
expect(console.debug).toHaveBeenCalled();
80+
expect(console.info).toHaveBeenCalled();
81+
expect(console.warn).toHaveBeenCalled();
82+
expect(console.error).toHaveBeenCalled();
83+
});
84+
});

0 commit comments

Comments
 (0)