1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
25 package org.onap.dmaap.mr.logging;
27 import java.io.IOException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import org.apache.log4j.AppenderSkeleton;
33 import org.apache.log4j.spi.LoggingEvent;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRPublisher;
42 public class MRAppender extends AppenderSkeleton {
44 private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
46 private MRPublisher fPublisher;
48 //Provided through log4j configuration
50 private String partition;
52 private int maxBatchSize = 1;
53 private int maxAgeMs = 1000;
54 private boolean compress = false;
66 public MRAppender(boolean isActive) {
71 * @see org.apache.log4j.Appender#close()
82 * @see org.apache.log4j.Appender#requiresLayout()
85 public boolean requiresLayout() {
90 * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
93 protected void append(LoggingEvent event) {
96 if (this.layout == null) {
97 message = event.getRenderedMessage();
99 message = this.layout.format(event);
103 fPublisher.send(partition, message);
104 } catch (IOException e) {
105 logger.error("IOException: ", e);
110 public void activateOptions() {
111 if (hosts != null && topic != null && partition != null) {
112 fPublisher = MRClientFactory.createBatchingPublisher(hosts.split(","), topic, maxBatchSize, maxAgeMs, compress);
114 logger.error("The Hosts, Topic, and Partition parameter are required to create a MR Log4J Appender");
117 public String getTopic() {
121 public void setTopic(String topic) {
125 public String getPartition() {
129 public void setPartition(String partition) {
130 this.partition = partition;
133 public String getHosts() {
137 public void setHosts(String hosts) {
141 public int getMaxBatchSize() {
145 public void setMaxBatchSize(int maxBatchSize) {
146 this.maxBatchSize = maxBatchSize;
149 public int getMaxAgeMs() {
153 public void setMaxAgeMs(int maxAgeMs) {
154 this.maxAgeMs = maxAgeMs;
157 public boolean isCompress() {
161 public void setCompress(boolean compress) {
162 this.compress = compress;