Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I want to insert a record to the DB that includes information about the process and class that is currently executing (something like process id and class and method name).

Is there any way to capture these (or similar) with Apex?

share|improve this question
    
What do you mean by "process id"? Are you referring to batch process IDs? –  Peter Jan 11 '13 at 17:24
    
Are you wanting to do this for exception logging? –  Mark Pond Jan 11 '13 at 17:51
    
Wondering what could use case to capture this information. Given the limitations of salesforce at database level, it would be more easier to capture what action caused so and so class to execute or to write to database on to a custom object and possibly include if the database write was successful or not. –  Sai Jan 11 '13 at 18:34
    
I want to capture usage information using the Limits class. Each time I capture this info I want to add a timestamp and an identifier of the class/method that it was captured from –  Dedo Jan 12 '13 at 11:43

2 Answers 2

This is going to be ridiculous but if it's really what you need...

Throw an exception and catch it, then parse the stacktrace ;)

EDIT

Daniel Ballinger was kind enough to test how stack trace behaves in managed packages and it seems to be a no-go. So while interesting - my idea works only in unpackaged code.

public class CrazyLogger {
    public static String log(){
        try{
            throw new LoggerException();
        } catch (LoggerException le){
            String stackTrace = le.getStackTraceString();
            //System.debug(stackTrace);
            List<String> stackLines = stackTrace.split('\n');

            String caller =  stackLines[1]; // stackLines[0] = CrazyLogger, we need to return info from 1 level up.
            return caller.left(caller.indexOf(':'));
        }
    }

    public class LoggerException extends Exception{}
}

And if I use it in my class as CrazyLogger.log() it's returning ClassName.methodName

But if you do this in order to capture Limits you must realize that inserts you plan to make will be also rolled back if you'll hit a gov. limit...

share|improve this answer
    
Nothing wrong with a ridiculous solution, as long as it's a solution :) Love it! –  zachelrath Feb 11 '13 at 1:26
    
haha, this is awesome. I never thought of using the stacktrace to figure out where we are. –  Greg Grinberg Feb 11 '13 at 17:58
    
Does getStackTraceString() work in a managed package? Often the stack trace gets blanked out for an installed package. Just something to keep in mind. –  Daniel Ballinger May 9 '13 at 0:22
    
@DanielBallinger I don't know :) I'd expect it to work though if you don't step out of the managed package (so the code anonymizer doesn't have to kick in). I understand that whenever I get fatal error and "your package manager has been notified" pops up they get an email with org id, user id and stacktrace? –  eyescream May 9 '13 at 8:23
1  
@eyescream I have a managed package where I assign the Exception.getStackTraceString() to a string and then store that string in a custom object field. During development I can see the stack trace, but when installed as a managed package the field only shows '(myNamespace)'. Either Salesforce is really smart in tracking the usage of the stack trace and hidding it or the stacktrace is always hidden. –  Daniel Ballinger May 9 '13 at 19:48

Apex itself has limited reflection capabilities. I don't know that you'll be able to easily do this in a non-hard-coded way. You could certainly write logic that simply takes your input as a developer and adds that to a custom log object in our database.

But there are two other problems I can think of. First of all, every time you decide to write some data to a table you will be using DML governor limits (max DML rows and calls).

The other problem has to do with the automic nature of Apex. In other words, if there was a failure in the transaction (potentially even a validation rule failure, provided it was an all-or-nothing transaction), you would only get data recorded when transactions were 100% successful. Otherwise your writing to the SFDC database would be rolled back just like any other DML.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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