4

I am trying to implement logger using apache commonn logging and log4j.xml file as configuration file .

So in the actual java code i am writing log as

I am using appache common logging

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

and i am logging information such as in . I am creating a instance of log for each class

 private static Log    logger  = LogFactory.getLog( MyClass.class );
    private static final String      name   ="SAM";
    logger.info("name= "+name);
  1. So now my question is does implementing logger like this create a performanace issue?

  2. Is it necessary to check log like

    if ( log.isInfoEnabled( ) ) 
     {
      log.info( "Info message" );
      }
    

The confusion is mainly because in Apache common logging they have mentioned to do this and in log4j they have mentioned it is unnecessary.

Each time when we write log in the files?

-Sam

1
  • 1
    for most applications there is no point to using commons-logging. commons-logging is for libraries and frameworks so that you can include different components in your application and have them log to the same place. When building an application you should be able to pick one logging implementation and use it consistently, so having a facade only adds problems. Commented Dec 28, 2012 at 17:23

3 Answers 3

2
  1. yes and no, logging always reduces your performance, but some functions are more expensive then others, e.g. getting the calling class/method-Name uses reflection and is very slow. But a normal logfuntion is not that expensive if you do not have an expensive statement in the calling logging function (this will be evaluated every time before the log-Level is checked. In this case you can use the .isLevelEnabledcheck to prevent the evaluation). Also logging to the console takes longer for the output than logging to a file. You will find more information about this by googling and in the FAQ/manual of log4j.

  2. You do not have to check the Log-Level before logging. This is done within the log-function itself. Therefore are the different methods for every Level or the Level-Argument in the generic log-Method.

Sign up to request clarification or add additional context in comments.

3 Comments

The only time I explicitly check the logging level is if I have a lot of low severity statements grouped together. For example, if I have 10 debug logs in a row, and I normally have that class set to error, I may wrap them in a check. I doubt it makes any meaningful difference though.
So you are saying that i do not need care about checking the levels most of time?
yes don't check the Level. Checking the Level is redundant and blows up your lines of code, what will lead to a lower code-readability.
2

A big potential performance problem with logging is usually if you have something passed into the log method that is very expensive to convert to a string. That's why you have methods like isInfoEnabled(), so that the code can avoid creating the message string from the parameter (otherwise the check within the info() method is too late, the conversion is already done). If your objects passed into the log methods are strings or are not very involved then the is*Enabled() methods won't be that useful.

SLF4J is worth checking out. It doesn't depend on classloader tricks (which is a big part of why commons-logging is reviled), and it has a different way of creating log messages which delays when the message string gets created so that the enabled check can take place within the logging method.

2 Comments

So If i trying to catch an exception say logger.error("Exception ", exception) i need to check if error level is enabled because it will convert it to a string ? and what about the case something like this ` logger.info("name= "+name)` ! Could you please explain the same as well?
@Sam: maybe. but for errors, as a practical matter you always want to see them, so it's not like you need to handle the case where logging level is set to exclude ERROR (if you do, you have other problems, like too many exceptions getting thrown ^_^ ). Checking enabled is more for cases with trace and debug logging, where you want to see a lot of output at certain times but want to exclude it in production.
1

There's a better way to do all of this. You can get really great performance without having to add the clutter of ifDebugEnabled, etc methods. Check out something like Logback (or SLF4J). Here's the great documentation about what kind of an API you want. Note that Log4J and Commons-Logging doesn't have an API like this. Use Parameterized Logging.

4 Comments

Sorry but its a requirement that i use commons logging because of multiple types of framework used for logging at varies levels in the application.
SLF4J is just a wrapper around other libraries, so you can still use the SLF4J API but have it use Commons Logging behind the scenes. If this isn't possible then I would recommend writing your own wrapper around your logging and implementing this kind of API. It is absolutely the right way to do logging, all the speed of checking if you should write the log entry, but none of the extra code.
I am using log4j for configuration purpose and common logging just as a front end
@Sam check out this, it describe how this works. It sucks adding another library but the SLF4J/Logback API are just a better way to log, so I think it's worth it. slf4j.org/legacy.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.