7 Logappender样例

RocketMQ日志提供log4j、log4j2和logback日志框架作为业务应用,下面是配置样例

7.1 log4j样例

按下面样例使用log4j属性配置

  1. log4j.appender.mq=org.apache.rocketmq.logappender.log4j.RocketmqLog4jAppender
  2. log4j.appender.mq.Tag=yourTag
  3. log4j.appender.mq.Topic=yourLogTopic
  4. log4j.appender.mq.ProducerGroup=yourLogGroup
  5. log4j.appender.mq.NameServerAddress=yourRocketmqNameserverAddress
  6. log4j.appender.mq.layout=org.apache.log4j.PatternLayout
  7. log4j.appender.mq.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-4r [%t] (%F:%L) %-5p - %m%n

按下面样例使用log4j xml配置来使用异步添加日志

  1. <appender name="mqAppender1"class="org.apache.rocketmq.logappender.log4j.RocketmqLog4jAppender">
  2. <param name="Tag" value="yourTag" />
  3. <param name="Topic" value="yourLogTopic" />
  4. <param name="ProducerGroup" value="yourLogGroup" />
  5. <param name="NameServerAddress" value="yourRocketmqNameserverAddress"/>
  6. <layout class="org.apache.log4j.PatternLayout">
  7. <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss}-%p %t %c - %m%n" />
  8. </layout>
  9. </appender>
  10. <appender name="mqAsyncAppender1"class="org.apache.log4j.AsyncAppender">
  11. <param name="BufferSize" value="1024" />
  12. <param name="Blocking" value="false" />
  13. <appender-ref ref="mqAppender1"/>
  14. </appender>

7.2 log4j2样例

用log4j2时,配置如下,如果想要非阻塞,只需要使用异步添加引用即可

  1. <RocketMQ name="rocketmqAppender" producerGroup="yourLogGroup" nameServerAddress="yourRocketmqNameserverAddress"
  2. topic="yourLogTopic" tag="yourTag">
  3. <PatternLayout pattern="%d [%p] hahahah %c %m%n"/>
  4. </RocketMQ>

7.3 logback样例

  1. <appender name="mqAppender1"class="org.apache.rocketmq.logappender.logback.RocketmqLogbackAppender">
  2. <tag>yourTag</tag>
  3. <topic>yourLogTopic</topic>
  4. <producerGroup>yourLogGroup</producerGroup>
  5. <nameServerAddress>yourRocketmqNameserverAddress</nameServerAddress>
  6. <layout>
  7. <pattern>%date %p %t - %m%n</pattern>
  8. </layout>
  9. </appender>
  10. <appender name="mqAsyncAppender1"class="ch.qos.logback.classic.AsyncAppender">
  11. <queueSize>1024</queueSize>
  12. <discardingThreshold>80</discardingThreshold>
  13. <maxFlushTime>2000</maxFlushTime>
  14. <neverBlock>true</neverBlock>
  15. <appender-ref ref="mqAppender1"/>
  16. </appender>