7.6.1. Override Logger

In this guide, you'll learn how to override the default Logger in Medusa with your custom implementation.

Why Override the Logger?#

Medusa's default Logger logs your application's events, errors, and general log messages into the console. However, you might want to customize this behavior for your specific use case.

For example, you might want to change the logs format, or add additional metadata to your logs. In those cases, it's useful to override the default Logger with your custom implementation.

After overriding the Logger, Medusa will use your custom logger whenever it needs to log a message.


How to Override the Logger?#

To override the default Logger:

  1. Create a class that extends the Logger interface from @medusajs/framework/types.
  2. Pass the class in the Medusa configurations.

Step 1: Create a Custom Logger Class#

To create a custom logger class, create a new file at src/utils/custom-logger.ts with the following content:

src/utils/custom-logger.ts
1import { Logger } from "@medusajs/framework/types"2
3class MyLogger implements Logger {4  panic(data: unknown): void {5    console.error("PANIC:", data)6  }7  shouldLog(level: string): boolean {8    // For demonstration, always log9    return true10  }11  setLogLevel(level: string): void {12    console.info("Set log level to:", level)13  }14  unsetLogLevel(): void {15    console.info("Unset log level")16  }17  activity(message: string, config?: unknown): string {18    console.log("ACTIVITY:", message, config)19    return "activity-id"20  }21  progress(activityId: string, message: string): void {22    console.log(`PROGRESS [${activityId}]:`, message)23  }24  error(messageOrError: string | Error, error?: Error): void {25    if (error) {26      console.error("ERROR:", messageOrError, error)27    } else {28      console.error("ERROR:", messageOrError)29    }30  }31  failure(activityId: string, message: string): unknown {32    console.warn(`FAILURE [${activityId}]:`, message)33    return null34  }35  success(activityId: string, message: string): Record<string, unknown> {36    console.log(`SUCCESS [${activityId}]:`, message)37    return { activityId, message }38  }39  silly(message: string): void {40    console.debug("SILLY:", message)41  }42  debug(message: string): void {43    console.debug("DEBUG:", message)44  }45  verbose(message: string): void {46    console.info("VERBOSE:", message)47  }48  http(message: string): void {49    console.info("HTTP:", message)50  }51  info(message: string): void {52    console.info("INFO:", message)53  }54  warn(message: string): void {55    console.warn("WARN:", message)56  }57  log(...args: unknown[]): void {58    console.log(...args)59  }60}61
62export const logger = new MyLogger()

You can implement the methods as per your requirements. For simplicity, the above example logs messages to the console with a prefix indicating the log level.

Notice that you must export an instance of your custom logger class to use it in the Medusa configurations.

Note: You can learn more about the methods in the Logger chapter.

Step 2: Pass the Custom Logger in Medusa Configurations#

Next, to use the custom logger, set the logger configuration in your medusa-config.ts file:

medusa-config.ts
1import { logger } from "./src/logger/my-logger"2
3module.exports = defineConfig({4  // ...5  logger,6})

The logger configuration accepts an instance of a class that extends the Logger interface.

Test Custom Logger#

To test that your custom logger is working, start the Medusa application:

The logs will be displayed based on your custom implementation. For example, the above implementation that logs messages to the console will show logs similar to the following:

Terminal
INFO: Watching filesystem to reload dev server on file changeWARN: Local Event Bus installed. This is not recommended for production.
Was this chapter helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break