Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with log4j.

Status
Not open for further replies.

komyg

Programmer
Dec 13, 2005
68
BR
Hi, I am using Java 1.6 and log4j 1.2.15 with Easy Eclipse Server 1.2.2.2 to develop a simple test program.

I wanted to log a few things about this program and then I decided to use log4j to that, but it seems I am not being able to configure it correctly. Whenever I run my application I get this error:

Code:
log4j:ERROR Could not read configuration file [log4j.xml].
java.io.FileNotFoundException: log4j.xml (The system cannot find the specified file)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(Unknown Source)
	at java.io.FileInputStream.<init>(Unknown Source)
	at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:316)
	at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:342)
	at com.itautec.comparadorNeurotech.Program.main(Program.java:90)
log4j:ERROR Ignoring configuration file [log4j.xml].

The part of my app that is initializing log4j is:

Code:
java.io.File f = new java.io.File("log4j.xml");
		if (f.exists())
			System.out.println("OK!");
		else
			System.out.println("Not OK!");

		PropertyConfigurator.configure("log4j.xml");

		Logger log = Logger.getLogger(Program.class);

			log.info("Iniciando o Comparador Neurotech.");

And my log4j.xml is below:

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

<log4j:configuration xmlns:log4j="[URL unfurl="true"]http://jakarta.apache.org/log4j/">[/URL]

	<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss} [%-5level] %type.%method - %message%newline" />
		</layout>
  	</appender>

    <appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="c:\Temp\ComparadorNeurotech.log" />
		<param name="Append" value="true" />
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />

		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss} [%-5level] %type.%method - %message%newline" />
		</layout>
	</appender>

	<root>
        <level value="ALL" />
		<appender-ref ref="LogFileAppender" />
    </root>

</log4j:configuration>

Finally I found somewhere in the net the following test to see if my application could find the log4j.xml file or not:

Code:
java.io.File f = new java.io.File("log4j.xml");
    if (f.exists())
	System.out.println("OK!");
    else
	System.out.println("Not OK!");

I have run my application with this code and I keep getting the Not OK message.

Also I have tried to use the full path to the log4j.xml (c:\workspace\...\log4j.xml) as the argument for the PropertyConfigurator.Configure() method and the test above. I found out that this way my program actually finds the log4j.xml, but no log file is generated, nor any text is ouputted to the console even though the FileNotFoundException is not thrown.

Please help me.

Thanks a lot,
Komyg
 
Eclipse finds if there is a log4j.xml in the directory of your compiled class because JRE runs your program there as working directory.
I guess you should specify a full path like
PropertyConfigurator.configure("c:/workspace/proj1/log4j.xml");

 
I think you should be putting your log4j.xml file on the Java classpath and letting log4j pick it up automatically. I always use the log4.properties configuration choice myself and this is how I do it. I don't see why it would be different with the log4.xml approach.

I tend to put all my config type files in the root folder of my classes which is on the classpath anyway (otherwise none of the classes would be picked up).

Tim
 
I saw a recommendation on the web to put the log4j.xml inside my src folder, and that is what I did. As far as I know the log4j.xml was indeed included on the classpath, right?

Is there a way to manually include the lo4j.xml inside the classpath with eclipse, something like right clicking the log4j.xml and selecting an "Include in classpath" option?

Also I have tried to put the full path of the log4.xml as an argument to the PropertyConfigurator.Configure() method. The problem is that even though no error would be generated (the application found the file), for some bizarre reason the log file as well as the console output would not be generated.

Perhaps I should try to use the log4j.properties instead of the log4j.xml...

Thanks,
Komyg
 
No, the src folder is not normally on the classpath. You run .class files, not .java files. The classes are generated into the build folder when you compile. However, when Eclipse builds the classes, it usually copies resource files such as .xml and .properties into the corresponding build folder anyway. And this means they will be on your classpath. Just have a look in the top folder where all your classes are and see if you can see your log4j.xml file.

Have you had a look in the log4j forum? There may be questions/replies there which might guide you. However, I don't think there is much activity in there these days.

Tim
 
That recommendation that I've read said exactly that: put your log4j.xml inside the src folder and Eclipse will copy it to the bin folder where the .class files will be generated.

I wasn't aware that there was a specific log4j forum here at tek-tips... However in my long search through google I still couldn't find anything that would solve this problem.

Thanks,
Komyg
 
Right, I've just created a minimal Eclipse project using your log4j.xml file and logging works fine!

1) Created project, copied log4j-1.2.13.jar to the lib folder and added it to libraries in Eclipse.
2) Created TestLogger.java under src/com/testlogging
3) Created log4j.xml (with your contents) under src

I made one little change to the log4j.xml to write to mylog.log rather than an absolute pathed file like yours
Code:
<param name="File" value="mylog.log" />

My java class looks like this:-
Code:
package com.testlogging;

import org.apache.log4j.Logger;

public class TestLogger {

	private static final Logger LOGGER = Logger.getLogger(TestLogger.class);

	public static void main(String[] args) {
		LOGGER.info("Hello");
	}
}

My project is set to compile automatically, so I just run the TestLogger class, and hey presto! I have a mylog.log file in my project folder containing the following line:-
Code:
2009-04-29 08:03:22 [com.testlogging.TestLogger.main(TestLogger.java:13)evel] mainype.Helloethod - Helloessage
ewline

The formatting is yours and looks like it'll need some adjusting once you get this working. Anyway, I advise you to set up a minimal project just like I've done and see if you get anything working. We'll take it from there. Good luck!

Tim
 
Some points:

- Is yours and standalone application? Did you try running outside eclipse?
- Has "easy eclipse server" different features?
- Did you check the file is actually being copied to the bin folder (if it exists)?

Cheers,
Dian
 
Hi, finally I've solved the problems!!!

First of all I created a basic app like you suggested with the code below:

Code:
package com.itautec.test;

import org.apache.log4j.Logger;

public class SimpleTest
{
	public static void main(String[] args)
	{

		Logger log = Logger.getLogger(SimpleTest.class);

		log.info("Hello World!");

	}
}

Then I've put the log4j.xml inside my src folder and compiled the project. The result was:

Code:
2009-04-29 09:41:35 INFO  [main] - SimpleTest.main -> Hello World!

As it turns out I don't have to use the PropertyConfigurator.configure("log4j.xml") method because the log4j.xml is already in the classpath (eclipse does copy it from the src folder to the bin folder where the .class files are generated).

If I do use the PropertyConfigurator, then I get that "System cannot find the specified file" error (I haven't tried to use it while suppling a full path to the log4j.xml, but I believe that it would have worked that way).

Also I have corrected the log file formatting (I had copied it from a log4net.xml file, but apparently the format patterns are different between the 2 files).

Finally I was able to solve the problem where the log file wasn't generated. I think that it happened because I used the following string as my log file path:

Code:
"C:\temp\SimpleTest.log"

When I changed it to:

Code:
"C:/temp/SimpleTest.log"

Then everything worked fine, therefore I must assume that the "\" was indeed being used as an escape character instead of just a slash, thus I should have used either "/" or "\\" as directory separator.

Anyway my corrected log4j.xml is below:

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

<log4j:configuration xmlns:log4j="[URL unfurl="true"]http://jakarta.apache.org/log4j/">[/URL]

	<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss} %-5p [%t] - %C{1}.%M -> %m%n" />
		</layout>
  	</appender>

    <appender name="LogFileAppender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="C:/temp/SimpleTest.log" />
		<param name="Append" value="true" />
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />

		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss} %-5p [%t] - %C{1}.%M -> %m%n" />
		</layout>
	</appender>

	<root>
        <level value="ALL" />
		<appender-ref ref="LogFileAppender" />
		<appender-ref ref="ConsoleAppender" />
    </root>

</log4j:configuration>

Thank you very much for all your help,
Komyg
 
Ah well spotted. Glad you got it sorted out in the end.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top