8

Is there a way to know if we can get class name or trigger or a component along with a debug statement like a system.debug line no and class to which it belongs?

I asked this because i came along a long debug logs file and couldnt get the line no in the class which I supposed it could have .

8

Open the log in dev console and select Debug Only. Then select the columns File and Method

enter image description here

In the above image, File corresponds to the Id of class which can be opened directly from url with /<--Id--> and Method is the apex method from which debug is originated. And ofcourse the line numbers of debugs are clear.

Class: enter image description here

  1. 01p0K00000GslFt is Id of class as shown in Url .
  2. myMethod is apex method and .
  3. 17 and 18 are line numbers .

ADDED BASED ON COMMENTS

Its not always possible to educate all developers to add source class/method in logs so I follow as below for downloaded logs:

Even when you have downloaded log, you can search for METHOD_ENTRY and/or METHOD_EXIT between which you will find the logs. Note that you will still get the method name and Id of class.

enter image description here

Search is actually better in VS Code:

enter image description here

  • Ah, it would appear that I've learned something today. The file being an Id is perhaps a bit obtuse, but better than nothing. I'd imagine that this is some dev console magic. Selecting those columns wouldn't cause that information to appear in a downloaded log file (and won't necessarily be available in other tools), right? – Derek F Dec 9 '19 at 16:53
  • @DerekF Thanks for pointing out downloaded logs. In downloaded file also you will get the method name and class Id, it just takes little more time - so I think its better to search in tools like vs code. added in answer. – salesforce-sas Dec 9 '19 at 18:14
  • great points thanks a lot – user75937 Dec 10 '19 at 7:22
6

The line numbers do appear in the debug logs, e.g.

08:02:34:164 USER_DEBUG [375]|DEBUG|LINE ITEM COUNTER B:1

That "[375]" in the log is the line number that this particular system.debug(); statement appears on.

If you're familiar enough with your codebase to take a guess at what classes might be involved, it's usually pretty simple to pin it down to the specific class based on the line number and the debug message. In some cases, line number alone is enough.

As for getting the class name into the debug statement, you'd have to add that yourself.

Apex doesn't really have that great of reflection/introspection capabilities. Personally, I've been adding a class variable to store the name of the class to minimize typing (and allow me to copy/paste debug statements). Something along the lines of

public Class MyClass{
    // While this would be a fit for a static variable, doing so would mean
    //   that you'd need to type out "MyClass.className" every time you wanted to use it.
    // We're mainly concerned about the name of the class that is currently
    //   executing, so having className being private is fine.
    private String className;

    public MyClass(){
        // So, instead, I keep it as an instance variable, and populate
        //   it here in the constructor
        this.className = MyClass.class.getName();
    }

    public void myMethod(){
        // Using an instance variable means that we can just use "this.className"
        // Applying this same pattern to other classes means you don't need to change
        //   anything if you copy/paste.
        system.debug(this.className);
    }
}

Of course, you could always just type out the name of the class in each debug statement, or forego a class variable and use MyClass.class.getName(); (every apex class, and classes provided by Salesforce, have a .class variable that gets you a instance of Type, which has the getName() method).

I like being lazy though, er... uhh, I mean, re-using code and minimizing the work needed to do so.

  • 1
    Not correct. You can directly get the details of class and method as shared in this – salesforce-sas Dec 9 '19 at 16:45
  • thanks for the detailed explanation it helped along with salesforce-sas points – user75937 Dec 10 '19 at 7:21
1

You can easily get a Class name and method name from debug logs. Let's say you found a line (debug, SOQL query, whatever...) and you want to know where it is called. Just go up line by line and find a log with METHOD_ENTRY event type. Like:

14:46:15.44 (64158770)|METHOD_ENTRY|[13]|01p2p000003s8c5|SomeClass.fakeMthd()

Take a look at a real example enter image description here

0

Probably starts by getting to know better of the debug log.

Usually what I did was searching for the first METHOD_ENTRY debug event which was logged before the USER_DEBUG line, that would tell me the class and its method name.

For example:

113122:21:24.630 (634997684)|METHOD_ENTRY|[48]|01p2N000000Kxmv|AccountTriggerHelperInt.updateContact()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.