I'm a newbie to log4j. This is what I have . I have about 20 files in different packages in a STAND ALONE JAVA APPLICATION. I am trying to use and write log files.
Following is my log4j.properties file which is in my class path:
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = /ParentFolder/ChildFolder/application.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
Following is the code to initialize logging in my main method
final String LOG_FILE = "C:/eclipse_workspace/lib/log4j.properties";
Properties logProp = new Properties();
try
{
logProp.load(new FileInputStream (LOG_FILE));
PropertyConfigurator.configure(logProperties);
logger.info("Logging enabled");
}
catch(IOException e)
{
System.out.println("Logging not enabled");
}
In every java class of the application I have the following code
import org.apache.log4j.*;
private static final Logger logger = Logger.getLogger(TheActualClassName.class);
But I get the following warning messages when I run the app.
log4j:WARN No appenders could be found for logger (com.xxx.myApp.MainProgram.MyFileName). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
What am I doing wrong?? The log file "application.log" is not being generated
Default Initialization under Tomcat x, 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.
println() over Log4j, of course for testing purposes, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but the most programmer will agree that they would prefer to use Log4j over println statements, even for test programs if it's easy to ...
May need the following line:
# Set root logger level to INFO and appender to R.
log4j.rootLogger=INFO, R
The root logger is always available and does not have a name.
Since the version 1.2.7, log4j (with the LogManager
class) looks for log4j.xml
in the classpath first. If the log4j.xml
not exists, then log4j (with the LogManager
class) looks for log4j.properties
in the classpath.
If you are going to use a file named log4j.properties
, and it's on your application's classpath, there is no need to even call PropertyConfiguration
or DOMConfigurator
- log4j will do this automatically when it is first initialized (when you first load a logger).
The error message seems to indicate that your configuration is not being loaded.
Add the VM argument -Dlog4j.debug
to your application to have log4j spit out a whole bunch of information when it starts up, which includes which files it tries to load and what values it finds in the configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With