Salesforce Debug Log Management
If you’re looking for an easier way of setting up and managing Salesforce debug logs then the following tool could help.
In an effort to find an easier way of setting up and extracting debug logs for users/integrations so they can be used in log analysis tools or having the ability to supply the debug logs for others led me to build this tool. The problems I’ve encountered include: the time it takes time to setup the debug trace, lack of built in tools for extracting the logs, and storage limitations.
Below's a snippet of the program extracting the logs via a Docker container, removing them from the instance and making them available via a nginx web browser.
Some of the use cases for this tool are: scheduled debugging for processes via cronjobs, running simultaneous debugging instances for testers, providing logs for 3rd party vendors and automatically triggered if an exception is detected.
The program is in Python and leverages the SFDC tooling api in combination with sfdx. The code along with the README for installing can be found here, https://github.com/michael-adam-sheehan/salesforce-errorlog-extractor.
The tool can be used via Docker or run as a standalone python app. The Docker implementation also deploys a nginx web instance to host your logs.
Extraction
For extraction, it’s relatively straight forward, the code uses the tooling api to query the ApexLogs for the defined user get information about the logs, extracts and stores them in a format based on date/time and record id in a subdir for each user.
filename = f"{d.strftime('%Y%m%d-%H%M%S')}-{result['Id']}.txt"
Setting User Debug Trace
If you enable the user debug trace, the program will setup a debug trace with the finest level via a debug level called ‘SFDXDebugLevel’ if the user doesn’t have a debug trace flag that’s expired. The default setting for the expiration is 30min. This uses the tooling api and std api for the call. The only issue I can across was setting the TraceFlag, the call was slightly different patch request for the initial call vs a TraceFlag that’s already defined in the system.
Deleting Logs
This uses the sfdx force:data:bulk:delete command with a generated csv of log ids to delete. The reason I choose to do this versus using the bulk delete api is for simplicity. To use the bulk api, I would’ve had to make two calls, one to generate the job and then the batch to run while using the sfdx, I could do it in one.
Additional comments
The only other thing worth mentioning is how the Docker container for sfdx is deployed and might prove useful for anyone deploying sfdx via Docker.
TARGET_USERNAME="admin@teegrep-dev01.com" && docker-compose build --force-rm --build-arg TARGET_USERNAME=$TARGET_USERNAME --build-arg SFDX_AUTH_URL=$(sfdx force:org:display --verbose -u $TARGET_USERNAME --json | grep "sfdxAuthUrl" | sed -E 's/^.*(force:[^"]+).*$/\1/') web
Dockerfile:
Congratulations!