文章11 | 阅读 6133 | 点赞0
楔子:今天也是平凡的一天,也要继续努力噢!
在 Windows 环境进行 RocketMQ 测试时,打印出 RocketMQLog:WAR 警告信息:
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent).
RocketMQLog:WARN Please initialize the logger system properly.
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent).
RocketMQLog:WARN Please initialize the logger system properly.
log4j:WARN No appenders could be found for logger (RocketmqRemoting).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
是什么原因呢?我们可以这样排除分析一下:
看看基础款的日志配置文件 log4j.properties,并没有发现什么问题:
log4j.rootLogger=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.mq=org.apache.rocketmq.logappender.log4j.RocketmqLog4jAppender
log4j.appender.mq.Topic=TOPIC_MEIWEI_SMS_NOTICE_TEST
log4j.appender.mq.Tag=PID_MEIWEI_SMS_RETRY_TIMEOUT
log4j.appender.mq.ProducerGroup=meiwei-producer-retry
log4j.appender.mq.NameServerAddress=127.0.0.1:9876;127.0.0.1:9877
log4j.appender.mq.layout=org.apache.log4j.PatternLayout
log4j.appender.mq.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %
注:
那就是没有加载到已经配置好的日志文件。
仔细一想,原来是自己把 Producer 程序写在了项目的 Service 模块下,这是一个纯服务层,没有 WEB-INF 来加载日志配置。这样的话,移动这个 Producer 代码到 WEB 项目下,再配置好上面的 log4j.properties 文件,用 applicationContext.xml 的 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer 来加载 log4j.properties 就解决了。
RocketMQ 的 Logappender 提供 log4j、log4j2 和 logback 日志框架作为业务应用。
按下面样例使用 log4j.properties 属性配置:
log4j.appender.mq=org.apache.rocketmq.logappender.log4j.RocketmqLog4jAppender
log4j.appender.mq.Tag=yourTag
log4j.appender.mq.Topic=yourLogTopic
log4j.appender.mq.ProducerGroup=yourLogGroup
log4j.appender.mq.NameServerAddress=yourRocketmqNameserverAddress
log4j.appender.mq.layout=org.apache.log4j.PatternLayout
log4j.appender.mq.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n
按下面样例使用 log4j.xml 配置来使用异步添加日志:
<appender name="mqAppender1" class="org.apache.rocketmq.logappender.log4j.RocketmqLog4jAppender">
<param name="Tag" value="yourTag" />
<param name="Topic" value="yourLogTopic" />
<param name="ProducerGroup" value="yourLogGroup" />
<param name="NameServerAddress" value="yourRocketmqNameserverAddress"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}-%p %t %c - %m%n" />
</layout>
</appender>
<appender name="mqAsyncAppender1" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="1024" />
<param name="Blocking" value="false" />
<appender-ref ref="mqAppender1"/>
</appender>
用 log4j2 时,配置如下,如果想要非阻塞,只需要使用异步添加引用即可:
<RocketMQ name="rocketmqAppender" producerGroup="yourLogGroup" nameServerAddress="yourRocketmqNameserverAddress"
topic="yourLogTopic" tag="yourTag">
<PatternLayout pattern="%d [%p] hahahah %c %m%n"/>
</RocketMQ>
<import resource="classpath*:dubbo/user-dubbo-provider.xml"/>
<appender name="mqAppender1"class="org.apache.rocketmq.logappender.logback.RocketmqLogbackAppender">
<tag>yourTag</tag>
<topic>yourLogTopic</topic>
<producerGroup>yourLogGroup</producerGroup>
<nameServerAddress>yourRocketmqNameserverAddress</nameServerAddress>
<layout>
<pattern>%date %p %t - %m%n</pattern>
</layout>
</appender>
<appender name="mqAsyncAppender1"class="ch.qos.logback.classic.AsyncAppender">
<queueSize>1024</queueSize>
<discardingThreshold>80</discardingThreshold>
<maxFlushTime>2000</maxFlushTime>
<neverBlock>true</neverBlock>
<appender-ref ref="mqAppender1"/>
</appender>
参考资料:
http://rocketmq.apache.org/docs/logappender-example/
http://logging.apache.org/log4j/1.2/faq.html#noconfig
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/itanping/article/details/102608818
内容来源于网络,如有侵权,请联系作者删除!