[SalesForce] Replacement for System.out.println() log statement – Please don’t say “Debug Logs”

In java it's easy to throw in a Sytem.out.println() and get some helpful info on the console. The closest I have come to that on Force.com is system.debug(); and then plowing through pages of Debug Logs trying to find my line of output. There must be a better way, right?

Best Answer

system.debug() is how you get the system to print to the debug log. There is no other way. If you don't like ploughing through pages of log (although when you view the log in the browser UI it's always just one page) there are several things you can do.

  1. Filter the log in the dev console as you suggest in your comment. Dev console works better in Chrome / Firefox / Safari than in IE but it isn't entirely glitch free in my experience
  2. Filter the log at source so that only your debug statements get into it see Debug Log Filtering for Apex Classes and Apex Triggers and Setting Debug log Filters

    To set log filters: From a class or trigger detail page, click Log Filters. Click Override Log Filters. The log filters are set to the default log levels. Choose the log level desired for each log category.

  3. The log file entry includes the line number where the entry originated. So if you're calling system.debug() on line 447, the entry includes [447]. Search for that in the browser and you'll find your debug.
  4. Old school - prepend an identifier to every debug output: system.debug('@@@ ' + someVar); and then CTRL-F search the log page for @@@ and you can easily skip through to all your statements.

EDIT: and there's a great full answer here: Need to get logfile smaller

There are also good arguments for building your own debug message store in a custom object, not least of which is that debugging an installed managed package in a client's org is still impossible as far as I know. Although I think they're working on making it possible.

And if you have access to sandbox, the apex debugger is coming. It was announced at DF14 but it's an extra cost.

Related Topic