You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by to...@jpmchase.com on 2007/11/08 23:30:27 UTC

Trouble Referencing An XML Config File

I need to send all of my Log4J output to STDERR instead of STDOUT.  As
far as I can tell, the only way to do this involves the following steps:

1. Use a log4j.xml file instead of a log4j.properties file.
2. In the log4j.xml file, specify the following under your "appender"
tag:

    <param name="Target" value="System.err"/>

I wanted to create some test code that would use an XML config, but I'm
having some issues.  My code only seems to be able to find a config file
if it's a properties file, not an XML file.

Here's what I'm running:

File Structure:

    $PROJ_HOME
    |
    |__bin/
    |  |
    |  |__tests/
    |     |
    |     |__Log4jTest.class
    | 
    |__conf/
       |
       |__log4j.properties
       |
       |__log4j.xml


Here's my log4j-specific code:

    ...
    Logger logger = Logger.getLogger("Log4jTest")
    PropertyConfigurator.configure(args[0])

    logger.debug("debug hello from main!")
    logger.info("info hello from main")
    logger.warn("warn hello from main")
    logger.error("error hello from main")
    logger.fatal("fatal hello from main")
    ...

...and here's my property files:

log4j.properties:

    log4j.rootLogger=INFO, console
    log4j.appender.console=org.apache.log4j.ConsoleAppender

    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - 
%m%n

log4j.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
      <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
        <param name="Target" value="System.out"/> 
        <layout class="org.apache.log4j.PatternLayout"> 
          <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - 
%m%n"/> 
        </layout> 
      </appender> 

      <root> 
        <priority value ="info" /> 
        <appender-ref ref="console" /> 
      </root>
 
    </log4j:configuration>

It's my understanding that both of these config files are equal. 

Here's what I get when I execute my script:

    c:\proj_root\bin\tests>java -cp . .\log4jTest 
..\..\config\log4j.properties
    0    [main] INFO  log4jTest  - info hello from main
    16   [main] WARN  log4jTest  - warn hello from main
    16   [main] ERROR log4jTest  - error hello from main
    16   [main] FATAL log4jTest  - fatal hello from main

    c:\proj_root\bin\tests>java -cp . .\log4jTest ..\..\config\log4j.xml
    FATAL - fatal hello from main

It appears to me that my script is able to read the "log4j.properties"
file, but not the "log4j.xml" file. 

Does anyone know what I need to do to use an XML configuration file with
Log4J?  What am I missing?

Thanks in advance!

Tom Purl

-----------------------------------------
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to UK legal entities.

Re: Trouble Referencing An XML Config File

Posted by to...@jpmchase.com.
Thanks for the quick response Matthew!

"Matthew Kemp" <ma...@gmail.com> wrote on 11/08/2007 04:35:21 PM:

> PropertyConfigurator only reads property files. To read an xml config
> you need to use the DOMConfigurator. Another option would allow log4j
> to statically initialize itself (your config file needs to be on the
> class path).

Thanks for this.  I couldn't find this information anywhere.

Also, I found that I don't have to use a XML config file to send all log
messages to STDERR.  Here's the config file that ended up working for
me:

    log4j.rootLogger=DEBUG, stderr

    log4j.appender.stderr=org.apache.log4j.ConsoleAppender
    log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
    log4j.appender.stderr.layout.ConversionPattern=%-4r [%t] %-5p %c %x - 
%m%n
    log4j.appender.stderr.Target=System.err
                          ^^^^^^^^^^^^^^^^^

Thanks again!

Tom Purl

-----------------------------------------
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to UK legal entities.

Re: Trouble Referencing An XML Config File

Posted by Matthew Kemp <ma...@gmail.com>.
PropertyConfigurator only reads property files. To read an xml config you
need to use the DOMConfigurator. Another option would allow log4j to
statically initialize itself (your config file needs to be on the class
path).

On Nov 8, 2007 4:30 PM, <to...@jpmchase.com> wrote:

> Here's my log4j-specific code:
>
>    ...
>    Logger logger = Logger.getLogger("Log4jTest")
>    PropertyConfigurator.configure(args[0])
>
>    logger.debug("debug hello from main!")
>    logger.info("info hello from main")
>    logger.warn("warn hello from main")
>    logger.error("error hello from main")
>    logger.fatal("fatal hello from main")
>