Warning in Initializing log4j - java

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.

Related

Where to place log4j.xml file in a non Maven Project

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.

Cannot find log file in my project logs folder in spring mvc project

I use log4j in my spring mvc project and I create log file uing log4j.properties in the following.
# Root logger option
log4j.rootLogger= INFO, info, file, stdout, error
# Direct log messages to a log file [INFO]
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.File=.logs\\mylog.log
log4j.appender.info.Threshold=INFO
log4j.appender.info.MaxFileSize=1Kb
log4j.appender.info.MaxBackupIndex=10
log4j.appender.info.DatePattern='.'yyyy-MM-dd
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
I want it to be stored in myapp/logs folder in my web application . what part i should for the file.
here is the directory
Right now it will be saved in the current working directory of your IDE (Eclipse or STS, I guess). Its not a good idea to put the file in the project structure, because that structure wont exist, once you deployed the app anywhere.
In Spring you can define a property in youe web.xml, to make the location variable:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>mywebapp.root</param-value>
</context-param>
Then add a variable to your config:
log4j.appender.info.File=${mywebapp.root}/logs/mylog.log
If you have to store it in the application, you can also define that programmatically, by makeing use of "ServletContext.getRealPath(".") to find out where your app is deployed, and then set the path to the file.
In the normal scenario web-app checks for your log file at the root of your class path. To load a file from custom location , have a look at below posts
Change location of log4j.properties
How to use custom file instead of log4j.properties
EDIT :
From op's comment to generate your log file in the projects path , where you have placed your logs directory . you can simply specify it as ,
log4j.appender.file.File= logs/mylog.log

No appenders could be found for logger using Log4j

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

Where should I put log4j.jar?

I create a Tomcat webapp project. If I put the log4j.xx.jar & log4j.properties in the Tomcat lib, I get the log file. If I put the log4j.properties in the WEB-INF/classes, I doesn't get the log file. I try again and again. When I put the log4j.xx.jar in the WEB-INF/lib and put the log4j.properties in the WEB-INF/classes, I get it again. Why? What's the logic?
log4j.rootLogger=INFO,A1, A2
#org.apache.log4j.ConsoleAppender
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.File=D:\\Tomcat2\\logs\\monitor.log
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
Both log4j.xx.jar and log4j.properties files have to be loaded by the same classloader. Tomcat lib files are loaded by a different classloader than WEB-INF/lib files. So you have to put both files under Tomcat lib or both files under WEB-INF/lib to make it work.
Put the log4j jar file in WEB-INF/lib
Put log4j.properties anywhere you like eg tomcat/conf . Make sure it is in classpath.
And then read the log4j.properties using the classloader.
YourClass.class.getClassLoader().getResourceAsStream("relative path");
I don't like the idea of putting things to tomcat/lib folders.
Firstly because different apps on tomcat may use different version of log4j.jar. And putting in tomcat/lib will cause a version conflit for some of the applications.
Secondly its not right to put a configuration file in the lib directory. Its right place is the conf folder.

Why can't tomcat find my log4j.properties file?

My web app seems to be working fine. I decided to add some logging. When I deployed the war file, the application failed with an exception because tomcat didn't know about log4j.jar.
I added the jar to the WEB-INF/lib folder and redeployed. The exception went away. This lets me know that WEB-INF/lib is on the classpath.
However, now logs/catalina.out has the following typical error message:
log4j:WARN No appenders could be found for logger (com.this.that.Validate).
log4j:WARN Please initialize the log4j system properly.
So, I copied my log4j.properties file into the WEB-INF/lib folder.
The log4j warning still gets displayed, and nothing is getting logged as far as I can tell.
I'd appreciate some guidance on solving this. I'm chasing my tail.
Update
I believe my first issue is resolved - I no longer get the warning message. I think what was happening was that I had a unix shell with a pwd in the exploded file hierarchy and this prevented Tomcat from successfully removing the old project.
LOG4j
My log4j.properties file is
log4j.rootLogger=DEBUG, FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=prog1.log
log4j.appender.FILE.MaxFileSize=100KB
# Keep one backup file
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%p %t %c - %m%n
Resolution
I changed the ' log4j.appender.FILE.File=prog1.log' line to instead have an explicit path to the tomcat logging folder.
Thanks all, your ideas helped me solve this and get 3.18% smarter.
Copy log4j.properties into WEB-INF/classes.

Categories