【问题标题】:log file not being created in Spring MVC application未在 Spring MVC 应用程序中创建日志文件
【发布时间】:2017-10-08 22:21:21
【问题描述】:

我正在尝试在我的 Spring MVC 应用程序中配置记录器,但不知何故它似乎不起作用。我正在使用Log4j 1.2.17 并为资源文件夹中的配置创建了一个属性文件。文件内容如下:

 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"));
    }

我还尝试在 tomcat 日志文件夹中创建一个 prismweb.log 文件,但是当我运行时,文件中没有记录任何内容并且它是空的。我的方法或配置有什么问题?

【问题讨论】:

标签: java spring spring-mvc logging log4j


【解决方案1】:

在您的 pom.xml 中尝试排除您的 spring 核心或 spring mvc 依赖项中的 commons-logging:

<!-- 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>

【讨论】:

  • 做到了。没有运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2022-01-19
相关资源
最近更新 更多