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 com.att.nsa.mr.logging;
27 import java.io.IOException;
29 import org.apache.log4j.AppenderSkeleton;
30 import org.apache.log4j.helpers.LogLog;
31 import org.apache.log4j.spi.LoggingEvent;
33 import com.att.nsa.mr.client.MRClientFactory;
34 import com.att.nsa.mr.client.MRPublisher;
40 public class MRAppender extends AppenderSkeleton {
42 private MRPublisher fPublisher;
44 //Provided through log4j configuration
46 private String partition;
48 private int maxBatchSize = 1;
49 private int maxAgeMs = 1000;
50 private boolean compress = false;
62 public MRAppender(boolean isActive) {
67 * @see org.apache.log4j.Appender#close()
78 * @see org.apache.log4j.Appender#requiresLayout()
81 public boolean requiresLayout() {
86 * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
89 protected void append(LoggingEvent event) {
92 if (this.layout == null) {
93 message = event.getRenderedMessage();
95 message = this.layout.format(event);
99 fPublisher.send(partition, message);
100 } catch (IOException e) {
105 public void activateOptions() {
106 if (hosts != null && topic != null && partition != null) {
107 fPublisher = MRClientFactory.createBatchingPublisher(hosts.split(","), topic, maxBatchSize, maxAgeMs, compress);
109 LogLog.error("The Hosts, Topic, and Partition parameter are required to create a MR Log4J Appender");
112 public String getTopic() {
116 public void setTopic(String topic) {
120 public String getPartition() {
124 public void setPartition(String partition) {
125 this.partition = partition;
128 public String getHosts() {
132 public void setHosts(String hosts) {
136 public int getMaxBatchSize() {
140 public void setMaxBatchSize(int maxBatchSize) {
141 this.maxBatchSize = maxBatchSize;
144 public int getMaxAgeMs() {
148 public void setMaxAgeMs(int maxAgeMs) {
149 this.maxAgeMs = maxAgeMs;
152 public boolean isCompress() {
156 public void setCompress(boolean compress) {
157 this.compress = compress;