Skip to content

Commit

Permalink
feat: use appropriate verbosity levels specifically in the browser en…
Browse files Browse the repository at this point in the history
…vironment
  • Loading branch information
bartoszherba committed Oct 29, 2024
1 parent 832a074 commit ecd2387
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-worms-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-storefront/logger": minor
---

use appropriate verbosity levels in the browser environment
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { jsonReporter } from "../../../../src/reporters/consola/jsonReporter";

describe("jsonReporter", () => {
let consoleSpy: jest.SpyInstance;
let warnSpy: jest.SpyInstance;

beforeEach(() => {
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => {});

Check warning on line 8 in packages/logger/__tests__/unit/reporter/consola/jsonReporter.spec.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run CI

Unexpected empty arrow function
warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});

Check warning on line 9 in packages/logger/__tests__/unit/reporter/consola/jsonReporter.spec.ts

View workflow job for this annotation

GitHub Actions / Continuous Integration / Run CI

Unexpected empty arrow function
});

afterEach(() => {
consoleSpy.mockRestore();
});

it("should log structuredLog directly in development environment", () => {
process.env.NODE_ENV = "development";
const logObject: any = {
type: "log",
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" });
});

it("should log structuredLog directly in browser environment", () => {
delete process.env.NODE_ENV;
(global as any).window = {};

const logObject: any = {
type: "log",
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" });

delete (global as any).window;
});

it("should log structuredLog as JSON in production environment", () => {
process.env.NODE_ENV = "production";
const logObject: any = {
type: "log",
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(consoleSpy).toHaveBeenCalledWith(
JSON.stringify({ message: "test message" })
);
});

it("should use default log type if log type is not defined", () => {
const logObject: any = {
type: undefined,
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(consoleSpy).toHaveBeenCalledWith(
JSON.stringify({ message: "test message" })
);
});

it("should use default log type if console does not support the log type", () => {
const logObject: any = {
type: "unsupportedType",
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(consoleSpy).toHaveBeenCalledWith(
JSON.stringify({ message: "test message" })
);
});

it("should use warn log type if log type is warn", () => {
const logObject: any = {
type: "warn",
args: [{ structuredLog: { message: "test message" } }],
};

jsonReporter(logObject);

expect(warnSpy).toHaveBeenCalledWith(
JSON.stringify({ message: "test message" })
);
});
});
12 changes: 2 additions & 10 deletions packages/logger/src/ConsolaStructuredLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from "./interfaces/LoggerInterface";
import type { LoggerOptions } from "./interfaces/LoggerOptions";
import type { StructuredLog } from "./interfaces/StructuredLog";
import { jsonReporter } from "./reporters/consola/jsonReporter";

// We do not want to load the .env in the browser and in the edge runtime
if (typeof window === "undefined" && process.env.NEXT_RUNTIME !== "edge") {
Expand Down Expand Up @@ -39,16 +40,7 @@ const createConsolaStructuredLogger = (

const buildJsonReporter = () => {
return {
log: (logObject) => {
if (
process.env.NODE_ENV === "development" ||
typeof window !== "undefined"
) {
console.log(logObject.args[0].structuredLog);
} else {
console.log(JSON.stringify(logObject.args[0].structuredLog));
}
},
log: jsonReporter,
};
};

Expand Down
13 changes: 13 additions & 0 deletions packages/logger/src/reporters/consola/jsonReporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { LogObject } from "consola";

export const jsonReporter = (logObject: LogObject) => {
const defLogType = "log";
const logType = logObject?.type ?? defLogType;
const logFn = console[logType] ?? console[defLogType];

if (process.env.NODE_ENV === "development" || typeof window !== "undefined") {
logFn(logObject.args[0].structuredLog);
} else {
logFn(JSON.stringify(logObject.args[0].structuredLog));
}
};

0 comments on commit ecd2387

Please sign in to comment.