【问题标题】:Initialize log4j in application using spring 4.1 without web.xml使用没有 web.xml 的 spring 4.1 在应用程序中初始化 log4j
【发布时间】:2014-12-15 00:13:16
【问题描述】:

在我基于 Spring 4.1 的应用程序中,我没有使用任何 web.xml。如何在我的应用程序中初始化没有 web.xml 的 log4j?我遇到了this SO 问题,该问题建议通过 web.xml 进行所需的配置,但我不知道如何通过代码来完成。

。我已将 log4j.properties 放在 WEB-INF/lib 文件夹中,它看起来像这样:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.category.org.springframework.beans.factory=DEBUG

log4j-1.2.17.jar 也在 WEB-INF/lib 文件夹中。但是启动时会出现以下信息:

log4j:WARN 找不到记录器的附加程序 (org.springframework.web.context.support.StandardServletEnvironment)。 log4j:WARN 请正确初始化log4j系统。

请帮忙。

【问题讨论】:

  • 您是否尝试将 log4j.properties 放入 src/main/resources 目录而不是 WEB-INF/lib?

标签: spring servlet-3.0


【解决方案1】:

将 log4j 添加为依赖项并将 log4j.properties 放入 main/resources - 应该可以正常工作。

【讨论】:

    【解决方案2】:

    当您在没有web.xml 的情况下启动一个spring 应用程序时,我想您有一些实现WebApplicationInitializer 的初始化类,并且可能扩展了AbstractAnnotationConfigDispatcherServletInitializer

    如果您对由freakman 提出的类路径基础的标准 log4j.properties 不满意,您可以覆盖onStartup 方法以使用Log4jConfigurer 配置log4j - 不要忘记调用基类实现...

    【讨论】:

      【解决方案3】:

      您可以将它用作 root-context.xml 中的 bean,使用 classpath 作为相对路径。它对我有用

      <bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="sbeHome">
          <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
          <property name="targetMethod" value="initLogging" />
          <property name="arguments">
              <list>
                  <value>classpath:/conf/log4j.xml</value>
              </list>
          </property>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-06
        • 2011-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 2013-07-02
        相关资源
        最近更新 更多