Log4j - Separating log lines to multiple log files.

It’s generally good practise to partition the log lines of your java application into different log files based on functionality/module etc.

For eg. In a web application, we would want application log lines to be logged in to server.log, whereas information about remote user who made the requests to be logged into request.log.

This can be done by defining two appenders in log4j properties and configuring them to output to two different log files.

Here is the example log4j properties.

We have defined two appenders, fileAppender to output to server.log and requestAppender to output to requests.log. Only fileAppender is added to rootLogger, so any Logger instances created by passing java class will be logged to server.log.

Example code snippets showing how to log to server.log.

To log to requests.log the Logger instance should be created passing requestLogger as logger name.

Example code snippets showing how to log to request.log.


If you would like to embed code examples your blog like in this article, Try Doculet.

comments powered by Disqus

Facebook Comments