is there a way to check a debug statement belongs to which class

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


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.