通过mvn dependency:tree可以看到Sprint Boot Starter和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) 18: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());
application.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>
这样就搞定了