Files
Shahed Nasser d510639193 docs: docs for next release (#13303)
* added draft order plugin docs

* fix vale error

* added note about draft order being optional

* add new shipping option - shipping method link

* update user guides

* generate

* fix github icon

* changes to shipping option type

* document logger

* reorder list

* fixes

* fixes
2025-08-28 18:49:07 +03:00

163 lines
4.5 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export const metadata = {
title: `${pageNumber} Logging`,
}
# {metadata.title}
In this chapter, youll learn how to use Medusas logging utility.
## Logger Class
Medusa provides a `Logger` class with advanced logging functionalities. This includes configuring logging levels or saving logs to a file.
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.
The Medusa application registers the `Logger` class in the Medusa container and each module's container as `logger`.
---
## How to Log a Message
Resolve the `logger` using the Medusa container to log a message in your resource.
For example, create the file `src/jobs/log-message.ts` with the following content:
export const highlights = [
["7", "resolve", "Resolve the `Logger` class."],
["9", "info", "Log a message of level `info`."]
]
```ts title="src/jobs/log-message.ts" highlights={highlights}
import { MedusaContainer } from "@medusajs/framework/types"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
export default async function myCustomJob(
container: MedusaContainer
) {
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
logger.info("I'm using the logger!")
}
export const config = {
name: "test-logger",
// execute every minute
schedule: "* * * * *",
}
```
This creates a scheduled job that resolves the `logger` from the Medusa container and uses it to log a message.
### Test the Scheduled Job
To test out the above scheduled job, start the Medusa application:
```bash npm2yarn
npm run dev
```
After a minute, you'll see the following message as part of the logged messages:
```text
info: I'm using the logger!
```
---
## Log Levels
The `Logger` class has the following methods:
- `info`: The message is logged with level `info`.
- `warn`: The message is logged with level `warn`.
- `error`: The message is logged with level `error`.
- `debug`: The message is logged with level `debug`.
Each of these methods accepts a string parameter to log in the terminal with the associated level.
---
## Logging Configurations
### Log Level
The available log levels, from lowest to highest levels, are:
1. `silly`
2. `debug`
3. `verbose`
4. `http` (default, meaning only HTTP requests are logged)
5. `info`
6. `warn`
7. `error`
You can change that by setting the `LOG_LEVEL` environment variable to the minimum level you want to be logged.
For example:
```bash
LOG_LEVEL=error
```
This logs `error` messages only.
<Note title="Important">
The environment variable must be set as a system environment variable and not in `.env`.
</Note>
### Save Logs in a File
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 servers root directory.
For example:
```bash
LOG_FILE=all.log
```
Your logs are now saved in the `all.log` file at the root of your Medusa application.
<Note title="Important">
The environment variable must be set as a system environment variable and not in `.env`.
</Note>
---
## Show Log with Progress
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.
For example:
```ts title="src/jobs/log-message.ts"
import { MedusaContainer } from "@medusajs/framework/types"
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
export default async function myCustomJob(
container: MedusaContainer
) {
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
const activityId = logger.activity("First log message")
logger.progress(activityId, `Second log message`)
logger.success(activityId, "Last log message")
}
```
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:
- `progress`: Log a message of level `info` that indicates progress within that same activity.
- `success`: Log a message of level `info` that indicates that the activity has succeeded. This also ends the associated activity.
- `failure`: Log a message of level `error` that indicates that the activity has failed. This also ends the associated activity.
<Note>
If you configured the `LOG_LEVEL` environment variable to a level higher than those associated with the above methods, their messages wont be logged.
</Note>