Getting started with Apex debug logs 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.

Posted

in

by


Related Posts

Comments

11 responses to “Getting started with Apex debug logs in salesforce”

  1. bhagirathi behera Avatar
    bhagirathi behera

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

  2. Ibtesam Avatar
    Ibtesam

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

    1. JitendraZaa Avatar
      JitendraZaa

      No, You have to add them manually.

  3. Amit Avatar
    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?

  4. Mani Avatar
    Mani

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

    1. JitendraZaa Avatar
      JitendraZaa

      System log is now known as Developer log.

  5. vamshi Avatar
    vamshi

    Hi Shiva,

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

    1. JitendraZaa Avatar
      JitendraZaa

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

  6. Karthick Raja Avatar
    Karthick Raja

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

  7. Khalil Badshah Avatar
    Khalil Badshah

    you have given details of how to check debug log? how to check system log?

    1. Jitendra Zaa Avatar

      You can check System Audit trail. It is available for SOQL as well. SELECT Id,
      Action,
      CreatedBy.Name,
      CreatedDate,
      Display,
      Section
      FROM SetupAuditTrail

Leave a Reply to IbtesamCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading