2017-05-10 60 views
0

我想在我的Spring MVC应用程序中配置日志记录器,但不知怎的,它似乎不工作。我正在使用Log4j 1.2.17并为资源文件夹内的配置创建了一个属性文件。文件内容如下:日志文件没有在Spring MVC应用程序中创建

Root logger option 
log4j.rootLogger=DEBUG, ERROR, stdout, file 

# Redirect log messages to a log file 
log4j.appender.file=org.apache.log4j.RollingFileAppender 

#outputs to Tomcat home 
log4j.appender.file.File=/Users/vshukla/Documents/softwares/apache-tomcat-8.0.26/logs/prismweb.log 
log4j.appender.file.MaxFileSize=5MB 
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 

这是我发起的每一类记录:

private static final Logger logger = Logger.getLogger(MyController.class); 

而只是为了测试,我加入了一些简单的错误情况下我控制器的方法,但尽管给完整的路径,日志文件不会被创建。

控制器代码:

try{ 
     int i=1/0; 
     System.out.println(i); 
    } catch (Exception e) { 
     logger.error("error:", new Exception("Init method")); 
    } 

我还试图建立一个prismweb.log文件中tomcat的日志文件夹,但是当我运行,没有被记录在文件中,它是空的。我的方法或配置有什么问题?

+0

为你链接:https://www.mkyong.com/spring-mvc/spring -mvc-log4j-integration-example/ – Byeon0gam

+0

我只跟随链接。 –

回答

0

在你的pom.xml尝试排除在弹簧芯或Spring MVC的依赖关系的共享记录:

<!-- 1. exclude commons-logging --> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${spring.version}</version> 
    <exclusions> 
     <exclusion> 
     <groupId>commons-logging</groupId> 
     <artifactId>commons-logging</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
+0

是否做到了。没有运气。 –

相关问题