Log4j Tutorial : How to send log messages to different log files?

by Veera on August 6, 2009

in How To, Java

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?

{ 6 comments… read them below or add one }

Tr??ng Xuân Tính August 15, 2009 at 8:28 AM

The reason for the concept ‘Category’ still there is backward compatibility. You should use the the concept ‘logger’ instead ;)

Reply

milan September 17, 2009 at 12:45 PM

How can i redirect sysouts to a log file ? Any help will be appriciated.

Thanks in advance,
Milan.

Reply

Veera September 17, 2009 at 4:46 PM

@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

Reply

Tr??ng Xuân Tính November 13, 2009 at 11:03 PM

The reason for the concept 'Category' still there is backward compatibility. You should use the the concept 'logger' instead ;)

Reply

Kosala November 20, 2009 at 4:27 PM

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.

Reply

Veera November 20, 2009 at 5:20 PM

@Kosala

thank you very much. :)

Reply

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post:

Next post: