如何在配置了 Java Annotations 并使用 log4j.properties 文件的 Spring Mvc 应用程序中配置 log4j
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36143287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure log4j in a Spring Mvc application configured with Java Annotations and using a log4j.properties file
提问by StackQuestion
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:
我正在使用 Maven Web 应用程序Spring MVC 4.2.5.RELEASE
,我正在使用Netbeans
IDE GlassFishSErver 4.1
,我想使用log4j 1.2.17
所以我创建了一个 log4j.properties 文件,但似乎我的属性文件没有被找到,因为我收到以下错误:
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
我使用 java 注释配置了我的 Spring 应用程序,这里是我的配置
@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
这是我的 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.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
我把这个 log4j.properties 放在我的资源文件夹中,这是我的项目结构
EDIT: I didn't mention that my project is a maven web application using spring and this is my project structure
编辑:我没有提到我的项目是一个使用 spring 的 maven web 应用程序,这是我的项目结构
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.
所以我把 log4j.properties 放在里面Other Resources/src/main/resources
,当我这样做的时候,Netbeans 创建了一个default Package
像这样里面的文件default package/log4j-properties
,它工作日志开始出现在输出中,一切都按预期工作。
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.
我试图创建一个Log4jResource
在此路径中命名的包,src/main/resources
但是当我这样做时它停止工作并且错误再次显示,因此有一种方法可以将此文件放在该文件夹中,但不必使用default package
NetBeans 在我放置文件夹时创建的文件在那里。
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.
我尝试重构重命名默认包,但它显示了这个错误 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
回答by lincolnadym
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.
尝试将 log4j.properties 文件移动到 ./WEB-INF/。您的 Spring Resources 处理程序当然可以查看 ./WEB-INF/resources/。我不相信 log4j 对 spring 一无所知,log4j 也不关心 spring。Log4j 将在类路径中查找属性文件,除非您明确告诉 log4j 查找其他地方。
I think right now, you've only told spring where to look for things.
我想现在,你只是告诉春天去哪里找东西。
回答by Filip
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
我相信这是违反您期望的 log4j.properties 文件的无效放置。您可以查看此堆栈文章中有关log4j-properties-file-where-to-put-it的答案