[java] Where should I put the log4j.properties file?

I wrote a web service project using netbeans 6.7.1 with glassfish v2.1, put log4j.properties to the root dir of project and use:

static Logger logger = Logger.getLogger(MyClass.class);

in Constructor:

PropertyConfigurator.configure("log4j.properties");

and in functions:

logger.info("...");
logger.error("...");
// ...

but, it is error info(actually, I have tried to put it almost every dir that I could realize):

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.

the example project could be get from http://www.91files.com/?N3F0QGQPWMDGPBRN0QA8

This question is related to java netbeans glassfish log4j

The answer is


You have to put it in the root directory, that corresponds to your execution context.

Example:

    MyProject
       src
           MyClass.java
           log4j.properties

If you start executing from a different project, you need to have that file in the project used for starting the execution. For example, if a different project holds some JUnit tests, it needs to have also its log4j.properties file.


I suggest using log4j.xml instead of the log4j.properties. You have more options, get assistance from your IDE and so on...


If you put log4j.properties inside src, you don't need to use the statement -

PropertyConfigurator.configure("log4j.properties");

It will be taken automatically as the properties file is in the classpath.


I don't know this is correct way.But it solved my problem. put log4j.properties file in "project folder"/config and use PropertyConfigurator.configure("config//log4j.properties");

it will works with IDE but not when run the jar file yourself. when you run the jar file by yourself just copy the log4j.properties file in to the folder that jar file is in.when the jar and property file in same directory it runs well.


For a Maven Based Project keep your log4j.properties in src/main/resources. Nothing else to do!


Try:

PropertyConfigurator.configure(getClass().getResource("/controlador/log4j.properties"));

You don't need to specify PropertyConfigurator.configure("log4j.properties"); in your Log4J class, If you have already defined the log4j.properties in your project structure.

In case of Web Dynamic Project: -

You need to save your log4j.properties under WebContent -> WEB-INF -> log4j.properties

I hope this may help you.


You can specify config file location with VM argument -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"


There are many ways to do it:

Way1: If you are trying in maven project without Using PropertyConfigurator

First: check for resources directory at scr/main

  if available,
       then: create a .properties file and add all configuration details.
  else
       then: create a directory named resources and a file with .properties     
write your configuration code/details.

follows the screenshot:

![enter image description here

Way2: If you are trying with Properties file for java/maven project Use PropertyConfigurator

Place properties file anywhere in project and give the correct path. say: src/javaLog4jProperties/log4j.properties

static{

 PropertyConfigurator.configure("src/javaLog4jProperties/log4j.properties");

}

enter image description here

Way3: If you are trying with xml on java/maven project Use DOMConfigurator

Place properties file anywhere in project and give correct path. say: src/javaLog4jProperties/log4j.xml

static{

 DOMConfigurator.configure("src/javaLog4jProperties/log4j.xml");

}

enter image description here


As already stated, log4j.properties should be in a directory included in the classpath, I want to add that in a mavenized project a good place can be src/main/resources/log4j.properties


I've spent a great deal of time to figure out why the log4j.properties file is not seen.
Then I noticed it was visible for the project only when it was in both MyProject/target/classes/ and MyProject/src/main/resources folders.
Hope it'll be useful to somebody.
PS: The project was maven-based.


I know it's a bit late to answer this question, and maybe you already found the solution, but I'm posting the solution I found (after I googled a lot) so it may help a little:

  1. Put log4j.properties under WEB-INF\classes of the project as mentioned previously in this thread.
  2. Put log4j-xx.jar under WEB-INF\lib
  3. Test if log4j was loaded: add -Dlog4j.debug @ the end of your java options of tomcat

Hope this will help.

rgds


Actually, I've just experienced this problem in a stardard Java project structure as follows:

\myproject
    \src
    \libs
    \res\log4j.properties

In Eclipse I need to add the res folder to build path, however, in Intellij, I need to mark the res folder as resouces as the linked screenshot shows: right click on the res folder and mark as resources.


My IDE is NetBeans. I put log4j.property file as shown in the pictures

Root

project's root folder

Web

web folder

WEB-INF

WEB-INF folder

To use this property file you should to write this code:

package example;

import java.io.File;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import javax.servlet.*;
public class test {

public static ServletContext context;
static Logger log = Logger.getLogger("example/test");

public test() {

        String homeDir = context.getRealPath("/");
        File propertiesFile = new File(homeDir, "WEB-INF/log4j.properties");
        PropertyConfigurator.configure(propertiesFile.toString());
        log.info("This is a test");
    }
}

You can define static ServletContext context from another JSP file. Example:

test.context = getServletContext(); 
test sample = new test(); 

Now you can use log4j.property file in your projects.


I found that Glassfish by default is looking at [Glassfish install location]\glassfish\domains[your domain]\ as the default working directory... you can drop the log4j.properties file in this location and initialize it in your code using PropertyConfigurator as previously mentioned...

Properties props = System.getProperties();
System.out.println("Current working directory is " + props.getProperty("user.dir"));
PropertyConfigurator.configure("log4j.properties");

The file should be located in the WEB-INF/classes directory. This directory structure should be packaged within the war file.


A few technically correct specific answers already provided but in general, it can be anywhere on the runtime classpath, i.e. wherever classes are sought by the JVM.

This could be the /src dir in Eclipse or the WEB-INF/classes directory in your deployed app, but it's best to be aware of the classpath concept and why the file is placed in it, don't just treat WEB-INF/classes as a "magic" directory.


Your standard project setup will have a project structure something like:

src/main/java
src/main/resources

You place log4j.properties inside the resources folder, you can create the resources folder if one does not exist


Put log4j.properties in classpath. Here is the 2 cases that will help you to identify the proper location- 1. For web application the classpath is /WEB-INF/classes.

\WEB-INF      
    classes\
        log4j.properties
  1. To test from main / unit test the classpath is source directory

    \Project\
       src\
          log4j.properties
    

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to netbeans

Can't create project on Netbeans 8.2 I'm getting favicon.ico error Cannot find java. Please use the --jdkhome switch Netbeans 8.0.2 The module has not been deployed Error starting Tomcat from NetBeans - '127.0.0.1*' is not recognized as an internal or external command Cannot start GlassFish 4.1 from within Netbeans 8.0.1 Service area javac: invalid target release: 1.8 connecting MySQL server to NetBeans Starting of Tomcat failed from Netbeans Display Records From MySQL Database using JTable in Java

Examples related to glassfish

Cannot start GlassFish 4.1 from within Netbeans 8.0.1 Service area How to configure Glassfish Server in Eclipse manually org.glassfish.jersey.servlet.ServletContainer ClassNotFoundException Location of GlassFish Server Logs How do I specify the JDK for a GlassFish domain? How to return a PNG image from Jersey REST service method to the browser Could not open ServletContext resource What is the difference between Tomcat, JBoss and Glassfish? Address already in use: JVM_Bind What does 'URI has an authority component' mean?

Examples related to log4j

No log4j2 configuration file found. Using default configuration: logging only errors to the console How to stop INFO messages displaying on spark console? What is log4j's default log file dumping path How to give environmental variable path for file appender in configuration file in log4j No appenders could be found for logger(log4j)? SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". in a Maven Project log4j:WARN No appenders could be found for logger (running jar file, not web app) Error: "setFile(null,false) call failed" when using log4j java.lang.ClassNotFoundException: org.apache.log4j.Level How can I create 2 separate log files with one log4j config file?