Download Recent Salesforce Debug Logs Using Salesforce CLI

Debug logs are an essential tool for Salesforce developers and admins. They help trace code execution, identify issues, and monitor system behavior. While you can view logs directly in Salesforce Setup, downloading multiple logs at once can be tedious. Thankfully, the Salesforce CLI (sf) makes this process quick and easy.

Using sf apex get log Command

The sf apex get log command allows you to download recent debug logs directly to your local machine. The syntax is simple:

sf apex get log --number <N> --output-dir <folder>

Parameters

  • --number <N>: Specifies the number of most recent debug logs you want to download.
    Example: --number 10 will download the 10 latest logs.
  • --output-dir <folder>: Specifies the directory where the downloaded logs will be saved.
    Example: --output-dir logs will save all logs into a folder named logs.

Example

To download the 10 most recent debug logs into a local folder named logs:

sf apex get log --number 10 --output-dir logs

What happens:

  1. Salesforce CLI fetches the 10 most recent debug logs from your org.
  2. The logs are saved in the logs folder in your current working directory.
  3. You can now open the .log files locally and search for errors, exceptions, or any debug statements.

Benefits of Using This Command

  • Time-saving: Download multiple logs at once instead of viewing them one by one in Setup.
  • Automation-ready: Combine with scripts to automatically download and parse logs.
  • Offline analysis: Search, filter, or share logs without logging into Salesforce.

Optional: Automate Log Analysis

After downloading logs, you can use simple tools like grep (Linux/macOS) or Select-String (Windows PowerShell) to search for keywords:

grep -i "Sandy" logs/*.log

OR 

Select-String -Path .\logs\*.log -Pattern "Sandy"

This instantly shows all occurrences of errors across the downloaded logs.


Conclusion

Using sf apex get log --number 10 --output-dir logs is the fastest way to retrieve multiple Salesforce debug logs. It saves time, allows offline analysis, and can easily be integrated into automation scripts for monitoring and debugging.

Next time you need to investigate multiple log entries, skip the Setup UI and use the CLI — your future self will thank you! 🚀

Published by Sandeep Kumar

He is a Salesforce Certified Application Architect having 11+ years of experience in Salesforce.

Leave a Reply