β

处理Sprint Boot与Storm1.2.2日志实现的冲突,使用logback记录日志

Harries Blog™ 399 阅读

通过mvn dependency:tree可以看到Sprint Boot S tar ter和Storm引入的日志记录框架不相同的,如下:

Spring Boot Starter引入的日志组件是 log4j-to-slf4j

+- org.springframework.boot:spring-boot-starter:jar:2.0.4.RELEASE:compile
|  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.4.RELEASE:compile
|  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile

Storm1.2.2引入的日志组件是 log4j-slf4j-impl

+- org.apache.storm:storm-core:jar:1.2.2:compile
|  +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.10.0:compile

两个放一起就会冲突,默认两个都引入了,就会报如下错误“No matching field found: getConfiguration for class org.apache.logging.slf4j.SLF4JLoggerContext”:

java.lang.IllegalArgumentException: No matching field found: getConfiguration for class org.apache.logging.slf4j.SLF4JLoggerContext
	at clojure.lang.Reflector.getInstanceField(Reflector.java:271)
	at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:315)
	at org.apache.storm.daemon.worker$get_logger_levels.invoke(worker.clj:500)
	at org.apache.storm.daemon.worker$fn__11300$exec_fn__2470__auto__$reify__11302.run(worker.clj:759)
	at java.security.AccessController.doPrivileged(Native Method)
	at javax.security.auth.Subject.doAs(Subject.java:422)
	at org.apache.storm.daemon.worker$fn__11300$exec_fn__2470__auto____11301.invoke(worker.clj:633)
	at clojure.lang.AFn.applyToHelper(AFn.java:178)
	at clojure.lang.AFn.applyTo(AFn.java:144)
	at clojure.core$apply.invoke(core.clj:630)
	at org.apache.storm.daemon.worker$fn__11300$mk_worker__11391.doInvoke(worker.clj:605)
	at clojure.lang.RestFn.invoke(RestFn.java:512)
	at clojure.lang.Var.invoke(Var.java:409)
	at org.apache.storm.daemon.supervisor.LocalContainer.launch(LocalContainer.java:61)
	at org.apache.storm.daemon.supervisor.LocalContainerLauncher.launchContainer(LocalContainerLauncher.java:45)
	at org.apache.storm.daemon.supervisor.Slot.handleWaitingForBlobLocalization(Slot.java:387)
	at org.apache.storm.daemon.supervisor.Slot.stateMachineStep(Slot.java:275)
	at org.apache.storm.daemon.supervisor.Slot.run(Slot.java:752)
:05:35.529 [SLOT_1027] ERROR org.apache.storm.util - Halting process: ("Error on initialization")
java.lang.RuntimeException: ("Error on initialization")
	at org.apache.storm.util$exit_process_BANG_.doInvoke(util.clj:341)
	at clojure.lang.RestFn.invoke(RestFn.java:423)
	at org.apache.storm.daemon.worker$fn__11300$mk_worker__11391.doInvoke(worker.clj:605)
	at clojure.lang.RestFn.invoke(RestFn.java:512)
	at clojure.lang.Var.invoke(Var.java:409)
	at org.apache.storm.daemon.supervisor.LocalContainer.launch(LocalContainer.java:61)
	at org.apache.storm.daemon.supervisor.LocalContainerLauncher.launchContainer(LocalContainerLauncher.java:45)
	at org.apache.storm.daemon.supervisor.Slot.handleWaitingForBlobLocalization(Slot.java:387)
	at org.apache.storm.daemon.supervisor.Slot.stateMachineStep(Slot.java:275)
	at org.apache.storm.daemon.supervisor.Slot.run(Slot.java:752)

为了解决这种冲突,此时的解决方案是去掉spring boot中的默认日志组件的引入:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-to-slf4j</artifactId>
		</exclusion>
	</exclusions>
</dependency>

此时在 代码 中通过如下引入slf4j记录日志:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

appli cat ion.properties中增加如下内容:

logging.config=classpath:logback.xml

另外当然别忘了在classpath下增加logback.xml的 配置 文件了,示例内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false" scan="true" scanPeriod="30 seconds" packagingData="true">
    <!-- 设置 logger context 名称,一旦设置不可改变,默认为default -->
    <contextName>StreamProcessor</contextName>
    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <!-- 当前活动日志文件名 -->
        <file>./my_log.log</file>
        <!-- 文件滚动策略根据%d{patter}中的“patter”而定,此处为每天产生一个文件 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 归档文件名“.zip或.gz结尾”,表示归档文件自动压缩 -->
            <FileNamePattern>./my_log%d{yyyyMMdd}.log.zip</FileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>        

        <encoder>
          <pattern>%d{HH:mm:ss.SSS}[%-5level][%thread]%logger{36} - %msg%n</pattern>         
        </encoder>
    </appender>
    
    <!-- 日志级别若没显示定义,则继承最近的父logger(该logger需显示定义level,直到rootLogger)的日志级别-->
    <!-- logger的appender默认具有累加性(默认日志输出到当前logger的appender和所有祖先logger的appender中),可通过配置 “additivity”属性修改默认行为-->
    <logger name="org.apache.storm"  level="INFO" additivity="false" >
    	<appender-ref ref="STDOUT" />
    </logger>
    <logger name="org.springframework.web"  level="INFO" additivity="false" >
    	<appender-ref ref="STDOUT" />
    </logger>

    <!-- 至多只能配置一个root -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

这样就搞定了

原文

https ://blog.csdn.net/feng lib ing/article/details/82261123

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » 处理Sprint Boot与Storm1.2.2日志实现的冲突,使用logback记录日志

作者:Harries Blog™
追心中的海,逐世界的梦