I am using netbeans, and log4j for logging , but getting file not found log4j.properties Please help to right place for log4j.properties file
Please Place your log4j.properties file into Default Package:
- Project Folder
- Source Package
- Default Package
Put log4j.jar file into Libraries folder and import correct namespace.
After that keep log4j.properties into a specific namespace and import that.
Related
I have my log4j.xml file stored in the project directory but i am getting following error:
log4j:WARN No appenders could be found for logger (p1.Employee).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more
info.
When i palced it in the bin folder Everything works fine. But i dont check in the bin folder in the code repository so i dont want to place it there.
I dont want to use DOMConfigurator.configure("log4j.xml") as well
Below is my Project Stucture.
In a normal Java Project, you can place a log4j configuration file, i.e., a log4j.properties or a log4j.xml, in a resources directory. The resources directory should be on the same level as src. Putting it here, will allow log4j to use the configuration file automatically.
Otherwise, you can set the log4j configuration file to use via a VM option by doing something like:
-Dlog4j.configuration=file:///path/to/log4j.xml
In your case, if you want to place it in the TrailProject project directory, you can do
-Dlog4j.configuration=file:///path/to/TrailProject/log4j.xml
As per the maven java project folder structure, the ideal path to put the config.xml would be in /src/main/resources directory, so for a non maven project you would put it in an equivalent /resources directory.
I am trying to set up log4j for a project of mine. I have found this tutorial, I followed each of its steps.
Of course, I have added the jar file to the Referenced Libraries. The following picture shows that the path to the log.out file is in the PATH variable. This is how my PATH variable looks like (just a snippet):
The following picture shows that the path to the log4j jar file and the log4j.properties file is in the CLASSPATH variable. This is my CLASSPATH:
log4j.properties file (the only thing I changed is the log4j.appender.FILE.File in comparison to the .properties example from the tutorial previously mentioned):
This is how my project hierarchy looks like:
I am testing it with the following class:
package test;
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
public class Log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(Log4jExample.class.getName());
public static void main(String[] args) throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
I keep receiving the following exceptions and nothing is being logged to the file specified:
When trying to follow the link specified in the WARN message, I have found the following answer:
This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration.
How come the log4j.properties cannot be found? How can this be solved?
NOTE: I have seen that there are many posts regarding these particular WARN messages, however, I have neither a maven project, nor a dynamic web project. This is a plain Java project and I am simply trying to test log4j. I have Windows 7 and use Eclipse Luna.
UPDATE: It seems that if I move my log4j.properties file to the src folder, everything goes fine. How could I modify things to work from the current structure of files?
UPDATE #2: The answer I marked as accepted below answers my question from UPDATE. However, I have also found this post that proposes an useful solution to this problem with the location.
By default, Log4j searches for a configuration file (log4j.xml or log4j.properties) at the root of your classpath. Putting it in the src folder automatically does that.
If you do not wish to have it there, you can tell Log4j where to look for it through the system property log4j.configuration. Launching your app then becomes : java -Dlog4j.configuration=file:"D:\..." -jar myapp.jar. In Eclipse, the system property is configured in "Run Configurations > Arguments > VM arguments".
By default, Log4j searches for a configuration file (log4j.xml or log4j.properties) at the root of your classpath. Putting it in the src folder automatically does that.
I'm running an application locally and trying to get it to output logs to a file I created. the log4j.properties file looks like this:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=C:\\Users\\jkm144\\workspace\\LdapPurge\\log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
It's a Maven project. It's defined in my pom.xml and is listed in my Maven dependencies.
I've imported three classes:
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.FileAppender;
and the code I'm using to define the logger is:
static Logger log = Logger.getLogger(LdapPurge.class.getName());
Any time I run into a log, it tells me that "log4j:WARN No appenders could be found for logger"
I'm not sure exactly what I'm doing wrong. This is my first time using log4j.
Your screenshot suggests this is a Maven project, so put the log4j.properties in src/main/resources. This is the Maven-standard path for resource files that need to end up on the classpath at runtime, and anything you put in there will be packaged into your application's JAR file along with the classes that were compiled from the sources in src/main/java.
You should place the log4j.properties under the
WEB-INF/classes
directory of your web-applications. Log4j will find the properties file and initialize itself. This is easy to do and it works.
Manaully Adding log4j.properties:
you can add it any where you want, when you run your project configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file. Click OK
i would like to turn of my debug statements at runtime in log4j2. According to the documentation we can do this. I kept my log4j.xml file in default package and then made jar out of it. Since I cannot modify jar i specified xml file using Dlog4j.configuration=/path/to/log4j.xml. However,this is not working. I Tried everything using file:// uri to all the combination, still it is not picking the xml.
The system property to specify the config file location is different from log4j-1.x.
In log4j2, the property is "log4j.configurationFile".
So if the config is in a jar file you would use:
-Dlog4j.configurationFile=jar:file:///C:/path/to/jarfile.jar!/path/to/log4j2.xml
(I assume you have the monitorInterval set in your configuration as documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticReconfiguration)
Following must be added to configuration file:
<Configuration monitorInterval="60" >
I'm working on making my Java app easy to deploy to other computers and am writing an ant script to do so, that is going fine.
I'm having trouble loading resources that are listed in the classpath named in the manifest file of the jar.
Folder structure looks like this:
/MyProgram.jar
/lib/<dependencies>
/config/configuration.xml
I can not for the life of me access the configuration.xml file using the ClassLoader. It, along with all the dependencies are listed explicitly in the Class-Path entry to the manifest file.
I've tried many variants of the following:
this.xml = Thread.currentThread().getContextClassLoader()
.getResourceAsStream(xmlName);
this.xml = this.getClass().getResourceAsStream(xmlName);
With xmlName as a string of all the following values:
"config/configuration.xml"
"configuration.xml"
"config.configuration.xml"
Related to this, I also have a log4j.properties file in the config directory. How do I get log4j to pick it up? Other references say it just needs to be in the classpath, and it too is explicitly named in the jar's manifest file. Can someone point me in the right direction?
Update:
Here are the actual entries from Class-Path:
Class-Path: <snip dependencies> config/configuration.xml config/log4j.properties
Classpath entries should either be directories or jar files, not individual files. Try changing your classpath to point to the config directory instead of the individual config files.
this.xml = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("config.xml");
Better yet would be to just include your config directory in MyProgram.jar. This would prevent you from needing to add it specifically to the classpath.
this.xml = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/config/configuration.xml");
As far as I know, log4j.xml should be at root of your classpath..
and also, you can read your configuration file with below code script. and config directory should be at your classpath.
this.xml = this.getClass().getResourceAsStream("/config/configuration.xml");
You can use the log4j.configuration system property when you startup your application:
java -Dlog4j.configuration=config/log4j.properties MyApp
See http://logging.apache.org/log4j/1.2/manual.html under "Default Initialization Procedure".
Regarding the other configuration files not being picked up, what does your Manifest.mf file looks like? Are you using something like
Class-Path: config/configuration.xml lib/yourLibOne.jar lib/blah.jar
in your Manifest.mf file?
Regarding log4j : If you want it to use a different config file from the default, you can use
org.apache.log4j.PropertyConfigurator.configure(...)
Variants of this static method accept URL, Properties or file name.
There is also
org.apache.log4j.xml.DOMConfigurator
for the XML files.