Skip to content

Commit

Permalink
feat: replace console with alokai logger in the middleware module (#7299
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bartoszherba authored Nov 4, 2024
1 parent 226bd0e commit 14350c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ describe("middlewareModule", () => {

it("should use the built in logger when ALOKAI_SDK_DEBUG is true", async () => {
process.env.ALOKAI_SDK_DEBUG = "true";
const logSpy = jest.spyOn(console, "log");
const logSpy = jest.spyOn(console, "debug");

const sdk = initSDK({
commerce: buildModule(middlewareModule<Endpoints>, {
Expand Down
20 changes: 12 additions & 8 deletions packages/sdk/src/modules/middlewareModule/utils/defaultLogger.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { LoggerFactory, LoggerType } from "@vue-storefront/logger";
import { Logger } from "../types";

const internalLogger = LoggerFactory.create(LoggerType.ConsolaGcp, {
level: "debug",
});

/**
* Default logger for the `middlewareModule`.
*/
export const defaultLogger: Logger = {
onRequest: ({ config, url, params }) => {
const { pathname } = new URL(url);
// eslint-disable-next-line no-console
console.log(
`${config.method} ${pathname}`,
`(${typeof window === "undefined" ? "server" : "client"} side)`,
JSON.stringify(params)
);
internalLogger.debug(`${config.method} ${pathname}`, {
context: `(${typeof window === "undefined" ? "server" : "client"} side)`,
params: JSON.stringify(params),
});
},
onResponse: ({ config, url, responseTime }) => {
const { pathname } = new URL(url);
// eslint-disable-next-line no-console
console.log(`${config.method} ${pathname} in ${responseTime.toFixed()}ms`);
internalLogger.debug(
`${config.method} ${pathname} in ${responseTime.toFixed()}ms`
);
},
};

0 comments on commit 14350c4

Please sign in to comment.