Getting started with Apex debug logs in salesforce

Introduction to debug logs in salesforce with step by step procedure and screenshot, Difference between Debug log and System log in Salesforce

For the newbie and intermediate developers, most of the time it becomes very necessary to log the program at some stage / line and look into those that what actually is happening with the code.

In salesforce it is very easy to log and track the apex program. To log the apex code use below line of code:

System.debug(“Your message”);
or
System.debug(Logginglevel,”Your Message”);

Log levels available in Apex are (listed from lowest to highest) :

  • ERROR
  • WARN
  • INFO
  • DEBUG
  • FINE
  • FINER
  • FINEST

Log levels are cumulative. For example, if the lowest level, ERROR, is specified, only debug methods with the log level of ERROR are logged. If the next level, WARN, is specified, the debug log contains debug methods specified as either ERROR or WARN.
In the following example, the string MsgTxt is not written to the debug log because the log level is ERROR, and the debug method has a level of INFO.

System.debug('msg');
System.debug(Logginglevel.INFO,'MsgTxt');

For more information on log levels, see “Setting Debug Log Filters” in the Salesforce online help.

Lets assume you have written below line of code in one of the controller extension’s constructor :

System.debug('*** In the constructor of extension class ***');

First step is to enable the log for particular user who is going to (reason to) execute that code. Go to “Setup | Administration Setup | Monitoring | Debug Logs“.

Debug Logs in Monitoring section of Administration setup - salesforce
Debug Logs in Monitoring section of Administration setup – salesforce

To enable the log for this user click on new button as shown in below image.

List of Users for Debug log enabled Salesforce
List of Users for Debug log enabled Salesforce

on next screen select the user and save the operation.

Now execute that visualforce page on which the code is written.

After execution again go to the Debug Logs page, where the user is added. You will see the screen like below

View Debug logs in Salesforce
View Debug logs in Salesforce

As you can see now that new row is added in “Debug Logs” section. click on the “view” button.

Debug statement printed in debug logs
Debug statement printed in debug logs

And the log is printed as shown above.

Difference between Debug log and System log in Salesforce

Normally the debug log gets confused with the System log. Difference in both log is that , the System log contains all the system related information, anonymous apex execution etc however the debug log contains all the debug statements and program execution related to the user for which the debug is granted.

For any query or suggestions, please leave the comments. your feedback is very important.

Related posts

  • bhagirathi behera

    SIR AM BHAGIRATHI BEHERA  HOW TO WRITE INTERFACACE CONCEPT IN APEX CODE AND HOUW TO  CALL IN TEXTCLASS
     

  • Ibtesam

    does system admin of the org has by default logs enabled?

    • JitendraZaa

      No, You have to add them manually.

  • Amit

    My debugs logs are crossing the maximum limit allowed for a debug log file. I there any way I can see the complete debug log file?

  • Mani

    What is the difference between Debug logs and System logs ??

    • JitendraZaa

      System log is now known as Developer log.

  • vamshi

    Hi Shiva,

    How should I display system.debug(‘hi’); on visual force page could you please give me the sample code.. thank you

    • JitendraZaa

      Not possible… To show something in vf, u can simply use string variable.

  • Karthick Raja

    Hi JitendraZaa,
    Can you tell me the general debugging in salesforce with an example