1

I need some help at least with the Logic to be able to do this.

Basically i need to create a Record on a custom Object everytime an Apex Class is runned on success or error, that Record should show information about the class that was runned, the method, the error message if it exists , etc...

If you could give me some guidelines i would be very appreciated, Thank you in advance.

1

Custom Object :

You have to alter your apex class to support the logging functionality. You basically start with a custom object with the fields you need and then log when you want it.

You have to alter your code blocks in such a way that the committing of logs is always called.

Src : http://succeedwithsalesforce.com/creating-persistent-logs-using-apex-and-a-custom-object/

https://medium.com/slalom-technology/without-a-debug-trace-easier-logging-in-apex-ad09c3ce97b

Logging Using Attachment: Custom Objects logs are good, But they have size limitations and large text field size limitation that can force you to truncate. Having an attachment instead helps you have large raw logs on integration JSON/XML or even your own debugging to full potential. I have a sample utility that logs in an attachment instead.

public without sharing class LoggerUtil {

    private static Id parentId;
    private Static String bodyString;


    public static void initialize(Id parentIdToSCtore){
        //Intitialzie things the parentID to save later
        parentId = parentIdToSCtore;
        bodyString ='';
    }

    public static void log(String loggedString){
        bodyString = bodyString + loggedString +'\n';
    }


    public static void commitLog(){
        Attachment attachment = new Attachment(ParentId = parentId, Body=Blob.valueOf(bodyString),Name='RawLogger'+System.now());
        insert attachment;
    }

}

Edit: Code in Trigger

Trigger AccountTrigger on Account(after insert){

    ApexDebugLog.createLog(
                    '{"Type" : "Logging","ApexClass" : "AccountTrigger ","Method" : "createErrorLog","RecordId" : "","Message" : "On :"+Trigger.isInsert  ,"StackTrace" : ""}'
        );

}
  • On the first link you sended , how can i call the ApexDebugLog class on a Trigger? and pass the parameters. Thank you for your answer btw. – Nuno Carvalho Dec 12 '18 at 12:31
  • @Praynay Jaiswal – Nuno Carvalho Dec 12 '18 at 14:10
  • @NunoCarvalho I cant see any issue in calling it from Triggger.gist.github.com/miragedeb/ac39f97d622572cfdb8d – Pranay Jaiswal Dec 12 '18 at 14:12
  • @Praynay Jaiswal , but how can i call it even if there's no error on the process, i still want to create a record that shows what class and method was run, the time it was run, etc... can i do this without a try/catch? – Nuno Carvalho Dec 12 '18 at 14:24
  • Use that to be the first line of the trigger, and send exception as null, it will help you track it – Pranay Jaiswal Dec 12 '18 at 14:25
3

You can instantiate any standard exception and get its stack trace. Using regular expressions, you can from there determine the running class and method. I outlined that process here:

Get Currently Executing Class/Method Name?

I would be extremely wary of logging every method call. Each time you log would consume DML Statements, a fairly restrictive governor limit. Even if you cache your logs and attempt to flush the cache as near the end of your transaction as possible, it adds burden to the system which could cause failures. What's worse, LimitException cannot be caught.

If you insist on incorporating this kind of logging, be absolutely certain you incorporate a configurable flag to suppress logging behavior. You can use Hierarchy Custom Setting, Custom Permission, etc. The point is, an administrator in your org should be able to turn off this logging at any time without needing a deployment.

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.