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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to