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.
Related
I have a war file that I deployed with Tomcat.
I have a Dockerfile and at the end I build a kubernetes pod.
The problem is that if my property files from my app do exist in the path: /usr/local/tomcat/webapps/myapp/WEB-INF/classes/config/ and not in path /usr/local/tomcat/webapps/myapp/WEB-INF/classes/, so the application does not start.
In config I have connection.properties and log4j2.xml.
I set another path by this method:
RUN sed -i -e 's/^shared.loader=$/shared.loader="${catalina.base}/webapps/napgui/WEB-INF/classes/config"/' /usr/local/tomcat/conf/catalina.properties
Everything is fine except log4j2.xml. The values from Kubernetes are overwritten here in the container on log4j but it seems that this path is not good for log4j because in the container logs I receive errors related to the old log4j. From what I understand, he look for the first time in WEB-INF/classes and if can't find any log4j file then it look in WEB-INF/lib and there is the jar with the old log4j, and hence the errors related to the old log4j. Why not consider the one in the new path and what should be done? Thank you
I am doing a maven application. If I place my log4j2.xml configuration file in the src/resources folder it works fine.
However, I need to store it outside the application. How can I call the location of the log4j2.xml file if it is stored, for example, on my desktop? Should I create a file in the src/resource folder that reads in my log file location and how can I do that?
The Log4j2 configuration file doesn't need to be in the classpath.
You can specify a relative or a full path with system property -Dlog4j.configurationFile=path/to/log4j2.xml.
See also the Log4j2 FAQ page.
I am getting the following error while running a struts application.
13/04/19 12:42:21 log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources).
13/04/19 12:42:21 log4j:WARN Please initialize the log4j system properly.
13/04/19 12:42:21 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
My log4j.properties file is:
log4j.threshold=ALL
log4j.rootLogger=ALL,DebugAppender,InfoAppender,RECEIPTAppender
#log4j.rootLogger=DEBUG,DebugAppender
log4j.category.DebugAppender.access=DEBUG
log4j.appender.DebugAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
#log4j.rootLogger=INFO,InfoAppender
log4j.category.InfoAppender.access=INFO
log4j.appender.InfoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
#log4j.logger.com.ctmis.hibernate=ERROR, RECEIPTAppender
log4j.category.RECEIPTAppender.access=ERROR
log4j.appender.RECEIPTAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RECEIPTAppender.Threshold=ERROR
log4j.appender.RECEIPTAppender.File=C:/APWD/ServerLogs/debug/assampwd.log
log4j.appender.RECEIPTAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RECEIPTAppender.layout.ConversionPattern=%d |[%t]| %-5p |%c |- |%m|%n
My log4j.properties file is in WEB-INF folder. I have found similar topics in stackoverflow, but didnot work out for me, so I post it. Would really appreciate if someone can help me out, Its a warning message, but causing some module not working in the application.
Thanks
Your log4j.properties file needs to be in the root of your classpath. Copy the file into the WEB-INF/classes folder inside your war file.
If you are using Maven, this should involve moving your file into the src/main/resources folder of your project before running the packaging command.
The log4j configuration file needs to be in the root of the classpath. In netbeans, when you create a web application project, the path is src\java. You can see the source packages folders with a right click on the project -> Properties -> soruces. You can place the file in src\java folder or make a new folder and add it from the project properties menu.
I'd like to put all of the .properties files that my executable jar requires in a sub-directory off of the directory from which I will run the jar file. Currently, my MANIFEST.MF looks something like this:
Manifest-Version: 1.0
Build-Jdk: 1.6.0_26
Main-Class: MyMainClass
Class-Path: . conf lib/dependency1.jar
If I keep my .properties files in the same directory as the jar file, everything works fine. If I move them to the conf sub-directory, they are not found. I've also tried specifying the paths to the .properties files directly, such as conf/log4j.properties.
Is what I'm trying to do possible?
Update: Here's an example of code in my executable jar that fails:
InputStream stream = getClass().getClassLoader().getResourceAsStream("myproperties.properties");
When myproperties.properties is in the conf sub-directory, this statement returns a null InputStream. When log4j.properties is in the conf sub-directory, it outputs its warning message about not finding its configuration:
log4j:WARN No appenders could be found for logger (MyMainClass).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Try using org.apache.log4j.PropertyConfigurator.configure(String configFilename)
This can be used to specify where your log file is located.
There is an example of that here:
http://logging.apache.org/log4j/1.2/manual.html
I have a web application, in which one of the JSPs contain:
PropertyConfigurator.configure(System.getenv("MY_HOME") + "/cfg/log4j.properties");
I double-checked that MY_HOME is setup
The Tomcat web server says:
log4j:WARN No appenders could be found for logger (com.mycompany.data.JobData).
log4j:WARN Please initialize the log4j system properly.
The same setup works fine in one of the other servers.
Any hints?
The problem is that no logs are created
Not related to your answer, but this helps too.
If it's a web application, the easiest way is to make sure that your log4j.properties is situated in WEB-INF/classes folder in your WAR file. When your application will be deployed, log4J will be configured.
The PropertyConfigurator must be called before anything in your system attempts to log to avoid this warning. You may find setting the log4j configuration on the command line more reliable.
Or you could ignore the warning. ;)
BTW: You don't want to call this method too often, ideally only once.
You can try this, its helps to me.
http://www.log4j.ru/articles/HelloWorld.html
I was able to find the solution to this problem running a Eclipse Dynamic Web Project in Apache Tomcat 6. Bascially, you need to load the log4j properties file out of your context.
Two basic steps
(1) Get the log4j.properties file into the "class directory" of the war file.
(2) Read the log4j properties file out of the current context. I found the best way to do this is to access the current thread's context and work from there.
For the first step above, alter the Eclipse build process to add an additional directory that will eventually load into the WEB-INF/classes directory in the war file. Specifically....
(1) In Eclipse, right click your project in the project explorer, select 'New'->'Folder'. You can name the folder anything, but the standard in this case is 'resources'. The new folder should appear at the root level of your project.
(2) Move the log4j.properties file into this new folder.
(3) Right click the project again, and select 'Build-Path'->'Configure Build Path'. Select the 'Sources' tab. Click the 'Add Folder' button. Browse to find your new folder you created in step (1) above. Select 'OK'.
(4) Once back to the eclipse Project Explorer view, note that the folder has now moved to the 'Java Resources' area (ie it's no longer at the root due to eclipse presentation abstraction).
(5) Clean build your project.
(6) To validate that the .properties file now exists in WEB-INF/classes in your war file, export a war file to an easy location (right click Project -> Export -> War file) and checkout the contents. Note that the log4j.properties file now appears in the WEB-INF/classes.
Now for the second step above, accessing the context to read the file. Add the following code where attempting to read the file. Note that this reads this out of the war file context, so this 'should' work as the war file moves from server to server.
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
PropertyConfigurator.configure(classLoader.getResourceAsStream("log4j.properties") );