Raw
1 export const metadata = {
2 title: `${pageNumber} Logging`,
3 }
4
5 # {metadata.title}
6
7 In this chapter, you’ll learn how to use Medusa’s logging utility.
8
9 ## Logger Class
10
11 Medusa provides a `Logger` class with advanced logging functionalities. This includes configuring logging levels or saving logs to a file.
12
13 By default, the `Logger` class logs messages to the console. You can also override the default logger with your custom implementation, as explained in the [Override Logger](./custom-logger/page.mdx) guide.
14
15 The Medusa application registers the `Logger` class in the Medusa container and each module's container as `logger`.
16
17 ---
18
19 ## How to Log a Message
20
21 Resolve the `logger` using the Medusa container to log a message in your resource.
22
23 For example, create the file `src/jobs/log-message.ts` with the following content:
24
25 export const highlights = [
26 ["7", "resolve", "Resolve the `Logger` class."],
27 ["9", "info", "Log a message of level `info`."]
28 ]
29
30 ```ts title="src/jobs/log-message.ts" highlights={highlights}
31 import { MedusaContainer } from "@medusajs/framework/types"
32 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
33
34 export default async function myCustomJob(
35 container: MedusaContainer
36 ) {
37 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
38
39 logger.info("I'm using the logger!")
40 }
41
42 export const config = {
43 name: "test-logger",
44 // execute every minute
45 schedule: "* * * * *",
46 }
47 ```
48
49 This creates a scheduled job that resolves the `logger` from the Medusa container and uses it to log a message.
50
51 ### Test the Scheduled Job
52
53 To test out the above scheduled job, start the Medusa application:
54
55 ```bash npm2yarn
56 npm run dev
57 ```
58
59 After a minute, you'll see the following message as part of the logged messages:
60
61 ```text
62 info: I'm using the logger!
63 ```
64
65 ---
66
67 ## Log Levels
68
69 The `Logger` class has the following methods:
70
71 - `info`: The message is logged with level `info`.
72 - `warn`: The message is logged with level `warn`.
73 - `error`: The message is logged with level `error`.
74 - `debug`: The message is logged with level `debug`.
75
76 Each of these methods accepts a string parameter to log in the terminal with the associated level.
77
78 ---
79
80 ## Logging Configurations
81
82 ### Log Level
83
84 The available log levels, from lowest to highest levels, are:
85
86 1. `silly`
87 2. `debug`
88 3. `verbose`
89 4. `http` (default, meaning only HTTP requests are logged)
90 5. `info`
91 6. `warn`
92 7. `error`
93
94 You can change that by setting the `LOG_LEVEL` environment variable to the minimum level you want to be logged.
95
96 For example:
97
98 ```bash
99 LOG_LEVEL=error
100 ```
101
102 This logs `error` messages only.
103
104 <Note title="Important">
105
106 The environment variable must be set as a system environment variable and not in `.env`.
107
108 </Note>
109
110 ### Save Logs in a File
111
112 Aside from showing the logs in the terminal, you can save the logs in a file by setting the `LOG_FILE` environment variable to the path of the file relative to the Medusa server’s root directory.
113
114 For example:
115
116 ```bash
117 LOG_FILE=all.log
118 ```
119
120 Your logs are now saved in the `all.log` file at the root of your Medusa application.
121
122 <Note title="Important">
123
124 The environment variable must be set as a system environment variable and not in `.env`.
125
126 </Note>
127
128 ---
129
130 ## Show Log with Progress
131
132 The `Logger` class has an `activity` method used to log a message of level `info`. If the Medusa application is running in a development environment, a spinner starts to show the activity's progress.
133
134 For example:
135
136 ```ts title="src/jobs/log-message.ts"
137 import { MedusaContainer } from "@medusajs/framework/types"
138 import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
139
140 export default async function myCustomJob(
141 container: MedusaContainer
142 ) {
143 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
144
145 const activityId = logger.activity("First log message")
146
147 logger.progress(activityId, `Second log message`)
148
149 logger.success(activityId, "Last log message")
150 }
151 ```
152
153 The `activity` method returns the ID of the started activity. This ID can then be passed to one of the following methods of the `Logger` class:
154
155 - `progress`: Log a message of level `info` that indicates progress within that same activity.
156 - `success`: Log a message of level `info` that indicates that the activity has succeeded. This also ends the associated activity.
157 - `failure`: Log a message of level `error` that indicates that the activity has failed. This also ends the associated activity.
158
159 <Note>
160
161 If you configured the `LOG_LEVEL` environment variable to a level higher than those associated with the above methods, their messages won’t be logged.
162
163 </Note>