springMVC项目中配置log4j.properties路径

本文介绍了如何在SpringMVC项目中不将log4j.properties打包到war包内,而是将其放在单独的配置文件夹,并在容器中设置为classpath。通过在web.xml中的配置,实现了日志配置的外部化。同时提到了log4j2的使用,包括引入依赖和日志写入数据库的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

log4j.properties不打到war包中,单独写到一个存放配置文件的文件夹中,在容器中将该文件夹放入classpath,在web.xml中可以如下配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-dao.xml,classpath:SpringMVC.xml
    </param-value>
  </context-param>


  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:db2client-cfg/log4j.properties</param-value>
  </context-param>

  <context-param>
    <param-name>log4jRefreshInterval</param-name>
    <param-value>60000</param-value>
  </context-param>

  <!-- 配置log4j.xml监听器-->
  <!--需要配置在 ContextLoaderListener之前-->
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

于是看了一下log4j2,实现过程记录一下: 
首先引入全新的log4j2 依赖jar包

 <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.10.0</version>
    </dependency>

    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-web</artifactId>
      <version>2.9.1</version>
    </dependency>
<!-- log4j2-begin -->
     <listener>
         <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>
     <filter>
         <filter-name>log4jServletFilter</filter-name>
         <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>log4jServletFilter</filter-name>
         <url-pattern>/*</url-pattern>
         <dispatcher>REQUEST</dispatcher>
         <dispatcher>FORWARD</dispatcher>
         <dispatcher>INCLUDE</dispatcher>
         <dispatcher>ERROR</dispatcher>
     </filter-mapping>
     <!-- log4j2-end -->

经典大神案例:https://blog.youkuaiyun.com/smallbabylong/article/details/83475308

将日志写入到数据库:https://blog.youkuaiyun.com/qq_23543983/article/details/80687643

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值