Skip to content

Commit

Permalink
chore: Log only to stderr (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
onurtemizkan authored Oct 28, 2024
1 parent 7f98448 commit f7c468d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions packages/bundler-plugin-core/src/sentry/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@ export type Logger = {
debug(message: string, ...params: unknown[]): void;
};

// Logging everything to stderr not to interfere with stdout
export function createLogger(options: LoggerOptions): Logger {
return {
info(message: string, ...params: unknown[]) {
if (!options.silent) {
// eslint-disable-next-line no-console
console.log(`${options.prefix} Info: ${message}`, ...params);
console.error(`${options.prefix} Info: ${message}`, ...params);
}
},
warn(message: string, ...params: unknown[]) {
if (!options.silent) {
// eslint-disable-next-line no-console
console.log(`${options.prefix} Warning: ${message}`, ...params);
console.error(`${options.prefix} Warning: ${message}`, ...params);
}
},
error(message: string, ...params: unknown[]) {
if (!options.silent) {
// eslint-disable-next-line no-console
console.log(`${options.prefix} Error: ${message}`, ...params);
console.error(`${options.prefix} Error: ${message}`, ...params);
}
},
debug(message: string, ...params: unknown[]) {
if (!options.silent && options.debug) {
// eslint-disable-next-line no-console
console.log(`${options.prefix} Debug: ${message}`, ...params);
console.error(`${options.prefix} Debug: ${message}`, ...params);
}
},
};
Expand Down
16 changes: 8 additions & 8 deletions packages/bundler-plugin-core/test/sentry/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createLogger } from "../../src/sentry/logger";

describe("Logger", () => {
const consoleLogSpy = jest.spyOn(console, "log").mockImplementation(() => undefined);
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => undefined);

afterEach(() => {
consoleLogSpy.mockReset();
consoleErrorSpy.mockReset();
});

it.each([
Expand All @@ -17,7 +17,7 @@ describe("Logger", () => {

logger[loggerMethod]("Hey!");

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

it.each([
Expand All @@ -30,7 +30,7 @@ describe("Logger", () => {

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

expect(consoleLogSpy).toHaveBeenCalledWith(
expect(consoleErrorSpy).toHaveBeenCalledWith(
`[some-prefix] ${logLevel}: Hey!`,
"this",
"is",
Expand All @@ -46,7 +46,7 @@ describe("Logger", () => {

logger.debug("Hey!");

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

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

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

expect(consoleLogSpy).toHaveBeenCalledWith(
expect(consoleErrorSpy).toHaveBeenCalledWith(
`[some-prefix] Debug: Hey!`,
"this",
"is",
Expand All @@ -72,7 +72,7 @@ describe("Logger", () => {

logger[loggerMethod]("Hey!");

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

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

logger.debug("Hey!");

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

0 comments on commit f7c468d

Please sign in to comment.