function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ketan vinodrai mehtaketan vinodrai mehta 

Read debug log details

Hi All,
    I want to read entire record of a debug log in my code. I am able to query ApexLog object but the log description ("LOG") column is not available. Please do guide.

Thanks in advance,
Ketan Mehta
Best Answer chosen by ketan vinodrai mehta
Dino UrsicDino Ursic

Almost 3 years too late, but just thought I'd save someone's time if they stumble upon this question like I did.

As explained in this SO topic (https://salesforce.stackexchange.com/a/216917), you cannot access the body of the ApexLog directly, but rather have to query it via REST API.

So to read contents of the log file in apex class, you would have to first query the ApexLog record with SOQL, then retrieve the contents of the log file with REST request, like so:

ApexLog al = [SELECT Id, LogUserId, LogUser.Name, Request, Operation, Application, Status, StartTime, Location, LogLength FROM ApexLog LIMIT 1];

// Set HTTPRequest Method
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm() + '/services/data/v45.0/sobjects/ApexLog/' + al.id + '/Body');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

// Execute web service call
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

If you use operation in your query i think you will get description as 
Name of the operation that triggered the debug log, such as APEXSOAP, Apex Sharing Recalculation, and so on.​
 
SELECT LogUserId,Operation,COUNT(Id) FROM ApexLog
Please refer below links to know which can be used for ApexLog object.

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_apexlog.htm
 
Hope this helps you!

Please mark it as Best Answer if my reply was helpful. It will make it available for other as the proper solution.
 
Thanks and Regards
Sandhya


 
ketan vinodrai mehtaketan vinodrai mehta
Hi Sandhya,
    Thanks for the answer. I have tried all the fields documented by SFDC on APEXLOG class and it is not available. Is there something I am missing?
Dino UrsicDino Ursic

Almost 3 years too late, but just thought I'd save someone's time if they stumble upon this question like I did.

As explained in this SO topic (https://salesforce.stackexchange.com/a/216917), you cannot access the body of the ApexLog directly, but rather have to query it via REST API.

So to read contents of the log file in apex class, you would have to first query the ApexLog record with SOQL, then retrieve the contents of the log file with REST request, like so:

ApexLog al = [SELECT Id, LogUserId, LogUser.Name, Request, Operation, Application, Status, StartTime, Location, LogLength FROM ApexLog LIMIT 1];

// Set HTTPRequest Method
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm() + '/services/data/v45.0/sobjects/ApexLog/' + al.id + '/Body');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());

// Execute web service call
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
This was selected as the best answer