Skip to content

Commit 1547fea

Browse files
authored
feat(logger): Use console methods respective to log level (#652)
1 parent 01404ae commit 1547fea

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

packages/bundler-plugin-core/src/sentry/logger.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ export function createLogger(options: LoggerOptions): Logger {
1717
info(message: string, ...params: unknown[]) {
1818
if (!options.silent) {
1919
// eslint-disable-next-line no-console
20-
console.error(`${options.prefix} Info: ${message}`, ...params);
20+
console.info(`${options.prefix} Info: ${message}`, ...params);
2121
}
2222
},
2323
warn(message: string, ...params: unknown[]) {
2424
if (!options.silent) {
2525
// eslint-disable-next-line no-console
26-
console.error(`${options.prefix} Warning: ${message}`, ...params);
26+
console.warn(`${options.prefix} Warning: ${message}`, ...params);
2727
}
2828
},
2929
error(message: string, ...params: unknown[]) {
@@ -35,7 +35,7 @@ export function createLogger(options: LoggerOptions): Logger {
3535
debug(message: string, ...params: unknown[]) {
3636
if (!options.silent && options.debug) {
3737
// eslint-disable-next-line no-console
38-
console.error(`${options.prefix} Debug: ${message}`, ...params);
38+
console.debug(`${options.prefix} Debug: ${message}`, ...params);
3939
}
4040
},
4141
};

packages/bundler-plugin-core/test/sentry/logger.test.ts

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,60 @@ import { createLogger } from "../../src/sentry/logger";
22

33
describe("Logger", () => {
44
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => undefined);
5+
const consoleInfoSpy = jest.spyOn(console, "info").mockImplementation(() => undefined);
6+
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined);
7+
const consoleDebugSpy = jest.spyOn(console, "debug").mockImplementation(() => undefined);
58

69
afterEach(() => {
710
consoleErrorSpy.mockReset();
11+
consoleInfoSpy.mockReset();
12+
consoleWarnSpy.mockReset();
13+
consoleDebugSpy.mockReset();
814
});
915

1016
it.each([
11-
["info", "Info"],
12-
["warn", "Warning"],
13-
["error", "Error"],
14-
] as const)(".%s() should log correctly", (loggerMethod, logLevel) => {
17+
["info", "Info", consoleInfoSpy],
18+
["warn", "Warning", consoleWarnSpy],
19+
["error", "Error", consoleErrorSpy],
20+
] as const)(".%s() should log correctly", (loggerMethod, logLevel, consoleSpy) => {
1521
const prefix = "[some-prefix]";
1622
const logger = createLogger({ prefix, silent: false, debug: true });
1723

1824
logger[loggerMethod]("Hey!");
1925

20-
expect(consoleErrorSpy).toHaveBeenCalledWith(`[some-prefix] ${logLevel}: Hey!`);
26+
expect(consoleSpy).toHaveBeenCalledWith(`[some-prefix] ${logLevel}: Hey!`);
2127
});
2228

2329
it.each([
24-
["info", "Info"],
25-
["warn", "Warning"],
26-
["error", "Error"],
27-
] as const)(".%s() should log multiple params correctly", (loggerMethod, logLevel) => {
28-
const prefix = "[some-prefix]";
29-
const logger = createLogger({ prefix, silent: false, debug: true });
30+
["info", "Info", consoleInfoSpy],
31+
["warn", "Warning", consoleWarnSpy],
32+
["error", "Error", consoleErrorSpy],
33+
] as const)(
34+
".%s() should log multiple params correctly",
35+
(loggerMethod, logLevel, consoleSpy) => {
36+
const prefix = "[some-prefix]";
37+
const logger = createLogger({ prefix, silent: false, debug: true });
3038

31-
logger[loggerMethod]("Hey!", "this", "is", "a test with", 5, "params");
39+
logger[loggerMethod]("Hey!", "this", "is", "a test with", 5, "params");
3240

33-
expect(consoleErrorSpy).toHaveBeenCalledWith(
34-
`[some-prefix] ${logLevel}: Hey!`,
35-
"this",
36-
"is",
37-
"a test with",
38-
5,
39-
"params"
40-
);
41-
});
41+
expect(consoleSpy).toHaveBeenCalledWith(
42+
`[some-prefix] ${logLevel}: Hey!`,
43+
"this",
44+
"is",
45+
"a test with",
46+
5,
47+
"params"
48+
);
49+
}
50+
);
4251

4352
it(".debug() should log correctly", () => {
4453
const prefix = "[some-prefix]";
4554
const logger = createLogger({ prefix, silent: false, debug: true });
4655

4756
logger.debug("Hey!");
4857

49-
expect(consoleErrorSpy).toHaveBeenCalledWith(`[some-prefix] Debug: Hey!`);
58+
expect(consoleDebugSpy).toHaveBeenCalledWith(`[some-prefix] Debug: Hey!`);
5059
});
5160

5261
it(".debug() should log multiple params correctly", () => {
@@ -55,7 +64,7 @@ describe("Logger", () => {
5564

5665
logger.debug("Hey!", "this", "is", "a test with", 5, "params");
5766

58-
expect(consoleErrorSpy).toHaveBeenCalledWith(
67+
expect(consoleDebugSpy).toHaveBeenCalledWith(
5968
`[some-prefix] Debug: Hey!`,
6069
"this",
6170
"is",
@@ -66,13 +75,17 @@ describe("Logger", () => {
6675
});
6776

6877
describe("doesn't log when `silent` option is `true`", () => {
69-
it.each(["info", "warn", "error"] as const)(".%s()", (loggerMethod) => {
78+
it.each([
79+
["info", consoleInfoSpy],
80+
["warn", consoleWarnSpy],
81+
["error", consoleErrorSpy],
82+
] as const)(".%s()", (loggerMethod, consoleSpy) => {
7083
const prefix = "[some-prefix]";
7184
const logger = createLogger({ prefix, silent: true, debug: true });
7285

7386
logger[loggerMethod]("Hey!");
7487

75-
expect(consoleErrorSpy).not.toHaveBeenCalled();
88+
expect(consoleSpy).not.toHaveBeenCalled();
7689
});
7790
});
7891

@@ -82,6 +95,6 @@ describe("Logger", () => {
8295

8396
logger.debug("Hey!");
8497

85-
expect(consoleErrorSpy).not.toHaveBeenCalled();
98+
expect(consoleDebugSpy).not.toHaveBeenCalled();
8699
});
87100
});

0 commit comments

Comments
 (0)