Discussion:
Log4j xml configuration
Mike Blake-Knox
2004-08-11 18:47:28 UTC
Permalink
I'm having difficulties configuring log4j with the DOMConfigurator. The
problem is basically that the configuration file seems to be ignored. (The
sysinternals' Filemon tools makes me believe that the configurator has
actually found and read the file but the layout that's displayed isn't what
is in the configuration file.

I'm trying to use the "trivial.java" example and have replaced the first few
lines of the trivial class with:

import org.apache.log4j.xml.DOMConfigurator;
public class Trivial {
static Logger cat = Logger.getLogger(Trivial.class.getName());

public static void main(String[] args) {

DOMConfigurator.configure("C:\\temp\\chainsaw2\\logging-log4j\\examples\\src
\\trivial\\log4jconfig.xml");
NDC.push("Client #45890");

The log4jconfig.xml file contains:

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

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

<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="Append" value="true" />
<param name="File" value="c:/temp/routetext.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="TEST%-5p %c{2} - %m\n"/>
</layout>

</appender>

<appender name="TEMP" class="org.apache.log4j.ConsoleAppender">
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="test%-5p %c{2} - %m\n"/>
</layout>


</appender>

<root>
<appender-ref ref="R" />
<appender-ref ref="TEMP" />
</root>
</log4j:configuration>

Running the program results in console output that doesn't include the
"test" text from the pattern and the c:/temp/routetext.log file isn't
created.

Any suggestions?

Thanks.

Mike Blake-Knox
email: MBlake-***@tsystsol.com
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
Ceki Gülcü
2004-08-11 19:21:25 UTC
Permalink
Post by Mike Blake-Knox
I'm having difficulties configuring log4j with the DOMConfigurator. The
problem is basically that the configuration file seems to be ignored. (The
sysinternals' Filemon tools makes me believe that the configurator has
actually found and read the file but the layout that's displayed isn't what
is in the configuration file.
I'm trying to use the "trivial.java" example and have replaced the first few
import org.apache.log4j.xml.DOMConfigurator;
public class Trivial {
static Logger cat = Logger.getLogger(Trivial.class.getName());
public static void main(String[] args) {
DOMConfigurator.configure("C:\\temp\\chainsaw2\\logging-log4j\\examples\\src
\\trivial\\log4jconfig.xml");
NDC.push("Client #45890");
The example is simple enough.
I suggest you try it again replacing configDebug="true" with debug="true"
Post by Mike Blake-Knox
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
configDebug="true">
--
Ceki Gülcü

For log4j documentation consider "The complete log4j manual"
ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp
Mike Blake-Knox
2004-08-11 20:11:15 UTC
Permalink
Thanks for your reply.
Post by Ceki Gülcü
The example is simple enough.
I suggest you try it again replacing configDebug="true" with
debug="true"
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE
log4j:configuration
SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
configDebug="true">
I made the change you suggested with no changes in symptoms.

As I had seen a deprecation warning involving DOMConfigurator, I tried
changing the test program to use the JoranConfigurator; again there were no
changes in symptoms.

The test program is now:

/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package trivial;

import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.NDC;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


/**
View the <a href="doc-files/Trivial.java">source code</a> of this a
trivial usage example. Running <code>java examples.Trivial</code>
should output something similar to:

<pre>
0 INFO [main] examples.Trivial (Client #45890) - Awake awake. Put
on thy strength.
15 DEBUG [main] examples.Trivial (Client #45890 DB) - Now king David
was old.
278 INFO [main] examples.Trivial$InnerTrivial (Client #45890) -
Entered foo.
293 INFO [main] examples.Trivial (Client #45890) - Exiting Trivial.
</pre>

<p> The increasing numbers at the beginning of each line are the
times elapsed since the start of the program. The string between
the parentheses is the nested diagnostic context.

<p>See {@link Sort} and {@link SortAlgo} for sligtly more elaborate
examples.

<p>Note thent class files for the example code is not included in
any of the distributed log4j jar files. You will have to add the
directory <code>/dir-where-you-unpacked-log4j/classes</code> to
your classpath before trying out the examples.

*/
public class Trivial {
static Logger cat = Logger.getLogger(Trivial.class.getName());


public static void main(String[] args) {
JoranConfigurator jc = new JoranConfigurator();

jc.doConfigure("C:\\temp\\chainsaw2\\logging-log4j\\examples\\src\\trivial\\
log4jconfig.xml", LogManager.getLoggerRepository());
jc.logErrors();
NDC.push("Client #45890");

cat.info("Awake awake. Put on thy strength.");
Trivial.foo();
InnerTrivial.foo();
cat.info("Exiting Trivial.");
}

static void foo() {
NDC.push("DB");
cat.debug("Now king David was old.");
NDC.pop();
}

static class InnerTrivial {
static Logger cat = Logger.getLogger(InnerTrivial.class.getName());

static void foo() {
cat.info("Entered foo.");
}
}
}

The log4jconfig.xml file now looks like:

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

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

<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="Append" value="true" />
<param name="File" value="c:/temp/routetext.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>

</appender>

<appender name="TEMP" class="org.apache.log4j.ConsoleAppender">
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>


</appender>

<root>
<appender-ref ref="R" />
<appender-ref ref="TEMP" />
</root>
</log4j:configuration>

Any ideas?

Thanks.

Mike Blake-Knox
email: MBlake-***@tsystsol.com
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
Mike Blake-Knox
2004-08-13 15:12:22 UTC
Permalink
I'm apparently still doing something wrong with what I'm told is a simple
example.

Could someone help me see what needs to be done to enable the "Trivial"
example program to use an xml configuration file?

Thanks.

Mike Blake-Knox
email: MBlake-***@tsystsol.com
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
-----Original Message-----
Sent: Wednesday, August 11, 2004 4:11 PM
To: 'Log4J Users List'
Subject: RE: Log4j xml configuration
Thanks for your reply.
Post by Ceki Gülcü
The example is simple enough.
I suggest you try it again replacing configDebug="true" with
debug="true"
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE
log4j:configuration
SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
configDebug="true">
I made the change you suggested with no changes in symptoms.
As I had seen a deprecation warning involving
DOMConfigurator, I tried changing the test program to use the
JoranConfigurator; again there were no changes in symptoms.
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package trivial;
import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.NDC;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
View the <a href="doc-files/Trivial.java">source code</a> of this a
trivial usage example. Running <code>java examples.Trivial</code>
<pre>
0 INFO [main] examples.Trivial (Client #45890) -
Awake awake. Put
on thy strength.
15 DEBUG [main] examples.Trivial (Client #45890 DB) -
Now king David
was old.
278 INFO [main] examples.Trivial$InnerTrivial (Client
#45890) - Entered foo.
293 INFO [main] examples.Trivial (Client #45890) -
Exiting Trivial.
</pre>
<p> The increasing numbers at the beginning of each line are the
times elapsed since the start of the program. The string between
the parentheses is the nested diagnostic context.
examples.
<p>Note thent class files for the example code is not included in
any of the distributed log4j jar files. You will have to add the
directory <code>/dir-where-you-unpacked-log4j/classes</code> to
your classpath before trying out the examples.
*/
public class Trivial {
static Logger cat = Logger.getLogger(Trivial.class.getName());
public static void main(String[] args) {
JoranConfigurator jc = new JoranConfigurator();
jc.doConfigure("C:\\temp\\chainsaw2\\logging-log4j\\examples\\
src\\trivial\\
log4jconfig.xml", LogManager.getLoggerRepository());
jc.logErrors();
NDC.push("Client #45890");
cat.info("Awake awake. Put on thy strength.");
Trivial.foo();
InnerTrivial.foo();
cat.info("Exiting Trivial.");
}
static void foo() {
NDC.push("DB");
cat.debug("Now king David was old.");
NDC.pop();
}
static class InnerTrivial {
static Logger cat =
Logger.getLogger(InnerTrivial.class.getName());
static void foo() {
cat.info("Entered foo.");
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
debug="true">
<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="Append" value="true" />
<param name="File" value="c:/temp/routetext.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>
</appender>
<appender name="TEMP" class="org.apache.log4j.ConsoleAppender">
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>
</appender>
<root>
<appender-ref ref="R" />
<appender-ref ref="TEMP" />
</root>
</log4j:configuration>
Any ideas?
Thanks.
Mike Blake-Knox
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
---------------------------------------------------------------------
Paul Smith
2004-08-15 21:39:54 UTC
Permalink
Mike, my email is getting flooded lately, and so I have lost the context
here.
What exactly are the symptoms/outputs of the code below? If you want, you
could zip up your example project and send it to me directly, and I could
have a peek at what it is doing.
Cheers,
Paul Smith
-----Original Message-----
Sent: Saturday, August 14, 2004 1:12 AM
To: 'Log4J Users List'
Subject: RE: Log4j xml configuration
I'm apparently still doing something wrong with what I'm told is a simple
example.
Could someone help me see what needs to be done to enable the "Trivial"
example program to use an xml configuration file?
Thanks.
Mike Blake-Knox
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
-----Original Message-----
Sent: Wednesday, August 11, 2004 4:11 PM
To: 'Log4J Users List'
Subject: RE: Log4j xml configuration
Thanks for your reply.
Post by Ceki Gülcü
The example is simple enough.
I suggest you try it again replacing configDebug="true" with
debug="true"
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE
log4j:configuration
SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
configDebug="true">
I made the change you suggested with no changes in symptoms.
As I had seen a deprecation warning involving
DOMConfigurator, I tried changing the test program to use the
JoranConfigurator; again there were no changes in symptoms.
/*
* Copyright 1999,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package trivial;
import org.apache.log4j.joran.JoranConfigurator;
import org.apache.log4j.Category;
import org.apache.log4j.NDC;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
/**
View the <a href="doc-files/Trivial.java">source code</a> of this a
trivial usage example. Running <code>java examples.Trivial</code>
<pre>
0 INFO [main] examples.Trivial (Client #45890) -
Awake awake. Put
on thy strength.
15 DEBUG [main] examples.Trivial (Client #45890 DB) -
Now king David
was old.
278 INFO [main] examples.Trivial$InnerTrivial (Client
#45890) - Entered foo.
293 INFO [main] examples.Trivial (Client #45890) -
Exiting Trivial.
</pre>
<p> The increasing numbers at the beginning of each line are the
times elapsed since the start of the program. The string between
the parentheses is the nested diagnostic context.
examples.
<p>Note thent class files for the example code is not included in
any of the distributed log4j jar files. You will have to add the
directory <code>/dir-where-you-unpacked-log4j/classes</code> to
your classpath before trying out the examples.
*/
public class Trivial {
static Logger cat = Logger.getLogger(Trivial.class.getName());
public static void main(String[] args) {
JoranConfigurator jc = new JoranConfigurator();
jc.doConfigure("C:\\temp\\chainsaw2\\logging-log4j\\examples\\
src\\trivial\\
log4jconfig.xml", LogManager.getLoggerRepository());
jc.logErrors();
NDC.push("Client #45890");
cat.info("Awake awake. Put on thy strength.");
Trivial.foo();
InnerTrivial.foo();
cat.info("Exiting Trivial.");
}
static void foo() {
NDC.push("DB");
cat.debug("Now king David was old.");
NDC.pop();
}
static class InnerTrivial {
static Logger cat =
Logger.getLogger(InnerTrivial.class.getName());
static void foo() {
cat.info("Entered foo.");
}
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'
debug="true">
<appender name="R" class="org.apache.log4j.RollingFileAppender">
<param name="MaxFileSize" value="100KB" />
<param name="Append" value="true" />
<param name="File" value="c:/temp/routetext.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>
</appender>
<appender name="TEMP" class="org.apache.log4j.ConsoleAppender">
<param name="Append" value="false" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-5p %c{2} - %m\n"/>
</layout>
</appender>
<root>
<appender-ref ref="R" />
<appender-ref ref="TEMP" />
</root>
</log4j:configuration>
Any ideas?
Thanks.
Mike Blake-Knox
TSYS Office: (706) 644-3643
cellphone: (706) 570-4641
---------------------------------------------------------------------
---------------------------------------------------------------------
Continue reading on narkive:
Loading...