A general question. We all put some System.debug() statements in our controllers for error handling, for info messages, etc.
But there are plenty of logs in the system. When some exception throws, how to narrow down to the correct log file to look at? Or do anyone using another proper mechanism to handling errors other than using custom objects?


2 Answers 2


I usually put ### in debug statements which helps in searching for debug statements in logs Or use can search USER_DEBUG in log for debug statements


System.debug('Opportunity Id #####'+o.Id);

Or In developer console you can Open the log for your latest action, then filter to show Debug Only

enter image description here

  • Looking for USER_DEBUG and all ok, my worry is how i can know which log file to looked at.And 20 logs per user so when an error occurs even we have put System.debug() in catch block, those may not be logged due to this limit.
    – highfive
    May 22, 2015 at 7:45
  • BTW, I can't see that Debug Only enabling check box. Any idea?
    – highfive
    May 22, 2015 at 9:53

Well, this is a big topic but a few points:

  1. Unless you have debug monitoring turned on for the running user, exceptions and logs won't exist anywhere - you'll need the user to be monitored and then repeat the use case
  2. Alternatively (and I can suggest Dan Appleman's Advanced Apex book by way of suggestion), you can implement a logging pattern wherein interesting things in your controllers of interest are written to a custom Log__c object for later analysis. This is even more helpful with async operations including batchables, queuables, future, ...
  3. And good practice is of course to wrap with try-catch where you can and using the catch block to issue useful diagnostics to the sysad and/or user (to forward to the sysad)
  4. I personally make all my system.debug(...) statements as System.debug(LoggingLevel.INFO,...) to avoid DEBUG noise in the sfdc-generated log. I also add reusable methods within my classes called debug() that return nicely-formatted, captioned strings of relevant local object values that can be used in producing a more human-meaningful log or diagnostics - this can help a lot in complex transactions or when returning to code that you haven't looked at for months or years.
  • about point #3.what's the way you are suggesting to send them to sysad?exactly this is what I'm looking for.sending a mail?I feel it's so heavy for every catch block :(
    – highfive
    May 22, 2015 at 7:41
  • if the catch block is catching unexpected exception, simply code a Util.notifySysad(theException) method -- not much coding overhead. The method can include the stacktrace;
    – cropredy
    May 22, 2015 at 9:16

You must log in to answer this question.

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