《Apache RocketMQ使用者指南》之日誌追加訊息示例
阿新 • • 發佈:2018-12-22
日誌追加型日誌示例
原文連結 譯者:小村長
RocketMQ logappender提供log4j appender,log4j2 appender和logback appender供業務使用,下面是配置示例.
log4j
當使用log4j屬性配置檔案時,config如下.
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:
<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
當使用log4j2時,config為這個。如果你想要noneblock,只需要為ref配置一個asyncAppender.
<RocketMQ name="rocketmqAppender" producerGroup="yourLogGroup" nameServerAddress="yourRocketmqNameserverAddress" topic="yourLogTopic" tag="yourTag"> <PatternLayout pattern="%d [%p] hahahah %c %m%n"/> </RocketMQ>
logback
在使用logback時,還需要一個asyncAppender.
<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>