spring4.x整合log4j2

1.所需jar包如下:

<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.7</slf4j.version>
<log4j.version>2.7</log4j.version>
<!-- 日志文件管理包 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${slf4j.version}</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>${slf4j.version}</version>
  <scope>runtime</scope>
</dependency>
<!--用于slf4j与log4j2保持桥接 -->
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <scope>runtime</scope>
  <version>${log4j.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>${log4j.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>${log4j.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-web</artifactId>
  <version>${log4j.version}</version>
</dependency>

2.将log4j2的配置文件放置到resource目录下:

3.log4j2的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off" monitorInterval="1800">

    <properties>
        <property name="LOG_HOME">logs/sample</property>
        <property name="FILE_NAME">mylog</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="running-log"
                                 fileName="${LOG_HOME}/${FILE_NAME}.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                    pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="10 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="20"/>
        </RollingRandomAccessFile>
    </Appenders>

    <Loggers>
        <Logger name="com.cnblogs.yjmyzz.App2" level="trace"
                additivity="true">
            <AppenderRef ref="running-log"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

4.在web.xml中增加配置

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

5.使用junit测试log4j2是否配置成功:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext.xml")
//多文件可以@ContextConfiguration(locations = { "classpath:spring1.xml", "classpath:spring2.xml"
public class wxorderTest extends AbstractJUnit4SpringContextTests {
    static Logger logger = LogManager.getLogger();
    @Test
    public void saveTest() {
            logger.trace("trace message " );
            logger.debug("debug message " );
            logger.info("info message " );
            logger.warn("warn message " );
            logger.error("error message " );
            logger.fatal("fatal message " );
    }
}

junit整合spring的配置可以看我的上一篇文章,关于log4j2的配置到此结束,网上关于log4j的配置内容的介绍实在太多,在这就不不在介绍了,谢谢各位关注。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在pom.xml文件中添加log4j2的依赖: ```xml <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.14.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.14.1</version> </dependency> ``` 接着,在resources目录下创建log4j2.properties文件,并进行配置: ```properties #日志级别 log4j.rootLogger=DEBUG,stdout #输出到控制台 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%p] %m%n #输出到文件 log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=./logs/spring.log log4j.appender.file.MaxFileSize=10MB 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} [%t] [%p] %m%n ``` 最后,在spring的配置文件中添加log4j2的配置: ```xml <bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="org.apache.logging.log4j.LogManager" /> <property name="targetMethod" value="getContext" /> <property name="arguments"> <list> <value>false</value> </list> </property> </bean> ``` 这样就完成了spring集成log4j2使用.properties的配置。可以通过调整log4j.properties文件中的配置,实现不同的日志输出方式和日志级别。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值