|
1
|
export const metadata = { |
|
2
|
title: `${pageNumber} Override Logger`, |
|
3
|
} |
|
4
|
|
|
5
|
# {metadata.title} |
|
6
|
|
|
7
|
In this guide, you'll learn how to override the default [Logger](../page.mdx) in Medusa with your custom implementation. |
|
8
|
|
|
9
|
## Why Override the Logger? |
|
10
|
|
|
11
|
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. |
|
12
|
|
|
13
|
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. |
|
14
|
|
|
15
|
After overriding the `Logger`, Medusa will use your custom logger whenever it needs to log a message. |
|
16
|
|
|
17
|
--- |
|
18
|
|
|
19
|
## How to Override the Logger? |
|
20
|
|
|
21
|
To override the default `Logger`: |
|
22
|
|
|
23
|
1. Create a class that extends the `Logger` interface from `@medusajs/framework/types`. |
|
24
|
2. Pass the class in the Medusa configurations. |
|
25
|
|
|
26
|
### Step 1: Create a Custom Logger Class |
|
27
|
|
|
28
|
To create a custom logger class, create a new file at `src/utils/custom-logger.ts` with the following content: |
|
29
|
|
|
30
|
```ts title="src/utils/custom-logger.ts" |
|
31
|
import { Logger } from "@medusajs/framework/types" |
|
32
|
|
|
33
|
class MyLogger implements Logger { |
|
34
|
panic(data: unknown): void { |
|
35
|
console.error("PANIC:", data) |
|
36
|
} |
|
37
|
shouldLog(level: string): boolean { |
|
38
|
// For demonstration, always log |
|
39
|
return true |
|
40
|
} |
|
41
|
setLogLevel(level: string): void { |
|
42
|
console.info("Set log level to:", level) |
|
43
|
} |
|
44
|
unsetLogLevel(): void { |
|
45
|
console.info("Unset log level") |
|
46
|
} |
|
47
|
activity(message: string, config?: unknown): string { |
|
48
|
console.log("ACTIVITY:", message, config) |
|
49
|
return "activity-id" |
|
50
|
} |
|
51
|
progress(activityId: string, message: string): void { |
|
52
|
console.log(`PROGRESS [${activityId}]:`, message) |
|
53
|
} |
|
54
|
error(messageOrError: string | Error, error?: Error): void { |
|
55
|
if (error) { |
|
56
|
console.error("ERROR:", messageOrError, error) |
|
57
|
} else { |
|
58
|
console.error("ERROR:", messageOrError) |
|
59
|
} |
|
60
|
} |
|
61
|
failure(activityId: string, message: string): unknown { |
|
62
|
console.warn(`FAILURE [${activityId}]:`, message) |
|
63
|
return null |
|
64
|
} |
|
65
|
success(activityId: string, message: string): Record<string, unknown> { |
|
66
|
console.log(`SUCCESS [${activityId}]:`, message) |
|
67
|
return { activityId, message } |
|
68
|
} |
|
69
|
silly(message: string): void { |
|
70
|
console.debug("SILLY:", message) |
|
71
|
} |
|
72
|
debug(message: string): void { |
|
73
|
console.debug("DEBUG:", message) |
|
74
|
} |
|
75
|
verbose(message: string): void { |
|
76
|
console.info("VERBOSE:", message) |
|
77
|
} |
|
78
|
http(message: string): void { |
|
79
|
console.info("HTTP:", message) |
|
80
|
} |
|
81
|
info(message: string): void { |
|
82
|
console.info("INFO:", message) |
|
83
|
} |
|
84
|
warn(message: string): void { |
|
85
|
console.warn("WARN:", message) |
|
86
|
} |
|
87
|
log(...args: unknown[]): void { |
|
88
|
console.log(...args) |
|
89
|
} |
|
90
|
} |
|
91
|
|
|
92
|
export const logger = new MyLogger() |
|
93
|
``` |
|
94
|
|
|
95
|
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. |
|
96
|
|
|
97
|
Notice that you must export an instance of your custom logger class to use it in the Medusa configurations. |
|
98
|
|
|
99
|
<Note> |
|
100
|
|
|
101
|
You can learn more about the methods in the [Logger](../page.mdx) chapter. |
|
102
|
|
|
103
|
</Note> |
|
104
|
|
|
105
|
### Step 2: Pass the Custom Logger in Medusa Configurations |
|
106
|
|
|
107
|
Next, to use the custom logger, set the `logger` configuration in your `medusa-config.ts` file: |
|
108
|
|
|
109
|
```ts title="medusa-config.ts" |
|
110
|
import { logger } from "./src/logger/my-logger" |
|
111
|
|
|
112
|
module.exports = defineConfig({ |
|
113
|
// ... |
|
114
|
logger, |
|
115
|
}) |
|
116
|
``` |
|
117
|
|
|
118
|
The `logger` configuration accepts an instance of a class that extends the `Logger` interface. |
|
119
|
|
|
120
|
### Test Custom Logger |
|
121
|
|
|
122
|
To test that your custom logger is working, start the Medusa application: |
|
123
|
|
|
124
|
```bash npm2yarn |
|
125
|
npm run dev |
|
126
|
``` |
|
127
|
|
|
128
|
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: |
|
129
|
|
|
130
|
```bash |
|
131
|
INFO: Watching filesystem to reload dev server on file change |
|
132
|
WARN: Local Event Bus installed. This is not recommended for production. |
|
133
|
``` |