How to get just the system.debug output when executing code?
Asked Answered
A

3

6

I wrote a simple program and want to see the output when I run the code. When I run it in the force.com IDE using the 'Annoymously execute apex code'command I get a lot of unwanted results, when I only want the system.debug statements. I could do use notepad or excel, but it seems like there should be a direct way (either native, or tool). Any advice?

Thanks,

el-noobre

code

public with sharing class Aa_playground {



 public static void listExp(){
    List<Integer> x = new List<Integer>();
    x.add(1212);
    for (Integer i = 0; i < x.size(); i++){
        System.debug(x[i]);
    }

}
} 

output

Anonymous execution was successful.

24.0     APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
Execute Anonymous: Aa_playground.listExp();
13:40:52.037 (37218000)|EXECUTION_STARTED
13:40:52.037 (37228000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
13:40:52.037 (37634000)|METHOD_ENTRY|[1]|01pQ000000062u5|Aa_playground.Aa_playground()
13:40:52.037 (37726000)|METHOD_EXIT|[1]|Aa_playground
13:40:52.037 (37740000)|METHOD_ENTRY|[1]|01pQ000000062u5|Aa_playground.listExp()
13:40:52.037 (37920000)|USER_DEBUG|[9]|DEBUG|1212
13:40:52.037 (37947000)|METHOD_EXIT|[1]|01pQ000000062u5|Aa_playground.listExp()
13:40:52.594 (37979000)|CUMULATIVE_LIMIT_USAGE
13:40:52.594|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of script statements: 5 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

13:40:52.594|CUMULATIVE_LIMIT_USAGE_END

13:40:52.038 (38005000)|CODE_UNIT_FINISHED|execute_anonymous_apex
13:40:52.038 (38011000)|EXECUTION_FINISHED
Atalie answered 24/4, 2012 at 17:50 Comment(5)
This link might be helpful: fishofprey.com/2011/02/…Declinature
Matt FTW! I modified his notepad++ macro- here's the regex to use for just the USER_DEBUG statements. Then use Text-F/X to clean it up. ^(?!.+USER_DEBUG.+$).*$Atalie
Also- the TextF/X is buggy when you automate it. Instead, here is a second regex to add to clear up the blank lines: (?m)^([ \t\s]*|;.*)(\r?\n|$) Put that and the above in your macro and you're done!Atalie
Wow, putting it all together (especially with that 2nd regex) in a macro works excellently.Declinature
UGH- there's a bug. It occurred to me that statements like this are legal degugging statements: System.debug('after change: '+ '\n'+'are you there?'); The 'are you there' is gone as a result of these regexes. The proper fix is for me to get a list of salesforce output statements and exclude each of those. Thus you'd be left with the Debug statements, and the summary at the end.Atalie
D
8

I figured I'd sum up some of the info I found, in an answer.

  1. Download Notepad++ v6.1+
  2. In Notepad++, open (or paste in) your debug log file.
  3. From the Menu, select Macro > Start Recording.
  4. Press CTRL-H (or the shortcut for Search and Replace).
  5. Copy this regular expression ^(?!.+USER_DEBUG.+$).*$ and paste it in the "Find What:" textbox.
  6. Make sure the Search Mode is set to Regular Expression and that the "Replace With:" textbox is blank.
  7. Click "Replace All" and "Ok" when the result dialog apprears.
  8. Copy this regular expression (?m)^([ \t\s]*|;.*)(\r?\n|$) and paste it in the "Find What:" textbox.
  9. Make sure the Search Mode is set to Regular Expression and that the "Replace With:" textbox is blank.
  10. Click "Replace All" and "Ok". Then, close the Search and Replace dialog.
  11. From the Menu, select Macro > Stop Recording. Then select Macro > Save Current Recorded Macro.
  12. Enter a Name for your new macro, and optionally set the shortcut keys. Then click "Ok".

To execute your new macro, select Macro from the menu then click your macro name.

Credit for the regular expressions goes to nivyaj (see question comments). Daniel Ballinger's blog post was helpful as well.

Declinature answered 25/4, 2012 at 18:57 Comment(2)
I know this followup question is pretty much context dependent, but generally speaking are there any other output statements one would want to see as well? I'll see if I can find a list of output statements and make a set of macros...Atalie
It's context dependent, as you mentioned, but you may want lines with EXCEPTION_THROWN, DML_, or CODE_UNIT_.Declinature
M
5

This is probably bad practice, but what I have done in the past is set the log level of APEX_CODE to INFO and set the log level of the debug message to info as well

System.debug(Logginglevel.INFO, 'Debug Message with INFO level');
Miguelinamiguelita answered 5/7, 2012 at 23:27 Comment(1)
From the docs it sounds like it is quite acceptable: "DEBUG and above by default. If the user sets the log level for the System.Debug method, the event is logged at that level instead."Bannerol
K
1

You can eliminate the LIMIT_USAGE_FOR_NS messages by setting the Apex Profiling level to None. But, you can't get rid of the METHOD_ENTRY AND METHOD_EXIT messages and still get the USER_DEBUG messages too since those messages are higher than the System.debug() messages in the filter. Unfortunately.

Kildare answered 24/4, 2012 at 19:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.