Skip to content

Commit 09bce45

Browse files
authored
feat: use appropriate verbosity levels specifically in the browser environment (#7296)
1 parent 3a290f8 commit 09bce45

File tree

5 files changed

+152
-10
lines changed

5 files changed

+152
-10
lines changed

.changeset/smart-worms-remember.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vue-storefront/logger": minor
3+
---
4+
5+
use appropriate verbosity levels in the browser environment

packages/logger/__tests__/unit/consolaStructuredLogger.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,40 @@ describe("createConsolaStructuredLogger", () => {
3939
const debugSpy = jest.spyOn(logger, "debug").mockImplementation(() => {});
4040
expect(debugSpy).not.toHaveBeenCalled();
4141
});
42+
43+
it("calls console.warn once when logger.warning is invoked", () => {
44+
const logger = createConsolaStructuredLogger(mockStructuredLog);
45+
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
46+
47+
logger.warning(logData, metadata);
48+
49+
expect(warnSpy).toHaveBeenCalledTimes(1);
50+
51+
warnSpy.mockRestore();
52+
});
53+
54+
it("calls console.error once for each error-level logging method", () => {
55+
const logger = createConsolaStructuredLogger(mockStructuredLog);
56+
const errorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
57+
58+
logger.error(logData, metadata);
59+
logger.critical(logData, metadata);
60+
logger.alert(logData, metadata);
61+
logger.emergency(logData, metadata);
62+
63+
expect(errorSpy).toHaveBeenCalledTimes(4);
64+
65+
errorSpy.mockRestore();
66+
});
67+
68+
it("calls console.info once when info-level is invoked", () => {
69+
const logger = createConsolaStructuredLogger(mockStructuredLog);
70+
const infoSpy = jest.spyOn(console, "info").mockImplementation(() => {});
71+
72+
logger.info(logData, metadata);
73+
74+
expect(infoSpy).toHaveBeenCalledTimes(1);
75+
76+
infoSpy.mockRestore();
77+
});
4278
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { jsonReporter } from "../../../../src/reporters/consola/jsonReporter";
2+
3+
describe("jsonReporter", () => {
4+
let consoleSpy: jest.SpyInstance;
5+
let warnSpy: jest.SpyInstance;
6+
7+
beforeEach(() => {
8+
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => {});
9+
warnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
10+
});
11+
12+
afterEach(() => {
13+
consoleSpy.mockRestore();
14+
});
15+
16+
it("should log structuredLog directly in development environment", () => {
17+
process.env.NODE_ENV = "development";
18+
const logObject: any = {
19+
type: "log",
20+
args: [{ structuredLog: { message: "test message" } }],
21+
};
22+
23+
jsonReporter(logObject);
24+
25+
expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" });
26+
});
27+
28+
it("should log structuredLog directly in browser environment", () => {
29+
delete process.env.NODE_ENV;
30+
(global as any).window = {};
31+
32+
const logObject: any = {
33+
type: "log",
34+
args: [{ structuredLog: { message: "test message" } }],
35+
};
36+
37+
jsonReporter(logObject);
38+
39+
expect(consoleSpy).toHaveBeenCalledWith({ message: "test message" });
40+
41+
delete (global as any).window;
42+
});
43+
44+
it("should log structuredLog as JSON in production environment", () => {
45+
process.env.NODE_ENV = "production";
46+
const logObject: any = {
47+
type: "log",
48+
args: [{ structuredLog: { message: "test message" } }],
49+
};
50+
51+
jsonReporter(logObject);
52+
53+
expect(consoleSpy).toHaveBeenCalledWith(
54+
JSON.stringify({ message: "test message" })
55+
);
56+
});
57+
58+
it("should use default log type if log type is not defined", () => {
59+
const logObject: any = {
60+
type: undefined,
61+
args: [{ structuredLog: { message: "test message" } }],
62+
};
63+
64+
jsonReporter(logObject);
65+
66+
expect(consoleSpy).toHaveBeenCalledWith(
67+
JSON.stringify({ message: "test message" })
68+
);
69+
});
70+
71+
it("should use default log type if console does not support the log type", () => {
72+
const logObject: any = {
73+
type: "unsupportedType",
74+
args: [{ structuredLog: { message: "test message" } }],
75+
};
76+
77+
jsonReporter(logObject);
78+
79+
expect(consoleSpy).toHaveBeenCalledWith(
80+
JSON.stringify({ message: "test message" })
81+
);
82+
});
83+
84+
it("should use warn log type if log type is warn", () => {
85+
const logObject: any = {
86+
type: "warn",
87+
args: [{ structuredLog: { message: "test message" } }],
88+
};
89+
90+
jsonReporter(logObject);
91+
92+
expect(warnSpy).toHaveBeenCalledWith(
93+
JSON.stringify({ message: "test message" })
94+
);
95+
});
96+
});

packages/logger/src/ConsolaStructuredLogger.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
} from "./interfaces/LoggerInterface";
1010
import type { LoggerOptions } from "./interfaces/LoggerOptions";
1111
import type { StructuredLog } from "./interfaces/StructuredLog";
12+
import { jsonReporter } from "./reporters/consola/jsonReporter";
1213

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

4041
const buildJsonReporter = () => {
4142
return {
42-
log: (logObject) => {
43-
if (
44-
process.env.NODE_ENV === "development" ||
45-
typeof window !== "undefined"
46-
) {
47-
console.log(logObject.args[0].structuredLog);
48-
} else {
49-
console.log(JSON.stringify(logObject.args[0].structuredLog));
50-
}
51-
},
43+
log: jsonReporter,
5244
};
5345
};
5446

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { LogObject } from "consola";
2+
3+
export const jsonReporter = (logObject: LogObject) => {
4+
const defLogType = "log";
5+
const logType = logObject?.type ?? defLogType;
6+
const logFn = console[logType] ?? console[defLogType];
7+
8+
if (process.env.NODE_ENV === "development" || typeof window !== "undefined") {
9+
logFn(logObject.args[0].structuredLog);
10+
} else {
11+
logFn(JSON.stringify(logObject.args[0].structuredLog));
12+
}
13+
};

0 commit comments

Comments
 (0)