1

I have a simple Logger class with a static list of log objects and two methods. One method adds logs to the static list and the other method inserts the list. Right now I am manually calling the save logs method in a few different places. I was wondering if it is possible to have this method automatically called just before the Apex execution context finishes.

  • It depends on how you define - apex execution context. An apex itself is not executable, methods in the apex define the boundaries. So if you have a logging method, you will need to call it from individual methods. – Jayant Das Mar 12 '19 at 18:49
  • So I have two use cases specifically, in one case I have a batchable class that adds logs throughout execution, then at the end of the execute method I call Logger.SaveLogs();. The second case it a webservice method where I call Logger.SaveLogs(); at the end of this method. I was just curious if there's some sort of a trigger or way to know when the apex code will finish executing its current transaction. – Jordan Lee Mar 12 '19 at 18:56
  • For batch class, you can have those logging in finish() method. For any other method, you will want to log it when your method execution ends. That too you will need to make sure that you have handled the exceptions so that the execution reaches your logger. – Jayant Das Mar 12 '19 at 18:58
  • Ok, I thought that might be the case but figured I'd ask if anybody knew of a way to make that happen automatically. If you answer the question outside of a comment I'll mark it as accepted. Thanks for the help – Jordan Lee Mar 12 '19 at 19:03
  • See also this Idea by a familiar face. – David Reed Mar 12 '19 at 19:05
1

Rolling up comments.

Apex class itself is not executable. We would define methods and then call them independently depending on use cases. So if you have to log a particular execution context, that will the start/end of your method execution.

For your use cases:

I have a batchable class that adds logs throughout execution, then at the end of the execute method I call Logger.SaveLogs();. The second case it a webservice method where I call Logger.SaveLogs(); at the end of this method.

Fortunately batch class provides you a way to be able to log at the end when all batches have been executed. For this purpose, you can utilize the finish() method of the batch class.

For other custom methods in custom classes, you current implementation is the only option. There's no equivalent of finish as available in batch classes, so you will need to make sure you log only when your method execution ends.

|improve this answer|||||
3

To log only at the end of a transaction requires knowing when the transaction ends.

  • Batchable - by using Database.Stateful, you can write to the persistent log in finish()
  • Queueable - write to the log at the end of execute()
  • Apex REST - write to log before returning a response
  • Invocable Apex - write to log at end of static method
  • Visualforce controller - Write to log at end of each action method you care about (like doMySave()
  • Schedulable - same as Queueable
  • Apex Trigger - This can get tricky as knowing when the last trigger in the chain has executed is indeterminate as triggers can be invoked recursively such as through field updates (Workflow) or Process Builder record updates. Furthermore, if the trigger is not the initiation of the transaction, you might need to suppress any log initiation code (e.g. a VF controller doing DML - the trigger's code path is subsidiary to the transaction boundary defined by the VF action method. Also, a transaction can have more than 200 records and (except in Platform Event use cases, the trigger will get called more than once for each block of 200).

For triggers you could also consider establishing via a static variable a transaction context with a start datetime and name. Then use Platform Events to log out every loggable code unit including every trigger invocation's conclusion and have some other trigger subscribe to the Platform Events to re-assemble the trigger-scope logs into one large log covering the entire transaction scope (will need a scheduled job as well as knowing when the last trigger-scope log is complete is indeterminate.

|improve this answer|||||
  • Also, this idea (yes, it is my own Idea). – sfdcfox Mar 13 '19 at 12:24
  • @sfdcfox. Your Idea is superb. – cropredy Mar 13 '19 at 13:23
  • Yes, I have already upvoted that idea! Hope it starts gaining some traction and gets implemented. – Jordan Lee Mar 13 '19 at 17:35

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.