Versions of libraries
soft version: spring-rabbit 1.7.1.RELEASE
log4j2 : 2.8.2
slf4j : 1.7.7
log4j2 config
<appenders>
<console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console>
<!--<Kafka name="Kafka" topic="log-test">-->
<!--<PatternLayout pattern="%date %message"/>-->
<!--<Property name="bootstrap.servers">192.168.3.166:9090,192.168.3.166:9091,192.168.3.166:9092</Property>-->
<!--</Kafka>-->
<RabbitMQ name="rabbitmq_logdata_tojson"
host="${sys:rabbitmq_host}"
port="${sys:rabbitmq_port}"
user="${sys:rabbitmq_user}"
password="${sys:rabbitmq_password}"
virtualHost="/"
exchange="${sys:rabbitmq_exchange}"
exchangeType="${sys:rabbitmq_exchangeType}"
declareExchange="true"
durable="true"
autoDelete="false"
applicationId="${sys:rabbitmq_qname_4logdata.json}"
routingKeyPattern="${sys:rabbitmq_qname_4logdata.json}"
contentType="text/plain"
contentEncoding="${sys:log4j2_charset}"
generateId="false"
deliveryMode="NON_PERSISTENT"
charset="${sys:log4j2_charset}"
senderPoolSize="1" maxSenderRetries="5">
</RabbitMQ>
<!--flume appender的配置,此处采用Avro类型 -->
<!--<Flume name="flume_logdata_in" compress="false" type="Avro" >-->
<!--<Agent host="192.168.2.111" port="4444"/>-->
<!--</Flume>-->
</appenders>
<loggers>
<logger name="${sys:rabbitmq_qname_4logdata.json}" level="info">
<appender-ref ref="rabbitmq_logdata_tojson"/>
</logger>
<!--<logger name="${sys:rabbitmq_qname_4logdata}" level="info">-->
<!--<appender-ref ref="rabbitmq_logdata_tobyte"/>-->
<!--</logger>-->
<root level="debug">
<appender-ref ref="console"/>
<!--<appender-ref ref="Kafka"/>-->
</root>
</loggers>
Java code
private static final Logger writeQueueLog= LoggerFactory.getLogger(System.getProperty("rabbitmq_qname_4logdata.json"));
writeQueueLog.info(jsonObject.toString());
System.properties
rabbitmq_qname_4logdata.json=user.behavior.countmessage.queue
rabbitmq_qname_4logdata=user.behavior.logdatainfo.queue
When I user AMQPAppender to send message to rabbitmq, always has some other logger message.
I had debug APQPAppender source code, when method "append" has been called, all of the events are right. But the Sender is running,always has some events from other logger, at last, the message count is right.
Then, I changed append queue to String like this : private final LinkedBlockingQueue<String> eventsStrs = new LinkedBlockingQueue<String>();
, and add data in method append(),
I see all the values are right, but values from events have some wrong message.
I guess the event has been changed in other place.
I have no idea about it, can you help me?
I found this code in Log4J2...
if (LOG_EVENT_FACTORY == null) {
LOG_EVENT_FACTORY = Constants.ENABLE_THREADLOCALS
? new ReusableLogEventFactory()
: new DefaultLogEventFactory();
}
If you run with -Dlog4j2.enable.threadlocals=false
it will be thread-safe.
I will add an option to the Spring appender to send the message on the calling thread instead of handing off to another thread, which will make using the ReusableLogEventFactory
safe.
You can also explicitly set the event factory with Log4jLogEventFactory
.
See System Properties here.