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 ? Commented Aug 31, 2012 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. Commented Aug 31, 2012 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
    Commented Jan 12, 2018 at 12:28
  • @AdrianLarson Yup. Can check the DebugLevel associated with the TraceFlag, but it isn't possible to do so without a callout. Commented Jan 12, 2018 at 21:07
  • Indeed it would. Your question doesn't specify that as a deal breaker. Is it?
    – Adrian Larson
    Commented Jan 12, 2018 at 23:56

2 Answers 2


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
    Commented Jan 12, 2018 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
    Commented Jan 14, 2018 at 10:43
  • 1
    You can set your variable from a custom setting/MDT when you set trace on.
    – Dizzley
    Commented Jan 14, 2018 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .