log4j external configuration files

Discussions

General J2EE: log4j external configuration files

  1. log4j external configuration files (3 messages)

    Hello again, I'm having some trouble using my configuration file. It's very simple, I'm using a .properties file rather than XML. I saved it as "log4j.properties"
    # Log4j configuration file.
    log4j.rootCategory=DEBUG, A1
    # A1 is a ConsoleAppender
    #
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-5p [%t] - %m%n
    #
    #set level
    #
    log4j.logger.com=ERROR
    I'm using NetBeans IDE 3.5.1 and I put the "log4j.properties" file in the same directory as my java class that has main (the 'demo' package - see below).
    public static void main(String args[]) {
           PropertyConfigurator.configure("log4j.properties");
                           
           logger = Logger.getLogger(com.corp.ors.demo.OrsDemo.class);
                          
           logger.info("OrsDemoLogger working");
    I keep getting this error, and I don't know what to do:
    log4j:ERROR Could not read configuration file [log4j.properties].
    java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(FileInputStream.java:106)
            at java.io.FileInputStream.<init>(FileInputStream.java:66)
            at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
            at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
            at com.corp.ors.demo.OrsDemo.main(OrisDemo.java:228)
    log4j:ERROR Ignoring configuration file [log4j.properties].
    log4j:WARN No appenders could be found for logger (com.corp.ors.demo.OrsDemo).
    log4j:WARN Please initialize the log4j system properly.
    Is my configuration file wrong? Does it need to be in a different place? Any help or references would be appreciated. Thanks!
  2. properties in correct folder?[ Go to top ]

    Is the class containing the main function in a package?
    You should place the properties file at the root of your structure.
    For example

    /
        /org
             /org/company
                   ClassContainingMain.class

    The log4j properties file should be at /.

    Also try if you can read the file using
    java.io.File f = new java.io.File("log4j.properties")
    if f.exists(){
         // OK
    }
  3. Hi Katie --

    By default, if no other configuration is given, log4j will search your classpath for a 'log4j.properties' file. In this case I think the best thing to do is remove the PropertyConfigurator.configure('...') and let log4j find the configuration file on its own.

    Hope this helps!

    Joe
  4. its an old thread .... but did not have the answere.... just thought of sharing this... I too had the same problem on my windows box ... and later found that the props file was actually named log4j.props.txt and NOT log4j.props .... On windows box, you see the file as log4j.props with a text file symbol which confuses us.