How to deal with production code logging (logback / slf4j)

I have implemented an algorithmic structure consisting of several classes. I am using slf4j and logback for logging. So many of my classes have code like:

protected final Logger logger = LoggerFactory.getLogger(Myclass.class);
...
logger.debug("Some debug information");

      

Now I would like to package my code and make it complete. What do I do with the registration code?

  • When running code, the developer usually wants to disable logging for performance reasons.
  • When developing your code, debug information is very useful, so removing all debug statements would be a bad idea.

Ideally, users should be able to enable / disable logging. I usually have a configuration file logback.xml that defines the log level for each class. Should I include such a file in the package? Or should I expect users to write their own or provide a default config file?

I have some code to set up algorithms:

/**
* Read algorithm confirguration from properties file
**/    
protected Configuration(Properties properties){...}

      

This function reads a properties file and sets a number of algorithmic parameters accordingly. Could this be helpful for enabling / disabling logging for various classes?

+3


source to share


2 answers


SLF4J and logback have little performance impact when logging to a level that is disabled. I've never run into a problem just leaving the logging code behind. It also has the added benefit that you can enable debug logging in production to get more information about an intermittent issue.

Since you create loggers using classes, you can simply configure those loggers so they don't add an application if you don't want any output from your database to be done. Or set the level to whatever is convenient for you in a production environment - just remember to turn off additivity so that events don't propagate to the root log. The syntax is described here: http://logback.qos.ch/manual/configuration.html#syntax

Another simple idea to allow your user to disable logging from your infrastructure with a simple switch is to use a token with an appropriate filter. You can change all of your entries to start with a marker:

private Marker MY_MARKER = MarkerFactory.getMarker("FrameworkMarker");

logger.debug(MY_MARKER, "Some debug information");

      

This allows you to suppress all log output marked with that specific marker with a single TurboFilter defined in your logback.xml application:



http://logback.qos.ch/manual/filters.html#TurboFilter

<turboFilter class="ch.qos.logback.classic.turbo.MarkerFilter">
    <Marker>FrameworkMarker</Marker>
    <OnMatch>DENY</OnMatch>
</turboFilter>

      

The MarkerFilter has the added benefit of not worrying about the level. Any log event with the specified marker is filtered out (and efficiently as described in the documentation).

Personally, I would go with both. This way you can use all levels of logging in your structure and disable logging entirely with MarkerFilter if needed, but you can also take advantage of verbose and specific logging when you need it.

+3


source


You can disable login at runtime by calling:

logger.setLevel(Level.OFF);

      

This will disable all levels for the calling registrar (TRACE, DEBUG, INFO WARN, ERROR will be disabled)

Manual redundancy:



Logging efficiency when logging is disabled completely

You can turn off logging entirely by setting the root level logger to Level.OFF, the highest possible. When logging is completely disabled, the cost of a log request consists of a method invocation plus an integer comparison. On a 3.2 GHz Pentium D machine, this cost is typically around 20 nanoseconds.

Read more about this on the log archiving guide page

Optional: It is good practice to gradle to set the build level according to the build type. development or production, etc. (setting a variable that the execution code can read later)

+1


source







All Articles