Fix for Sonar critical issues
[dmaap/messagerouter/dmaapclient.git] / src / main / java / com / att / nsa / mr / logging / MRAppender.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 /**
23  * 
24  */
25 package com.att.nsa.mr.logging;
26
27 import java.io.IOException;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import org.apache.log4j.AppenderSkeleton;
33 import org.apache.log4j.spi.LoggingEvent;
34
35 import com.att.nsa.mr.client.MRClientFactory;
36 import com.att.nsa.mr.client.MRPublisher;
37
38 /**
39  * @author author
40  *
41  */
42 public class MRAppender extends AppenderSkeleton {
43
44         private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
45
46         private MRPublisher fPublisher;
47
48         //Provided through log4j configuration
49         private String topic;
50         private String partition;
51         private String hosts;
52         private int maxBatchSize = 1;
53         private int maxAgeMs = 1000;
54         private boolean compress = false;
55
56         /**
57          * 
58          */
59         public MRAppender() {
60                 super();
61         }
62
63         /**
64          * @param isActive
65          */
66         public MRAppender(boolean isActive) {
67                 super(isActive);
68         }
69
70         /* (non-Javadoc)
71          * @see org.apache.log4j.Appender#close()
72          */
73         @Override
74         public void close() {
75                 if (!this.closed) {
76                         this.closed = true;
77                         fPublisher.close();
78                 }
79         }
80
81         /* (non-Javadoc)
82          * @see org.apache.log4j.Appender#requiresLayout()
83          */
84         @Override
85         public boolean requiresLayout() {
86                 return false;
87         }
88
89         /* (non-Javadoc)
90          * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
91          */
92         @Override
93         protected void append(LoggingEvent event) {
94                 final String message;
95                 
96                 if (this.layout == null) {
97                         message = event.getRenderedMessage();
98                 } else {
99                         message = this.layout.format(event);
100                 }
101                 
102                 try {
103                         fPublisher.send(partition, message);
104                 } catch (IOException e) {
105                     logger.error("IOException: ", e);
106                 }
107         }
108
109         public void activateOptions() {
110                 if (hosts != null && topic != null && partition != null) {
111                         fPublisher = MRClientFactory.createBatchingPublisher(hosts.split(","), topic, maxBatchSize, maxAgeMs, compress);
112                 } else {
113                         logger.error("The Hosts, Topic, and Partition parameter are required to create a MR Log4J Appender");
114                 }
115         }
116         public String getTopic() {
117                 return topic;
118         }
119
120         public void setTopic(String topic) {
121                 this.topic = topic;
122         }
123
124         public String getPartition() {
125                 return partition;
126         }
127
128         public void setPartition(String partition) {
129                 this.partition = partition;
130         }
131
132         public String getHosts() {
133                 return hosts;
134         }
135
136         public void setHosts(String hosts) {
137                 this.hosts = hosts;
138         }
139         
140         public int getMaxBatchSize() {
141                 return maxBatchSize;
142         }
143
144         public void setMaxBatchSize(int maxBatchSize) {
145                 this.maxBatchSize = maxBatchSize;
146         }
147
148         public int getMaxAgeMs() {
149                 return maxAgeMs;
150         }
151
152         public void setMaxAgeMs(int maxAgeMs) {
153                 this.maxAgeMs = maxAgeMs;
154         }       
155         
156         public boolean isCompress() {
157                 return compress;
158         }
159
160         public void setCompress(boolean compress) {
161                 this.compress = compress;
162         }
163
164 }