Skip to content

feat(logger): Use console methods respective to log level #652

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
Jan 13, 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
6 changes: 3 additions & 3 deletions packages/bundler-plugin-core/src/sentry/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export function createLogger(options: LoggerOptions): Logger {
info(message: string, ...params: unknown[]) {
if (!options.silent) {
// eslint-disable-next-line no-console
console.error(`${options.prefix} Info: ${message}`, ...params);
console.info(`${options.prefix} Info: ${message}`, ...params);
}
},
warn(message: string, ...params: unknown[]) {
if (!options.silent) {
// eslint-disable-next-line no-console
console.error(`${options.prefix} Warning: ${message}`, ...params);
console.warn(`${options.prefix} Warning: ${message}`, ...params);
}
},
error(message: string, ...params: unknown[]) {
Expand All @@ -35,7 +35,7 @@ export function createLogger(options: LoggerOptions): Logger {
debug(message: string, ...params: unknown[]) {
if (!options.silent && options.debug) {
// eslint-disable-next-line no-console
console.error(`${options.prefix} Debug: ${message}`, ...params);
console.debug(`${options.prefix} Debug: ${message}`, ...params);
}
},
};
Expand Down
65 changes: 39 additions & 26 deletions packages/bundler-plugin-core/test/sentry/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,60 @@ import { createLogger } from "../../src/sentry/logger";

describe("Logger", () => {
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => undefined);
const consoleInfoSpy = jest.spyOn(console, "info").mockImplementation(() => undefined);
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => undefined);
const consoleDebugSpy = jest.spyOn(console, "debug").mockImplementation(() => undefined);

afterEach(() => {
consoleErrorSpy.mockReset();
consoleInfoSpy.mockReset();
consoleWarnSpy.mockReset();
consoleDebugSpy.mockReset();
});

it.each([
["info", "Info"],
["warn", "Warning"],
["error", "Error"],
] as const)(".%s() should log correctly", (loggerMethod, logLevel) => {
["info", "Info", consoleInfoSpy],
["warn", "Warning", consoleWarnSpy],
["error", "Error", consoleErrorSpy],
] as const)(".%s() should log correctly", (loggerMethod, logLevel, consoleSpy) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });

logger[loggerMethod]("Hey!");

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

it.each([
["info", "Info"],
["warn", "Warning"],
["error", "Error"],
] as const)(".%s() should log multiple params correctly", (loggerMethod, logLevel) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });
["info", "Info", consoleInfoSpy],
["warn", "Warning", consoleWarnSpy],
["error", "Error", consoleErrorSpy],
] as const)(
".%s() should log multiple params correctly",
(loggerMethod, logLevel, consoleSpy) => {
const prefix = "[some-prefix]";
const logger = createLogger({ prefix, silent: false, debug: true });

logger[loggerMethod]("Hey!", "this", "is", "a test with", 5, "params");
logger[loggerMethod]("Hey!", "this", "is", "a test with", 5, "params");

expect(consoleErrorSpy).toHaveBeenCalledWith(
`[some-prefix] ${logLevel}: Hey!`,
"this",
"is",
"a test with",
5,
"params"
);
});
expect(consoleSpy).toHaveBeenCalledWith(
`[some-prefix] ${logLevel}: Hey!`,
"this",
"is",
"a test with",
5,
"params"
);
}
);

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

logger.debug("Hey!");

expect(consoleErrorSpy).toHaveBeenCalledWith(`[some-prefix] Debug: Hey!`);
expect(consoleDebugSpy).toHaveBeenCalledWith(`[some-prefix] Debug: Hey!`);
});

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

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

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect(consoleDebugSpy).toHaveBeenCalledWith(
`[some-prefix] Debug: Hey!`,
"this",
"is",
Expand All @@ -66,13 +75,17 @@ describe("Logger", () => {
});

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

logger[loggerMethod]("Hey!");

expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(consoleSpy).not.toHaveBeenCalled();
});
});

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

logger.debug("Hey!");

expect(consoleErrorSpy).not.toHaveBeenCalled();
expect(consoleDebugSpy).not.toHaveBeenCalled();
});
});
Loading