I have put log4j to my buildpath, but I get the following message when I run my application:
log4j:WARN No appenders could be found for logger (dao.hsqlmanag
Add the following as the first code:
Properties prop = new Properties();
prop.setProperty("log4j.rootLogger", "WARN");
PropertyConfigurator.configure(prop);
Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:
Run
Run Configurations
Classpath (tab)
User Entries
add jar on the right
add log4j jar file
Apply
Run
The error message should no longer appear.
I faced the same issue when I tried to run the JUnit test class.
The issue is resolved after I manually added the log4j.properties file in the src/test/resources folder.
Adding the below code to the log4j.properties file solved the issue:
# Root logger option
log4j.rootLogger=INFO, file, stdout
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
My Eclipse installation could not find log4j.properties
when running JUnit tests from Eclipse, even though the file was located at src/test/resources
.
The reason was that Eclipse (or the m2e connector) did not copy content from src/test/resources
to the expected output folder target/test-classes
- the root cause was that in the project's properties under Java Build Path -> Source tab -> Source folders on build path -> src/test/resources, somehow there was an Excluded: **
entry. I removed that excluded entry.
Alternatively, I could have manually copied src/test/resources/log4j.properties
to target/test-classes/log4j.properties
.
This Short introduction to log4j guide is a little bit old but still valid.
That guide will give you some information about how to use loggers and appenders.
Just to get you going you have two simple approaches you can take.
First one is to just add this line to your main method:
BasicConfigurator.configure();
Second approach is to add this standard log4j.properties
(taken from the above mentioned guide) file to your classpath:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
It looks like you need to add the location of your log4j.properties
file to the Classpath in Eclipse.
Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:
The error message should no longer appear.