forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ROCKETMQ-194] log appender support closes apache#101
- Loading branch information
1 parent
99ce40a
commit 37fbb7b
Showing
17 changed files
with
1,395 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.apache.rocketmq</groupId> | ||
<artifactId>rocketmq-all</artifactId> | ||
<version>4.1.0-incubating-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>rocketmq-logappender</artifactId> | ||
<packaging>jar</packaging> | ||
<name>rocketmq-logappender ${project.version}</name> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.logging.log4j</groupId> | ||
<artifactId>log4j-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-core</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>rocketmq-client</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>rocketmq-namesrv</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>rocketmq-broker</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
|
||
</project> |
93 changes: 93 additions & 0 deletions
93
logappender/src/main/java/org/apache/rocketmq/logappender/common/ProducerInstance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.rocketmq.logappender.common; | ||
|
||
import org.apache.rocketmq.client.exception.MQClientException; | ||
import org.apache.rocketmq.client.producer.DefaultMQProducer; | ||
import org.apache.rocketmq.client.producer.MQProducer; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Common Producer component | ||
*/ | ||
public class ProducerInstance { | ||
|
||
public static final String APPENDER_TYPE = "APPENDER_TYPE"; | ||
|
||
public static final String LOG4J_APPENDER = "LOG4J_APPENDER"; | ||
|
||
public static final String LOG4J2_APPENDER = "LOG4J2_APPENDER"; | ||
|
||
public static final String LOGBACK_APPENDER = "LOGBACK_APPENDER"; | ||
|
||
public static final String DEFAULT_GROUP = "rocketmq_appender"; | ||
|
||
private static ConcurrentHashMap<String, MQProducer> producerMap = new ConcurrentHashMap<String, MQProducer>(); | ||
|
||
private static String genKey(String nameServerAddress, String group) { | ||
return nameServerAddress + "_" + group; | ||
} | ||
|
||
|
||
public static MQProducer getInstance(String nameServerAddress, String group) throws MQClientException { | ||
if (group == null) { | ||
group = DEFAULT_GROUP; | ||
} | ||
|
||
String genKey = genKey(nameServerAddress, group); | ||
MQProducer p = producerMap.get(genKey); | ||
if (p != null) { | ||
return p; | ||
} | ||
|
||
DefaultMQProducer defaultMQProducer = new DefaultMQProducer(group); | ||
defaultMQProducer.setNamesrvAddr(nameServerAddress); | ||
MQProducer beforeProducer = null; | ||
//cas put producer | ||
beforeProducer = producerMap.putIfAbsent(genKey, defaultMQProducer); | ||
if (beforeProducer != null) { | ||
return beforeProducer; | ||
} | ||
defaultMQProducer.start(); | ||
return defaultMQProducer; | ||
} | ||
|
||
|
||
public static void removeAndClose(String nameServerAddress, String group) { | ||
if (group == null) { | ||
group = DEFAULT_GROUP; | ||
} | ||
String genKey = genKey(nameServerAddress, group); | ||
MQProducer producer = producerMap.remove(genKey); | ||
|
||
if (producer != null) { | ||
producer.shutdown(); | ||
} | ||
} | ||
|
||
public static void closeAll() { | ||
Set<Map.Entry<String, MQProducer>> entries = producerMap.entrySet(); | ||
for (Map.Entry<String, MQProducer> entry : entries) { | ||
producerMap.remove(entry.getKey()); | ||
entry.getValue().shutdown(); | ||
} | ||
} | ||
|
||
} |
194 changes: 194 additions & 0 deletions
194
logappender/src/main/java/org/apache/rocketmq/logappender/log4j/RocketmqLog4jAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.rocketmq.logappender.log4j; | ||
|
||
import org.apache.rocketmq.common.message.Message; | ||
import org.apache.rocketmq.logappender.common.ProducerInstance; | ||
import org.apache.log4j.AppenderSkeleton; | ||
import org.apache.log4j.helpers.LogLog; | ||
import org.apache.log4j.spi.ErrorCode; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
import org.apache.rocketmq.client.producer.MQProducer; | ||
|
||
/** | ||
* Log4j Appender Component | ||
*/ | ||
public class RocketmqLog4jAppender extends AppenderSkeleton { | ||
|
||
/** | ||
* Appended message tag define | ||
*/ | ||
private String tag; | ||
|
||
/** | ||
* Whitch topic to send log messages | ||
*/ | ||
private String topic; | ||
|
||
private boolean locationInfo; | ||
|
||
/** | ||
* Log producer send instance | ||
*/ | ||
private MQProducer producer; | ||
|
||
/** | ||
* RocketMQ nameserver address | ||
*/ | ||
private String nameServerAddress; | ||
|
||
/** | ||
* Log producer group | ||
*/ | ||
private String producerGroup; | ||
|
||
public RocketmqLog4jAppender() { | ||
} | ||
|
||
|
||
public void activateOptions() { | ||
LogLog.debug("Getting initial context."); | ||
if (!checkEntryConditions()) { | ||
return; | ||
} | ||
try { | ||
producer = ProducerInstance.getInstance(nameServerAddress, producerGroup); | ||
} catch (Exception e) { | ||
LogLog.error("activateOptions nameserver:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage()); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Info,error,warn,callback method implementation | ||
* | ||
* @param event | ||
*/ | ||
public void append(LoggingEvent event) { | ||
if (null == producer) { | ||
return; | ||
} | ||
if (locationInfo) { | ||
event.getLocationInformation(); | ||
} | ||
byte[] data = this.layout.format(event).getBytes(); | ||
try { | ||
Message msg = new Message(topic, tag, data); | ||
msg.getProperties().put(ProducerInstance.APPENDER_TYPE, ProducerInstance.LOG4J_APPENDER); | ||
|
||
//Send message and do not wait for the ack from the message broker. | ||
producer.sendOneway(msg); | ||
} catch (Exception e) { | ||
String msg = new String(data); | ||
errorHandler.error("Could not send message in RocketmqLog4jAppender [" + name + "].Message is :" + msg, e, | ||
ErrorCode.GENERIC_FAILURE); | ||
} | ||
} | ||
|
||
protected boolean checkEntryConditions() { | ||
String fail = null; | ||
|
||
if (this.topic == null) { | ||
fail = "No topic"; | ||
} else if (this.tag == null) { | ||
fail = "No tag"; | ||
} | ||
|
||
if (fail != null) { | ||
errorHandler.error(fail + " for RocketmqLog4jAppender named [" + name + "]."); | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* When system exit,this method will be called to close resources | ||
*/ | ||
public synchronized void close() { | ||
// The synchronized modifier avoids concurrent append and close operations | ||
|
||
if (this.closed) | ||
return; | ||
|
||
LogLog.debug("Closing RocketmqLog4jAppender [" + name + "]."); | ||
this.closed = true; | ||
|
||
try { | ||
ProducerInstance.removeAndClose(this.nameServerAddress, this.producerGroup); | ||
} catch (Exception e) { | ||
LogLog.error("Closing RocketmqLog4jAppender [" + name + "] nameServerAddress:" + nameServerAddress + " group:" + producerGroup + " " + e.getMessage()); | ||
} | ||
// Help garbage collection | ||
producer = null; | ||
} | ||
|
||
public boolean requiresLayout() { | ||
return true; | ||
} | ||
|
||
public String getTopic() { | ||
return topic; | ||
} | ||
|
||
|
||
public void setTopic(String topic) { | ||
this.topic = topic; | ||
} | ||
|
||
public String getTag() { | ||
return tag; | ||
} | ||
|
||
public void setTag(String tag) { | ||
this.tag = tag; | ||
} | ||
|
||
/** | ||
* Returns value of the <b>LocationInfo</b> property which | ||
* determines whether location (stack) info is sent to the remote | ||
* subscriber. | ||
*/ | ||
public boolean isLocationInfo() { | ||
return locationInfo; | ||
} | ||
|
||
/** | ||
* If true, the information sent to the remote subscriber will | ||
* include caller's location information. By default no location | ||
* information is sent to the subscriber. | ||
*/ | ||
public void setLocationInfo(boolean locationInfo) { | ||
this.locationInfo = locationInfo; | ||
} | ||
|
||
/** | ||
* Returns the message producer,Only valid after | ||
* activateOptions() method has been invoked. | ||
*/ | ||
protected MQProducer getProducer() { | ||
return producer; | ||
} | ||
|
||
public void setNameServerAddress(String nameServerAddress) { | ||
this.nameServerAddress = nameServerAddress; | ||
} | ||
|
||
public void setProducerGroup(String producerGroup) { | ||
this.producerGroup = producerGroup; | ||
} | ||
} |
Oops, something went wrong.