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
Make sure the properties file has properly set. And again, it seems like that the compiler cannot find the properties file, you can set as follow at the pom (only when you use maven project).
<build>
<sourceDirectory> src/main/java</sourceDirectory>
<testSourceDirectory> src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>resources</directory>
</resource>
</resources>
</build >
I think you should understand where the log4j jar file or the Java code looks for the log4j configuration files.
src/main/resources/log4j.properties
is Eclipse path.
Place them in a proper location so that you don't have to hard code the absolute path in code.
Read my article and sample solution for that http://askyourquestions.info/2016/03/27/how-to-see-where-the-log-is-logger-in-slf4j/
In my case, the error was the flag "additivity". If it's "false" for you root project package, then the child packages will not have an appender and you will see the "appender not found" error.
First of all: Create a log4j.properties file
# Root logger option
log4j.rootLogger=INFO, stdout
# 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
Place it in src/main/resources/
After that, use this 2 dependencies:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
It is necessary to add this final dependency to POM file:
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
Quick solution:
add code to main function:
String log4jConfPath = "/path/to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
create a file named log4j.properties at /path/to
log4j.rootLogger=INFO, 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{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
If log4j.properties
is indeed on the classpath, you are using Spring Boot to make a WAR file for deployment to an app server, you are omitting a web.xml
file in favour of Spring Boot's autoconfigure, and you are not getting any log messages whatsoever, you need to explicitly configure Log4j. Assuming you are using Log4j 1.2.x:
public class AppConfig extends SpringBootServletInitializer {
public static void main( String[] args ) {
// Launch the application
ConfigurableApplicationContext context = SpringApplication.run( AppConfig.class, args );
}
@Override
protected SpringApplicationBuilder configure( SpringApplicationBuilder application ) {
InputStream log4j = this.getClass().getClassLoader().getResourceAsStream("log4j.properties");
PropertyConfigurator.configure(log4j);
return application;
}
// Other beans as required...
}