Custom Object to Hold Log Information












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.










share|improve this question



























    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.










    share|improve this question

























      1












      1








      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.










      share|improve this question













      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.







      apex custom-object create-records






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 12 '18 at 11:29









      Nuno Carvalho

      255




      255






















          2 Answers
          2






          active

          oldest

          votes


















          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" : ""}'
          );

          }





          share|improve this answer























          • 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.






          share|improve this answer





















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "459"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242294%2fcustom-object-to-hold-log-information%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            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" : ""}'
            );

            }





            share|improve this answer























            • 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
















            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" : ""}'
            );

            }





            share|improve this answer























            • 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














            1












            1








            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" : ""}'
            );

            }





            share|improve this answer














            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" : ""}'
            );

            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 12 '18 at 14:40

























            answered Dec 12 '18 at 11:46









            Pranay Jaiswal

            13.4k32351




            13.4k32351












            • 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


















            • 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
















            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




            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




            @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






            @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




            @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




            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.






            share|improve this answer


























              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.






              share|improve this answer
























                3












                3








                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 12 '18 at 14:54









                Adrian Larson

                104k19112235




                104k19112235






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f242294%2fcustom-object-to-hold-log-information%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?