views:

396

answers:

1

There is some auditing in my application. Every audit event has it's own log4j category with name that is prefixed by same string.

EG: general auditing category name: com.company.audit

web login category name: com.company.audit.web.login
create something: com.company.audit.api.create etc..

some of the categories should be logged by default, but some not =>

<category name="com.compamy.audit" additivity="true"> <priority value="INFO"/> </category>

<category name="com.company.audit.web.login" additivity="true"> <priority value="DEBUG"/> </category>

As default is INFO second category shouldn't be logged. At least I thought it but it doesn't work. Any help how to override logging level on 'sub-category'.

NOTE: I don't have much possibility to change a naming scheme as a whole

ADDED:

here is configuration of appender:

<appender class="org.apache.log4j.RollingFileAppender" name="Company_AUDIT">

<param name="File" value="${jboss.server.log.dir}/company_audit.log"/>

<param value="10000KB" name="MaxFileSize"/>

<param value="10" name="MaxBackupIndex"/>

<layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="foo"/> </layout>

</appender>

+1  A: 

Your configuration means: for category com.company.audit, log all messages of level INFO or higher. For com.company.audit.web.login, log all messages of level DEBUG or higher. So all the messages of the second category are logged, which is what you asked for.

What you really want is to select whether the second category is displayed or not. I can think of two approaches to do that:

  1. in your code, use the convention that all messages in com.company.audit will be logged at level INFO, and all messages in com.company.audit.web.login will be logged at level DEBUG. In your configuration, you only need to set the level of the main category:

    • DEBUG: display all messages
    • INFO: don't display "optional" messages (those of the subcategory)
  2. In the configuration, put com.company.audit.web.login to level OFF when you want to disable its messages. When you want to activate it, just comment the line (it will inherit the configuration from the main category).

I prefer the second option, because you can keep using the level for message gravity (what it is originally meant for).

Olivier
I thought it should work exactly as you wrote. But it looks like log4j match both categories a it does logging for both of them = if com.company.audit is INFO it logs once and then it logs also as com.company.audit.web.login (if it's not OFF)
Supowski
I tried 2. and it works. thx for help
Supowski