ProgrammingPanda
ProgrammingPanda

Reputation: 143

Log4j multiple configurations within a single web-app

I have a web-app which I am trying to work with. I have different services in my web-app. Each provides a unique function but access the same database. Each service communicates with the database, adds or updates stuff, and GUI displays the output. I am working with Spring, maven and using mybatis for SQL mapping. I am packaging everything into a war file and deploying it onto Tomcat.

I want each component of the web-app to log SQL queries that it executes. I am using log4j as the logging engine. How can I make each component use different log4j configuration? Can jdbcdslog help me store some metadata related to the queries?

Basically I want to log data into separate log files per component. And log4j picks up only the last configuration file it loads from the components. All other configurations go useless. So I am trying to find a way to separate out the log data per component.

    Web-App
       Component1 
       Component2
       Component3
       Component4

Upvotes: 1

Views: 635

Answers (1)

Juned Ahsan
Juned Ahsan

Reputation: 68715

You need to define a different log4j appender for each of your service package. Here is how you can do this in log4j.xml:

Appender1

<category name="com.test.project.Component1" additivity="true">
    <priority value="DEBUG"/>
    <appender-ref ref="LOG1"/>
</category>

Appender2

<category name="com.test.project.Component2" additivity="true">
    <priority value="TRACE"/>
    <appender-ref ref="LOG2"/>
</category>

You can use the same approach for properties file in case you are using properties file configuration for log4j.

Upvotes: 1

Related Questions