Thursday, January 25, 2018

Finding and downloading Salesforce debug logs via API

I guess Salesforce's UI for viewing/downloading persistent debug logs is OK.  Nothing fancy, but usable.  However that's only true if you have a handful to browse through.  Recently I was in a situation that was way beyond that limited circumstance.  We were troubleshooting a sporadic API error with Salesforce Support, and they asked us to log against the API user for 24 hours.  That means 10s of thousands of logs (if not 100+).  Can you imagine paging through them from Salesforce UI trying to find the useful ones?  Here's where an API approach comes in.

First step is to find the logs you want.  Downloading all of them is much less efficient.  Fortunately that can (usually) solved by a SOQL query on ApexLog object, where the metadata on debug logs is stored.


For my recent use case, it's sufficient to find just the failed transactions, but there are other useful filtering fields in ApexLog such as timestamps and types.

Next comes the actual retrieval of the log text.  That needs to be done through Tooling API.  The resource URI for REST is /services/data/v39.0/tooling/sobjects/ApexLog/[log Id]/Body/.  Log Ids would come from the query in step one.  Obviously you can used other compatible API versions as well.


I'm using Workbench as the tool just because it's unbelievably convenient for one-off operations.  If downloading is needed for a bunch of logs, some CLI script would probably be needed, which can also enable automated downloading if so desired.

No comments:

Post a Comment