I'm using unlimited edition of Salesforce. I would like to enable debug logs for a user for a particular period of time. Currently Salesforce is only enablling a maximum period of 24 hours to enable debug logs. Once the 24 hours is over, I need to enable logs again. Is there any way to accomplish this (Enabling log for more than 24 hours)?

In an out of the box thinking, Is there a way to enable debug logs with Apex code? So that I can run a batch job every day so as to enable logs.

1 Answer 1


You will need to automate the creation of the TraceFlag record for the user in question. This could be done either via Apex calling the Tooling API, or via an external process.

TraceFlag records are what the Developer Console and the Debug Logs pages create to cause the debug logs to be captured.

  • Set the TracedEntityId to the UserId you want to get debug logs for.
  • Set the LogType to "USER_DEBUG"
  • ExpirationDate to some point in the next 24 hours.
  • DebugLevelId to the ID of a DebugLevel

If in doubt, perform the action manually via the UI and observe the changes to the TraceFlag records. You can SOQL for these in the Developer Console if you enable the Tooling API.

Soql for TraceFlag

  • Hi Daniel, Can you give more info about the same? Commented Aug 7, 2017 at 5:14
  • 1
    @ManuMurukesan What more do you need to know about it? Commented Aug 7, 2017 at 8:38
  • But the object TraceFlag, is not visible for me. Could you let me know how to get my hands on that? Commented Aug 8, 2017 at 9:20
  • @ManuMurukesan It will be accessible. I've added a screenshot of a SOQL query via the Developer Console. The important part to note is that the Tooling API isn't directly accessible via apex. You need to call it via the REST or SOAP APIs. There are wrappers available for it. Commented Aug 8, 2017 at 10:13
  • I tried the same. But got the below error Commented Aug 9, 2017 at 11:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .