Skip to content

Instantly share code, notes, and snippets.

@roko-p
Created August 15, 2021 13:32
Show Gist options
  • Save roko-p/d5d519c1cb4addfdd4644fd11c0aaa02 to your computer and use it in GitHub Desktop.
Save roko-p/d5d519c1cb4addfdd4644fd11c0aaa02 to your computer and use it in GitHub Desktop.
Download all debug logs from a Salesforce org
#!/bin/bash
# This is a script by Thomas Gagné posted here:
# https://trailhead.salesforce.com/trailblazer-community/feed/0D54S00000A8irTSAR
# but modified to use the `sfdx` CLI tool instead
# of the `force` CLI tool.
# This assumes the user is logged in and the default
# org is set. If you're running this outside of a
# Salesforce DX project folder, just add the username
# (to specify which org to run this against) to both
# sfdx commands with `-u your.username@whatever.com`
# Original comments below:
# script for export ALL debug logs to a subdirectory
# named "log".
#
# it requires the "force" CLI tool be available and
# assumes the user is already logged in.
#
# Sure, I could have added more debugging, like checking
# to see if the user was logged in, but I didn't have
# all night. ;-)
#
# 2017-08-29 tggagne@gmail.com
for logId in $(sfdx force:data:soql:query -q "select id from apexlog" | tail -n +3); do
echo "writing log/$logId.debug"
sfdx force:apex:log:get --logid $logId > log/$logId.debug
done
@Hcision
Copy link

Hcision commented Aug 31, 2021

How can i launch this in SFDX CLI ?

@roko-p
Copy link
Author

roko-p commented Aug 31, 2021

How can i launch this in SFDX CLI ?

@Hcision What do you mean? SFDX CLI is just a collection of commands, right? And one of those commands is that sfdx force:apex:log:get in there, for example.

What this piece of code is is a bash script. It's a loop that goes through the list of all the logs from your org and downloads each one to a file. It uses bash stuff to do this (for loop statement, tail and echo commands, redirection (>) and pipe (|) operators...).


The simplest way to run this is to just paste it into your Terminal/Command Prompt, where you usually execute your SFDX commands.


(Also, don't forget to read that first paragraph of the #comments.)

@Hcision
Copy link

Hcision commented Aug 31, 2021

sorry i'm new in the ecosystem , i just launched a piece of code on anonymous and i want to extract records in a csv file that's all

@aviationfan
Copy link

Just curious as to why the use of tail -n +3 after the soql query?

@roko-p
Copy link
Author

roko-p commented Dec 20, 2021

@aviationfan If I remember correctly, there are some header lines so they are left out with that tail part so that you get only the logs

@aviationfan
Copy link

aviationfan commented Dec 20, 2021

I do see some weird files that must have been those lines at the top. I am trying this with powershell since I am stuck on windows right now.

this is a very helpful script

@kamal2231
Copy link

Hi @roko-p ,

I am facing this below error while running the script in VS code CLI terminal. Please suggest what I am missing
Error:
At line:1 char:4

  • for logId in $(sfdx force:data:soql:query -q "select id from apexlog" ...
  • ~
    Missing opening '(' after keyword 'for'.
    At line:2 char:3
  • do
  • ~
    Missing statement body in do loop.
    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    • FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

@roko-p
Copy link
Author

roko-p commented Nov 27, 2022

Hi @kamal2231 ,

This looks like you are trying to run this in PowerShell, and this is a bash script, so try to search for how to run a bash script from Windows PowerShell (maybe this helps?)

@prakharxagrawal
Copy link

I am getting:

line 1: sfdx: command not found

@roko-p
Copy link
Author

roko-p commented Apr 26, 2024

I am getting:

line 1: sfdx: command not found

@prakharxagrawal you need to have the Salesforce CLI installed first: How to.

(You probably won't see sfdx in these docs any more, but sf instead, which is the new command replacing the old sfdx. I believe you will still get the sfdx command installed together with sf, but in case you don't you can just try replacing the sfdx in the script above with sf and everything should still work.)

@prakharxagrawal
Copy link

I am getting:
line 1: sfdx: command not found

@prakharxagrawal you need to have the Salesforce CLI installed first: How to.

(You probably won't see sfdx in these docs any more, but sf instead, which is the new command replacing the old sfdx. I believe you will still get the sfdx command installed together with sf, but in case you don't you can just try replacing the sfdx in the script above with sf and everything should still work.)

Hi @roko-p, I Have SFDX installed on my system, I have tried running it on VS Code Terminal, Bash Terminal, Command Prompt, everywher. I get the same error, Additionally, I am able to run both SFDX Commands individually that are inside for loop.

@prakharxagrawal
Copy link

I am getting:
line 1: sfdx: command not found

@prakharxagrawal you need to have the Salesforce CLI installed first: How to.
(You probably won't see sfdx in these docs any more, but sf instead, which is the new command replacing the old sfdx. I believe you will still get the sfdx command installed together with sf, but in case you don't you can just try replacing the sfdx in the script above with sf and everything should still work.)

Hi @roko-p, I Have SFDX installed on my system, I have tried running it on VS Code Terminal, Bash Terminal, Command Prompt, everywher. I get the same error, Additionally, I am able to run both SFDX Commands individually that are inside for loop.

Hi @roko-p, I modified this to batch script and it worked for me, Thanks again :), Here's my modified script
https://gist.github.com/prakharxagrawal/416dfa0fe8621c543ff4097474eb937e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment