gknauth
gknauth

Reputation: 2390

How can I get log4j to use a log4j.xml file and not write to console when using Spring?

I'm helping a friend with a project. He uses Spring; I am not. I gave him a log4j.xml file I use to enable logging to a file in a logs/ subdirectory. He tried the log4j.xml but his app is ignoring it (doesn't react to its presence or absence), sending output to the console. It appears Spring is auto-configuring log4j to do this. We also tried -Ddebug.log4j=true to find discern more about log4j's initialization when Spring is involved, but that parameter had no effect.

Upvotes: 0

Views: 1862

Answers (2)

RP-
RP-

Reputation: 5837

By default, log4j doesn't make any assumptions about its configuration, it definitely expects either a configuration file or a programmatic configuration.

It scans through the classpath for log4j.properties or log4j.xml. If you prefer a different path and different name, you need pass a system variable something like -Dlog4j.configuration=relative_path_to_file.xml

Upvotes: 2

DeepInJava
DeepInJava

Reputation: 1961

Right now in our project

we use below xml file

<appender name="EvolutionWebASYNC" class="org.apache.log4j.AsyncAppender">
    <appender-ref ref="logFileOut" />
</appender>

<appender name="logFileOut" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="C:/home/wasadmin/applogs/myproject/myproject.log" />
    <param name="Append" value="true" />
    <param name="Threshold" value="ALL" />
    <param name="MaxFileSize" value="10MB" />
    <param name="MaxBackupIndex" value="5" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d %-5p - [TXID: %X{TXID}] [UID: %X{UID}] [ORGOID: %X{ORGOID}] [UA_ORGOID: %X{UA_ORGOID}] - %m%n" />
    </layout>
</appender>

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d %-5p - [TXID: %X{TXID}] [UID: %X{UID}] [ORGOID: %X{ORGOID}] [UA_ORGOID: %X{UA_ORGOID}] - %m%n" />
    </layout>
</appender>

<root>
    <level value="DEBUG" />
    <appender-ref ref="EvolutionWebASYNC" /> 
</root>

I hope it helps !!!

Upvotes: 0

Related Questions