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