---
schema_version: "1"
id: 58b917cf54cdd1a931f1e5520b0a13007b281510be9983734c874fd8e4a3bf91
title: Add Logging
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforcedx/sfcliplugin/guides/code/logging.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-cli-plugin/guide/logging.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 0a32b5fa73d9f38036b390d101dae7e233714c5cca95cbcf2b639a8ea8f21ece
products:
  - Salesforce CLI Plugin Developer Guide
  - salesforcedx
---
# Add Logging

Add logging to your plugin with the [`Logger`](https://forcedotcom.github.io/sfdx-core/classes/logger_logger.Logger.html) class from `@salesforce/core`.

## Add Logging to a Command

Let's show how to add logging to the initial `hello world` command that `sf dev generate plugin` generated.

1. Open `src/commands/hello/world.ts` and update the import from `@salesforce/core` to include the `Logger` class:

   ```typescript
   import { Messages, Logger } from "@salesforce/core";
   ```

2. Now create child logger instance. The `Logger` class creates a root logger with some defaults. Other code, like a command or library, then creates a child logger using the `Logger.child()` method. To try it out, replace the `run` method with this code:

   ```typescript
     public async run(): Promise<HelloWorldResult> {
       const log = await Logger.child(this.ctor.name);

       const { flags } = await this.parse(World);
       const time = new Date().toDateString();
       const message = `Hello ${flags.name} at ${time}`;

       log.debug(`Time: ${time} | Name: ${flags.name}`);

       this.log(message);
       return {
         name: flags.name,
         time,
       };
     }
   ```

   The example shows how you pass the name of the child logger as the first argument of `Logger.child()`. In the example, it's set it to `this.ctor.name` which points to the name of the command class (`World` in our case). But it can be anything you want.

3. Now make the logger print records to the terminal. First set the `DEBUG` environment variable, either to `*` to see all log records or `sf:_LoggerName_` to filter records by logger.

   For now, let's print logs for only the `hello world` command:

   For bash/zsh:

   ```bash
   export DEBUG='sf:World'
   ```

   For PowerShell:

   ```bash
   $Env:DEBUG = 'sf:World'
   ```

4. Run the `hello world` command using `bin/dev` as usual:

   ```bash
   ./bin/dev hello world --name Astro
   ```

   You see log and command output similar to this:

   ```bash
     sf:World DEBUG Time: Wed Sep 7 2022 | Name: Astro +0ms
   Hello Astro at Wed Sep 7 2022
   ```

5. Remember to unset the `DEBUG` environment variable after you're done:

   For bash/zsh:

   ```bash
   unset DEBUG
   ```

   For PowerShell:

   ```bash
   $Env:DEBUG = ''
   ```

## Logging Levels

The `Logger` class wraps [`pino`](https://github.com/pinojs/pino), which already sets log levels. See [logger.levels](https://getpino.io/#/docs/api?id=levels) for more information.

## Log Storage

Salesforce CLI saves logs to a file in the global `.sf` folder; you can turn off this behavior by setting the `SFDX_DISABLE_LOG_FILE` environment variable to `true`. The CLI starts a new log file every day, formatted like `sf-YYYY-MM-DD.log`. The CLI cleans them up if they're more than 7 days old.

Salesforce CLI saves log records to a log file only if the log level method used is the **same level or higher** than the level set in the logger instance.

Let's see how this works. By default, the root logger sets the log level to `warn`. This example shows calls to `debug`, `warn`, and `error` methods, but only the `warn` and `error` records are saved to the log file:

```typescript
log.debug("test1"); // log level: 20, below 40 so don't save it.
log.warn("test2"); // log level: 40, same level as the logger instance so record goes to file.
log.error("test3"); // log level: 50, above `warn` so it's saved to a file too.
```

The user can set the log level with environment variables (`SF_LOG_LEVEL=debug` or `SF_LOG_LEVEL=trace`) so it's best not to rely on code to manage your child Logger. Rather,let the user control the log level.
