Is it possible to check the current LoggingLevel in Apex code and conditionally vary the execution?


// Somehow detect the current logging level here?
System.LoggingLevel level = LoggingLevel.FINEST;

if(level == LoggingLevel.FINEST) {
  // Extra logging code that may make additional SOQL calls, etc...
  // that you don't want during standard execution.
  System.debug(logginglevel.INFO, level);
  • Is it not the case that the logging flow is done continuously in sf, and the levels are only a filter to what is stored or displayed ? Aug 31 '12 at 5:32
  • @Sdry I don't know. I had assumed that only the levels that existed during the execution are captured. Otherwise you would be able to vary the logging level after the execution. Aug 31 '12 at 6:05
  • Pretty sure you can now work this out via the Tooling API at least. Can't test for a few days though.
    – Adrian Larson
    Jan 12 '18 at 12:28
  • @AdrianLarson Yup. Can check the DebugLevel associated with the TraceFlag, but it isn't possible to do so without a callout. Jan 12 '18 at 21:07
  • Indeed it would. Your question doesn't specify that as a deal breaker. Is it?
    – Adrian Larson
    Jan 12 '18 at 23:56

Unfortunately, I don't think there is a way to check the current logging level in APEX.

UPDATE: I've (Daniel) finally got around to creating an idea on the Idea Exchange to allow Apex to detect the logging level for the transaction and alter the logging accordingly - Allow apex to detect the logging level so expensive logging can be skipped.


You can vary the Logging Level by using a LoggingLevel variable in your debug calls rather than the constants. Then at least you can turn some messages on or off depending on the code you are debugging.


// Normally, suppress messages
LoggingLevel myLogLevel = LoggingLevel.NONE;
System.debug(myLogLevel,'A debug message that does not interest me today');
LoggingLevel myLogLevel = LoggingLevel.DEBUG;
System.debug(myLogLevel,'A debug message that I want to see today');
if(myLogLevel == LoggingLevel.DEBUG) {
  // do conditional actions here
  • That doesn't answer the question at all. OP wants to know what the logging level is on the user trace in order to do more expensive logging only when it would hit the logs.
    – Adrian Larson
    Jan 12 '18 at 12:27
  • @AdrianLarson, It does help. If you track the logging level in a variable, then you have control of it and you can code conditional on logging level. I will update my example.
    – Dizzley
    Jan 14 '18 at 10:43
  • 1
    You can set your variable from a custom setting/MDT when you set trace on.
    – Dizzley
    Jan 14 '18 at 10:49

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.