Spring集成Ignite Log4j2日志

2018-04-26  本文已影响239人  GuangHui

在项目中,是使用Spring集成ignite的方式进行嵌入式启动.那么Spring如何集成Ignite Log4j2日志呢,请见下面介绍:

1. Spring集成Log4j2

首先来看Spring是如何集成Log4j2日志的.

引入对应的maven依赖:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>2.7</version>
        </dependency>

再在classpath目录下添加log4j2.xml就可以了,不需要像log4j一样添加web监听器 .

log4j2.xml例子如下,关于log4j2配置文件的具体配置和读取优先级可以参考log4j2官网配置参考

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="org.springframework.beans.factory" level="DEBUG"/>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

这样配置之后,在项目启动后,log4j2自然就会启动.

注意log4j2中获取log的方法与log4j不同,需要从LogManager中获取

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

2. Spring集成Ignite Log4j2日志

集成方式,同样很简单.如果项目中使用maven进行依赖管理,那么需要添加如下的依赖:

<dependency>
  <groupId>org.apache.ignite</groupId>
  <artifactId>ignite-log4j2</artifactId>
  <version>${ignite.version}</version>
</dependency>

${ignite.version}替换为实际使用的Ignite版本。

要使用Log4j2进行日志记录,需要配置IgniteConfiguration的gridLogger属性,如下所示:
XML:

<bean class="org.apache.ignite.configuration.IgniteConfiguration">
  <property name="gridLogger">
    <bean class="org.apache.ignite.logger.log4j2.Log4J2Logger">
      <constructor-arg type="java.lang.String" value="log4j2.xml"/>
    </bean>
  </property>
  <!-- Other Ignite configurations -->
  ...
</bean>

在上面的配置中,log4j2.xml的路径要么是绝对路径,要么是相对路径,相对路径可以相对于META-INF,也可以相对于IGNITE_HOME。

只需要进行如上简单的配置,还是原来使用log4j2的方式,即可兼容正常Ignite log4j2日志.

日志示例如下:

|2018-04-26 11:17:52.410|main|8081|settlement-service|com.crt.settlement.service.business.dailyclearing.points.impl.PointsDailyClearingServiceImpl||INFO||###+-###|##+-##|##+-##||解锁+++++++++++++++++++++++++++++++++
|2018-04-26 11:17:58.415|main|8081|settlement-service|com.crt.settlement.service.business.dailyclearing.points.impl.PointsDailyClearingServiceImpl||INFO||###+-###|##+-##|##+-##||=================flabo========111111111
|2018-04-26 11:18:01.607|grid-timeout-worker-#23%myBatisObjectGrid%|8081|settlement-service|org.apache.ignite.internal.IgniteKernal%myBatisObjectGrid||INFO||###+-###|##+-##|##+-##||
Metrics for local node (to disable set 'metricsLogFrequency' to 0)
    ^-- Node [id=56ea94bb, name=myBatisObjectGrid, uptime=01:04:00.279]
    ^-- H/N/C [hosts=1, nodes=1, CPUs=4]
    ^-- CPU [cur=0%, avg=0.55%, GC=0%]
    ^-- PageMemory [pages=6359]
    ^-- Heap [used=579MB, free=67.68%, comm=812MB]
    ^-- Non heap [used=120MB, free=-1%, comm=122MB]
    ^-- Outbound messages queue [size=0]
    ^-- Public thread pool [active=0, idle=0, qSize=0]
    ^-- System thread pool [active=0, idle=6, qSize=0]
|2018-04-26 11:18:01.607|grid-timeout-worker-#23%myBatisObjectGrid%|8081|settlement-service|org.apache.ignite.internal.IgniteKernal%myBatisObjectGrid||INFO||###+-###|##+-##|##+-##||FreeList [name=myBatisObjectGrid, buckets=256, dataPages=1, reusePages=0]

3. 参考文章

[1] springMVC集成log4j2

上一篇下一篇

猜你喜欢

热点阅读