Solved

Configuring Log4j: log4j:WARN Please initialize the log4j system properly.

Posted on 2009-05-10
6
9,614 Views
Last Modified: 2012-05-06
Hi,

I am configuring Log4j for logging in my java application

I get an warning

log4j:WARN Please initialize the log4j system properly.

and my logs are not generated. My log4j exists at the URL passed

I have created URL with my method

public URL doConfiguration()
    {
        URL url = getClass().getResource("log4j.xml");
        System.out.println("URL is "+url);

        return url;
    }
   
Now when I run my program I get message


URL is file:/C:/Documents%20and%20Settings/user/My%20Documents/NetBeansProjects/Outlook/build/classes/outlook/log4j.xml
log4j:WARN Please initialize the log4j system properly.


Please help me in fixing my log4j conf

Please find my main class code snippet and log4j.xml in Code option below
Kind Regards
package outlook;
 

import com.moyosoft.connector.com.*;

import com.moyosoft.connector.exception.*;

import com.moyosoft.connector.ms.outlook.*;

import com.moyosoft.connector.ms.outlook.folder.*;

import com.moyosoft.samples.outlook.gui.*;
 

import com.comp.outlook.xmlLogic.*;

import com.comp.outlook.businessobjects.*;

import java.io.File;

import java.net.URL;

import org.apache.log4j.Logger;

import java.util.TimeZone;

import org.apache.log4j.PropertyConfigurator;
 
 

/**

 *

 * @author user

 */

public class Main {
 

    Logger mL = Logger.getLogger(Main.class);

    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        

        TimeZone.setDefault(TimeZone.getTimeZone("Europe/London"));

        

        File f = new File("C:\\Documents and Settings\\user\\My Documents\\NetBeansProjects\\compNewsletterMailMerge\\src\\log4j.xml");

        System.out.println("File Exists: "+f.exists());
 
 

	Main mn = new Main();

        URL url = mn.doConfiguration();

        PropertyConfigurator.configure(url);

        

        FindNewMail fnewMails = new FindNewMail();

        fnewMails.findnewMails();

        

	}
 

    public URL doConfiguration()

    {

        URL url = getClass().getResource("log4j.xml");

        System.out.println("URL is "+url);
 

        return url;

    }

    

}
 
 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
 

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

    <appender name="TEMP" class="org.apache.log4j.DailyRollingFileAppender">

           <param name="File" value="/NewsletterMailMergeLog.log"/>

           <param name="Append" value="true"/>           

           <param name="DatePattern" value="yyyy-MM-dd"/>

           <param name="Threshold" value="INFO"/>

           <layout class="org.apache.log4j.PatternLayout">

              <param name="ConversionPattern"

                     value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>

	   </layout>

	</appender>
 
 

 <!--   <appender name="ErrorLog" class="org.apache.log4j.RollingFileAppender">

           <param name="File" value="/ErrorLog.log"/>

           <param name="Append" value="true"/>

           <param name="MaxFileSize" value="20MB"/> 

           <param name="MaxBackupIndex" value="10"/>

           <param name="Threshold" value="ERROR"/>

           <layout class="org.apache.log4j.PatternLayout">

              <param name="ConversionPattern"

                     value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>

	   </layout>

	</appender>

-->

    <appender name="ErrorLog" class="org.apache.log4j.DailyRollingFileAppender">

           <param name="File" value="/NewsletterMailMergeErrorLog.log"/>

           <param name="Append" value="true"/>

           <param name="DatePattern" value="yyyy-MM-dd"/>

           <param name="Threshold" value="ERROR"/>

           <layout class="org.apache.log4j.PatternLayout">

              <param name="ConversionPattern"

                     value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>

	   </layout>

	</appender>
 

<!-- 220.227.6.10 -->

 <appender name="mail" class="org.apache.log4j.net.SMTPAppender">

  <param name="SMTPHost" value="PBI-NAMSG-04.MGDPBI.global.pvt" />

  <param name="From" value="user@pb.com" />

  <param name="To" value="user@pb.com" />

  <param name="Subject" value="[LOG] Test" />

  <param name="BufferSize" value="1" />

  <param name="threshold" value="ERROR" />

  <layout class="org.apache.log4j.PatternLayout">

   <param name="ConversionPattern"

    value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />

  </layout>

 </appender>
 
 

    <appender name="TraceLog" class="org.apache.log4j.DailyRollingFileAppender">

           <param name="File" value="/NewsletterMailMergeTraceLog.log"/>

           <param name="Append" value="true"/>

           <param name="DatePattern" value="yyyy-MM-dd"/>

           <param name="Threshold" value="TRACE"/>

           <layout class="org.apache.log4j.PatternLayout">

              <param name="ConversionPattern"

                     value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] %-5p %c %x - %m%n"/>

	   </layout>

	</appender>
 

<!--
 

<logger name="mccprocesswatcher.Main" additivity="true">

  <level value="Info"/>

  <appender-ref ref="TEMP" />

</logger>
 

<logger name="mccprocesswatcher.Main" additivity="true">

  <level value="Info"/>

  <appender-ref ref="TEMP" />

</logger>
 

<logger name="outlook.Main" additivity="true">

  <level value="Info"/>

  <appender-ref ref="TEMP" />

</logger>
 

<logger name="mccprocesswatcher.Main" additivity="true">

  <level value="Warn"/>

  <appender-ref ref="ErrorLog" />

</logger>

-->

<logger name="compnewslettermailmerge" additivity="true">

  <appender-ref ref="TraceLog" />

  <appender-ref ref="TEMP" />  

  <!--<appender-ref ref="mail" /> -->

</logger>
 

<logger name="com.comp" additivity="true">

  <appender-ref ref="TraceLog" />

  <appender-ref ref="TEMP" />

  <appender-ref ref="ErrorLog" />

</logger>
 
 

    <root>

        <priority value="TRACE" />

   

    </root>
 
 

</log4j:configuration>

Open in new window

0
Comment
Question by:tia_kamakshi
  • 4
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24347910
Put log4j.xml at the root of the app and it should be loaded automatically
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24347916
IOW you shouldn't need to use a Configurator. In any case, you should be using DOMConfigurator with log4j.xml if for some reason you need to do explicit config
0
 

Author Comment

by:tia_kamakshi
ID: 24348006
Many Thanks it works when I have kept my log4j.xml at the root folder

How can I rename log4j.xml file

May be I can do this using DOMConfigurator

When I am using

File f = new File("C:\\SMEC\\Conf\\log4j.xml");
System.out.println("Conf File Exists: "+f.exists());

URL url = getClass().getResource("C:\\SMEC\\Conf\\log4j.xml");
System.out.println("URL is "+url);

URL url = mn.doConfiguration();
DOMConfigurator.configure(url);

I get ERROR

File Exists: true
Conf File Exists: true
URL is null
Exception in thread "main" java.lang.NullPointerException
        at org.apache.log4j.xml.DOMConfigurator$2.toString(DOMConfigurator.java:693)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:793)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:696)
        at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:821)
        at outlook.Main.main(Main.java:51)
Java Result: 1


Why I am getting URL null.

I wanted to rename the log4j.xml file

As I may have to change the location of log4j.xml in near future and I have multiple java applications running on same server.

May be I am thinking when I set the classpath for all applications then my application should not confuse which log4j.xml file to use

Thanks and regards
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24348021
>>I wanted to rename the log4j.xml file

No need to do that. Just keep it in the root of each app and you'll be fine, even with multiple apps, as long as you don't configure overlapping classpaths
0
 

Author Closing Comment

by:tia_kamakshi
ID: 31579900
Many Thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24348510
:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
groovy example issue 10 90
netstat -ano | find "8000" and taskkill /f /pid 2984 3 35
tomcat not starting 6 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now