'How to configure log4j in a Spring Mvc application configured with Java Annotations and using a log4j.properties file
I'm working on a Maven Web application using Spring MVC 4.2.5.RELEASE
, I'm using Netbeans
IDE with GlassFishSErver 4.1
, I want to use log4j 1.2.17
so I created a log4j.properties file but it seems like my properties file it's not being found because I get the following error:
log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
I configured my Spring application using java annotations here is my configuration
@Configuration
@EnableWebMvc
@ComponentScan({"config", "controllers"})
public class ConfigMVC extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
}
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
}
here is my WebInitializer
public class WebInicializar implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ConfigMVC.class);
ctx.setServletContext(servletContext);
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setInitParameter("throwExceptionIfNoHandlerFound", "true");
}
}
and here is my controller
@Controller
public class HomeController {
private static final Logger logger = Logger.getLogger(HomeController.class);
@RequestMapping(value = "/myPage", method = RequestMethod.GET)
public String paginaSinMap(ModelMap map) {
logger.info("This is an info log entry");
logger.error("This is an error log entry");
return "myPage";
}
and here it's my log4j.properties
# LOG4J configuration
log4j.rootLogger=INFO, Appender1, Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=D:/Logs/SpringMVC.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
and I put this log4j.properties in my resources folder, here is my project structure
EDIT: I didn't mention that my project is a maven web application using spring and this is my project structure
MyWebAppProjectName
--Web Pages
--WEB-INF
--jsp
index.jsp
--resources
--js
js.js
--css
site.css
log4j.properties
--Source Packages
--config
ConfigMVC.java
WebInitializaer.java
--controllers
HomeController.java
--Test Packages
--Other Resources
--src/main/resources
--default package
--Dependencies
--Java Dependencies
--Project FIles
So I put the log4j.properties in the Other Resources/src/main/resources
and when I did this Netbeans created a default Package
with the file inside like this default package/log4j-properties
and it worked the logs starting to appear in the output and everything worked fine as expected.
I tried to create a package named Log4jResource
inside this path src/main/resources
but when I did this it stopped to work and the error showed again, so it's there a way to put this file in that folder but without having to use default package
that NetBeans creates when I put the folder in there.
I tried doing refactor rename the default package but it shows me this error Module JPA Refactoring threw java.lang.NullPointerException. Please report a bug against JPA Refactoring module and attach your var/log/messages.log.
My final project structure looks like this
MyWebAppProjectName
--Web Pages
--WEB-INF
--jsp
index.jsp
--resources
--js
js.js
--css
site.css
log4j.properties
--Source Packages
--config
ConfigMVC.java
WebInitializaer.java
--controllers
HomeController.java
--Test Packages
--Other Resources
--src/main/resources
--default package
--log4j.properties
--Dependencies
--Java Dependencies
--Project FIles
Solution 1:[1]
Try moving the log4j.properties file to ./WEB-INF/. Your Spring Resources handler is certainly ok with looking in ./WEB-INF/resources/. I don't believe log4j knows anything about spring, nor does log4j care about spring. Log4j is going to look in the classpath for a properties file unless you expicitly tell log4j to look elsewhere.
I think right now, you've only told spring where to look for things.
Solution 2:[2]
I believe it's the invalid placement of the log4j.properties file that's breaking your expectations. You could have a look at the answers presented in this stack article about log4j-properties-file-where-to-put-it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lincolnadym |
Solution 2 | Community |