Having the ability to capture various system log details is priceless when debugging code in your projects.
If you're new to development, you may not realize that nearly every mouse click you make within the Salesforce UI generates one or more "debug logs." A debug log contains information about everything that occurs after you click, which can include DML actions like inserting and updating, Workflow Rules that are fired off, triggers that are executed, callouts that are made as well as additional details about errors and exceptions that may (or may not) appear on the screen.
They're often the quickest way to quickly identify and isolate a problematic piece of code. They can contain tens, hundreds, if not thousands of lines of details. Rarely do you need to go through the logs line by line and often you'll define various filter levels to minimize the amount of "noise" in the logs that aren't of interest to you. You'll also have the ability to search for keywords and show only system.debug() results. They may seem intimidating at first, but fear not, you'll quickly learn a handful of tricks, keywords, and other filters that make all of that data more manageable.
Most often the code is being executed from within your own user's context, making it a cinch to capture the debug logs. This simple tutorial will outline how to capture Debug Logs via the Developer Console, or by navigating to the "Debug Logs" page from the setup menu.
Capturing Logs Via the Developer Console
Capturing logs for anything you can invoke is easy through the Developer Console.
Open the Developer Console
Click on your name in the upper-right hand corner and then click on "Developer Console"
Once the console window opens, ensure that you are on the "Logs" subtab. Cause your code to run and you should see new log entries appear.
How to Capture Logs Via Setup --> Debug Logs
Setup --> Debug Logs
Search for "Debug Logs" in the "Search all setup..." quick find box and then click on the "Debug Logs" link.
Add a New Monitored User
Within the "Monitored Users" list, you can see all of the Salesforce Users that are currently configured to capture logs. To add an additional user, including yourself, click on the "New" button.
Choose a User
Click on the lookup icon (to the right of the input box, to the left of the "Save" button). A pop-up will appear that will allow you to search for users. Click on the User's name in the "Search Results" section to add that user.
Debug Log Page Benefits
Capturing the debug logs from the Debug Log page is also beneficial for scenarios where your code is running under another user's execution context. Things like scheduled or batch jobs and custom inbound email services are associated with a running user that's not always guaranteed to be you. It might be another user in your org, a dedicated user, or even a user in one of your customer's orgs where your app is installed.
Salesforce Site Example
Another example is when you need to debug something that is executed via a public Salesforce Site. If you're not familiar with Salesforce Sites, not to be confused with Site.com Sites, it's simply a way of making a Visualforce page (or Apex Class for web services) exposed externally to the public, accessible via a Salesforce generated URL. Pages (or classes) exposed in this fashion are generally public facing, do not expose org data, and are very limited in permissions.
When a Salesforce Site is created, a dedicated "Web Site User" is also created and assigned to the Site. This "Web Site User" is also associated with a Profile that is dedicated to this User. This Profile is accessible via the Salesforce Site's details page by clicking on the "Public Access Settings" button.
Sometimes this User and/or its Profile will not be configured correctly and error messages won't bubble up to the Visualforce Page for you to diagnose. You can use the Debug Logs Page to capture this User's logs.
Accessing Salesforce Sites
From within the "Search All Setup..." quick find menu, search for "Sites" and then click on the "Sites" link under "Build" and "Develop."
Here I have a test configured, called "Sample Site."
Public Access Settings
Clicking on the "Public Access Settings" button will take you to the dedicated Site User's Profile, where you can update object and field level security.
Determine the User's Name
From the Profile, click on the "View Users" Button. This will take you to a page that displays all of the Users assigned to this Profile. Take note of the "Full Name" value (copy this to your clipboard).
Head back to the Setup --> Debug Logs page and click on the "New" button. Search for the Site's Name ("Sample_Site") or search for the full name you just copied ("Site Guest User, Sample Site"). The name should appear, allowing you to select it and begin to collect site generated logs via this user.
Note: The Site must be active for this user to appear in the search results.
For more information regarding Debug Logs is this page in the Developer's Guide: Understanding the Debug Log.