One of the common requirement in Java projects, that are using Log4j logging, is to have different log files for each module (or layer) in the project. For example, if you have a web application, you may want to log the debug/info messages from the service layer to a service.log file and the log messages from the presentation layer to the web-app.log file and so on. This is very simple to achieve in Log4j.
Log4j has a concept called Category using which you can classify a package as a category and assign a appender to that category alone. To make this point clear, consider that we have the below packages in our project:
com.someapp.service.myservice.SomeServiceClass.java com.someapp.dao.SomeDaoClass.java
Now, if we want to send the log messages from SomeServiceClass.java to service.log and SomeDaoClass.java to dao.log files respectively, then the log4j configuration would be something like this:
log4j.category.com.someapp.service = INFO, serviceFileAppender log4j.category.com.someapp.dao = INFO, daoFileAppender log4j.rootLogger = INFO, defaultAppender
As you can see in above snippet, we are defining two categories for the service and dao packages and associating them with the appenders serviceFileAppender and daoFileAppender respectively. These two appender are just any file appenders configured somewhere in the same log4j.properties file. (Have a look at this article: How to configure the File Appenders in Log4j). So, all the log messages that coming from the classes which resides under the com.someapp.service package will go to the serviceFileAppender, which in turn will go to service.log. The same logic applies to dao.log also.
Apart from these two logs, we also defined the defaultAppender as the root logger. Means, the log messages from all the packages will go to defaultAppender, including the service and dao log messages. Yes! with the above configuration, the service and dao log messages will be there in two places. In their respective log appenders + the defaultAppender.
Next: Additivity: What and Why?
Previous: How to send the log messages to a File?
{ 7 comments… read them below or add one }
The reason for the concept ‘Category’ still there is backward compatibility. You should use the the concept ‘logger’ instead
How can i redirect sysouts to a log file ? Any help will be appriciated.
Thanks in advance,
Milan.
@Milan
If you want to redirect the System.out.println statements to a log file, you need to use the function System.setOut for re-assigning the standard output stream.
Refer this link more information on how to do that: Redirecting System.out and System.err to a rolling log file
The reason for the concept 'Category' still there is backward compatibility. You should use the the concept 'logger' instead
thank you very much for this short and sweet tutorial. you really cleared the mess i had about log4j and showed how easy it actually is to use. keep up the good work dude.
@Kosala
thank you very much.
Good and very useful tutorial in very short.
Thanks,
Binod Suman