I have a package called Resources in which I have placed my log4j.properties file. When I run my code, I get the following error:
log4j:WARN No appenders could be found for logger.
log4j:WARN Please initialize the log4j system properly.
How should I update the classpath to include Resource folder ?
Solution tried:
I added the log4j.properties file directly under 'source' and it worked.
Here is the contents of properties file:
#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.File=C:\\Users\\kagarwal\\Desktop\\Application.log
log4j.appender.dest1.Append=false
Right click on the folder, select Build Path then Use as a Source Folder.
Just remember that when you will deploy you will need to create a directory for resources and add it to class path.
You should also consider to use maven for your java project, it may seem overkill initially but it will pay off in the long run.
I have a maven project and in class-path (resources folder according to maven standard layout) I have several log4j.xml files, like this:
-resources
-log4j.xml
-folder1
-log4j.xml
-folder2
-log4j.xml
How I can read log4j.xml located at root? I think the following code doesn't guarantee that I will get log4j.xml from root:
Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.xml")
Unless some other jar file is in the classpath of the current thread(s classloader, before yours, and also contains a log4j.xml file at the root, your code will do what you expect it to do.
To get the other one you would use "folder1/log4j.xml" as path.
I have a Java Project that was added to the Java Build Path of a Web Project.
On the first Java project I added the Log4J JAR file to the Java Build Path and since this project was added to the Java Build Project of the Web Project, the JAR file was automatically added to the Web Project Java Build Path also.
On the Web Project I have a Web Service that instantiates a class of the Java Project. That class has a simple Logger, and it works correctly.
Now, I am trying to create a properties file named log4j.properties to configure the Logger, Appender and LayoutPattern.
Whenever I try to call a method of the instantiaded class I get this error on the console:
log4j:ERROR Could not read configuration file [log4j.properties].
What am I doing wrong?
Here's the log4j properties file:
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Sorry but it was a false alarm...
Like I said, the project where the class that instantiates the logger resides is added as a dependency o a main project where the web services are defined.
As a result that project is published on a JAR file and with the suposed solution I mentioned:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
I get a path like:
C:/project_path.jar/log4j.properties.
Obviously the propertied files still isn't found...
Sory... Still working on a solution
If using log4j 2.x, you need to have a file called log4j2.xml.
log4j.properties won't do it.
Place your log4j.properties file in your classes directory if using unpacked WAR, else place it in the src folder (root folder for your java classes).
The only way I found to solve this was to put the log4j.properties file in the src root folder.
Then on the class that instantiates the logger the following line:
PropertyConfigurator.configure("log4j.properties")
... had to be changed to:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
And finally the file was found.
Thanks for the insight Pål
Ok, sometimes the obvious answer is the one you least expect.
As it turned out I simply needed to remove the PropertyConfigurator.configure(xxx) line and place the log4j.properties file on the src folder of the dependency project.
Thanks
I'm trying to permit debug logging per a particular class using Log4j, and I've got the following:
log4j.rootLogger=stdout, daily
log4j.logger.com.mycompany.myapplication.mymodule=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.stdout.threshold=warn
log4j.appender.daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.daily.layout=org.apache.log4j.PatternLayout
log4j.appender.daily.layout.ConversionPattern=%d{h:mm:ssa} %5p (%F:%L) - %m%n
log4j.appender.daily.File=/some/file/path/stuff
log4j.appender.daily.DatePattern=MMdd'.log'
log4j.appender.daily.threshold=info
If this is the WEB-INF/classes/log4j.properties file as part of tomcat should debug messages from mycompany.myapplication.mymodule be seen or do the entries later in the file override it? (or am I changing the logging level per class completely wrong?)
Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?
You tell your class to log at level DEBUG but tell the appenders to ignore anything below WARN and INFO, so you won't see the log messages.
As for the order in which the log4j.properties will be discovered:
WEB-INF/classes
Any JAR in WEB-INF/lib
common/classes (in the tomcat directory)
Any JAR that you put into common/endorsed
Any JAR that you put into common/lib
shared/classes
Any JAR that you put into shared/lib
The discovery will stop with the first file found.
For your first question, I would say that log4j.appender.daily.threshold=info is a problem. I might not pick any DEBUG message.
Normally, we don't give a threshold to an appender, levels are configured for loggers.
I suggest you use the log4j.xml instead of the log4j.properties.
In addition to the useful validation, it adds some interesting features or default values.
I don't recall exactly which though...
Yes, you seem to be configuring your individual loggers correctly.
No, they will not be seen because you are declaring thresholds on both of your appenders which are above DEBUG:
log4j.appender.stdout.threshold=warn
log4j.appender.daily.threshold=info
As for "Secondly, if a log4j.properties file is included in a jar file, do my settings in Tomcat override those?", I'm not sure what you mean by this. log4j attempts to load a log4j.xml or log4j.properties from the classpath, and will use whichever it finds first. IIRC, entries in the shared lib folder of Tomcat supercede the classpath of each application. If you have a log4j configuration in both a jar in WEB-INF/lib and in a plaintext file in WEB-INF/classes, then you are essentially flipping a coin at runtime as to which will be used.
I think you have to give mymodule an appender, as in
log4j.logger.com.mycompany.myapplication.mymodule=DEBUG, Daily
log4j.additivity.com.mycompany.myapplication.mymodule=false
Also, if you set the additivity to false, you make sure messages get logged only
by the appender you specify.
The 'root' of a jar is normally not included in the CLASSPATH, so a log4j.properties within a jar should be invisible to the JVM.
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
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:
Put log4j.properties under WEB-INF\classes of the project as mentioned previously in this thread.
Put log4j-xx.jar under WEB-INF\lib
Test if log4j was loaded: add -Dlog4j.debug # the end of your java options of tomcat
Hope this will help.
rgds
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
You can specify config file location with VM argument -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"
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...
For a Maven Based Project keep your log4j.properties in src/main/resources. Nothing else to do!
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.
Try:
PropertyConfigurator.configure(getClass().getResource("/controlador/log4j.properties"));
The file should be located in the WEB-INF/classes directory.
This directory structure should be packaged within the war file.
My IDE is NetBeans. I put log4j.property file as shown in the pictures
Root
Web
WEB-INF
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.
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.
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 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");
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
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.
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
To test from main / unit test the classpath is source directory
\Project\
src\
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:
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");
}
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");
}
For me, it worked when I put the file inside the resources folder.
Also, it was a war file for my project. My recommendation is to ensure that the name of the file is log4j.properties, as my project didn't recognize "log4j2.properties"
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.
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.
Open spark-shell
Then type System.getenv("SPARK_CONF_DIR")
That will print where your log4j.properties should go.