logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Camer38 <rembi...@gmail.com>
Subject Re: "additivity" attribute does not work for user-defined logger but works for root logger.
Date Mon, 07 Jul 2008 08:20:55 GMT

Thanks. I like your idea with the constructor but I call also the code that I
do not have an acces and I cannot modify, so I needed to look for something
different. 


I have decided to use apache-log4j-extras-1.0.jar and use
filters(org.apache.log4j.filter.ExpressionFilter). 
In each specific class I give an unique name for the thread associated with
the tool.

Then my log4j.xml looks like:

<log4j:configuration debug="true"
xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="addQuestionSequenceOrder"
class="org.apache.log4j.RollingFileAppender">
        
        
        
        
        <layout class="org.apache.log4j.PatternLayout">
            
        </layout>
        <filter class="org.apache.log4j.filter.ExpressionFilter">
            />
        </filter>
        <filter class="org.apache.log4j.filter.DenyAllFilter"/>
    </appender>

    <appender name="taskRedirection"
class="org.apache.log4j.RollingFileAppender">
        
        
        
        
        <layout class="org.apache.log4j.PatternLayout">
            
        </layout>
        <filter class="org.apache.log4j.filter.ExpressionFilter">
            
            
            
        </filter>
        <filter class="org.apache.log4j.filter.DenyAllFilter"/>
    </appender>

    <root>
        <level value="debug"/>
        <appender-ref ref="addQuestionSequenceOrder"/>
        <appender-ref ref="taskRedirection"/>
    </root>
</log4j:configuration>


I hope it would help anybody else.
NCP instead of THREAD could be used as well, but it required additional
changes to the code.

M.

Jacob Kjome wrote:
> 
> 
> 
> Camer38 wrote:
>> I have two Java classes that used log4j. 
>> 
>> curam.tools.upgradehelper.ieg.QuestionSequenceOrderConverter and
>> curam.tools.upgradehelper.taskredirection.TaskRedirectionConverter 
>> 
>> Both classes from above uses different class: 
>> curam.tools.upgradehelper.util.SQLStatements 
>>  
>> where a logger in each of the class with defined in following pattern: 
>> private static final Logger log = Logger.getLogger(NameOfTheClass.class); 
>> 
>> Next I have defined log4j.xml. I want to have a full log file in two
>> separate files for both classes. 
>> 
>> curam.tools.upgradehelper.ieg.QuestionSequenceOrderConverter in
>> QuestionSequenceOrderConverterTool.log 
>> curam.tools.upgradehelper.taskredirection.TaskRedirectionConverter in
>> TaskRedirectionConverterTool.log 
>> 
>> I did a following:                         
>>  I have not defined root at all. 
>> 
>> My assumption was that if I run one of the class
>> curam.tools.upgradehelper.ieg.QuestionSequenceOrderConverter I would got
>> all
>> the logs from that class and all the classes used internally by
>> curam.tools.upgradehelper.ieg.QuestionSequenceOrderConverter in the
>> addQuestionSequenceOrderTool.log.
>> 
> 
> That's simply not the way Log4j works.  Logger naming is hierarchical, and
> though
> this does not have to be packaged-based, that's the traditional usage.
> 
>> 
>>  Instead I got an log4j warning: 
>> 
>> log4j:WARN No appenders could be found for logger
>> (curam.tools.upgradehelper.util.SQLStatements). 
>> 
> 
> ...because curam.tools.upgradehelper.util.SQLStatements is not part of the
> naming
> hierarchy of either of the defined loggers.  This is why you usually
> define the
> ROOT logger as a catch-all.
> 
>> 
>> What is the meaning of the attribute "additivity" for each logger? 
>> Why don't I get all the logs in one file? 
>> 
>>  
>> During my testing I have defined a root logger as well,  but it does not
>> do
>> what I want. 
>> All the expected lines are present in both files but the files are
>> populated
>> with the log from the same class.
>>     <root>
>>         <level value="info"/>
>>         <appender-ref ref="taskRedirection"/>
>>         <appender-ref ref="addQuestionSequenceOrder"/>
>>     </root>
>> 
> 
> As I said previously, using package names is the "traditional usage", but
> it's not
> the only way to name loggers.  You can use any arbitrary naming scheme. 
> Of
> course, the problem you are running into here is that you want one utility
> class's
> logging to go to one of two separate files depending on which class used
> the
> utility.  That's only possible if you design the utility class to change
> it's
> logging name each time it is used.  You'll have do add a constructor that
> takes a
> logger name, such as...
> 
> public SQLStatements(String loggerName) {...}
> 
> And then each time you use this helper class, pass in the logger name to
> use.  For
> instance...
> 
> public class QuestionSequenceOrderConverter {
> 
>     public void doSomething() {
>         SQLStatements ss = new SQLStatements(this.getClass().getName());
>         ....
>     }
> 
> }
> 
> The other way to do this is to use something like SLF4J/Logback's Markers
> to
> associate distinct loggers in a user-defined way.  Log4j 1.3 started with
> this,
> but it was abandoned.  I believe there's work in progress for something
> like this
> in the Log4j extras or companions, though i can't remember what it's
> called, offhand?
> 
>> 
>> I use log4j-1.2.15.jar
>> 
>>  Could anybody help me with defining a correct log4j.xml file?
> 
> You're looking for additivity="false", not "true".  Of course, you will
> get zero
> entries for the two specifically named loggers with additivity="false" in
> output
> to the appenders attached to the ROOT logger.  Hopefully that's not a
> problem.
> 
> 
> <logger
> name="curam.tools.upgradehelper.ieg.QuestionSequenceOrderConverter"
> additivity="false">
>         <level value="debug"/>
>         <appender-ref ref="addQuestionSequenceOrder"/>
> </logger>
> 
> <logger
> name="curam.tools.upgradehelper.taskredirection.TaskRedirectionConverter"
> additivity="false">
>         <level value="debug"/>
>         <appender-ref ref="taskRedirection"/>
> </logger>
> 
> <root>
>     <level value="warn"/>
>     <appender-ref ref="allOtherCasesAppender"/>
> </root>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/%22additivity%22-attribute-does-not-work-for-user-defined-logger-but-works-for-root-logger.-tp18259092p18312072.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message