1212import java .util .logging .Handler ;
1313import java .util .logging .Level ;
1414import java .util .logging .LogRecord ;
15-
1615import javax .ws .rs .client .Client ;
1716import javax .ws .rs .client .ClientBuilder ;
1817import javax .ws .rs .client .Entity ;
1918import javax .ws .rs .client .Invocation ;
2019import javax .ws .rs .client .WebTarget ;
2120import javax .ws .rs .core .Response ;
22-
2321import weblogic .diagnostics .logging .LogVariablesImpl ;
2422import weblogic .diagnostics .query .QueryException ;
2523import weblogic .i18n .logging .Severities ;
@@ -41,8 +39,8 @@ public class LogExportHandler extends Handler {
4139 private boolean enabled = true ;
4240
4341 private String httpHostPort = "http://" + elasticSearchHost + ":" + elasticSearchPort ;
44- private String singleURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/?pretty" ;
45- private String bulkURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/_bulk?pretty" ;
42+ private String singleURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/?pretty" ;
43+ private String bulkURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/_bulk?pretty" ;
4644
4745 private Client httpClient = ClientBuilder .newClient ();
4846 private List <FilterConfig > filterConfigs = new ArrayList <>();
@@ -58,17 +56,20 @@ public LogExportHandler(Config config) {
5856 @ Override
5957 public void publish (LogRecord record ) {
6058 WLLogRecord wlLogRecord = (WLLogRecord ) record ;
61- if (! isLoggable (record )){
59+ if (!isLoggable (record )) {
6260 return ;
6361 }
6462 String payload = recordToPayload (wlLogRecord );
65- if (bulkSize <= 1 ) {
63+ if (bulkSize <= 1 ) {
6664 Result result = executePutOrPostOnUrl (singleURL , payload , true );
6765 if (!result .successful ) {
6866 System .out .println (
69- "<weblogic.logging.exporter.LogExportHandler> logging of " + payload + " got result " + result );
67+ "<weblogic.logging.exporter.LogExportHandler> logging of "
68+ + payload
69+ + " got result "
70+ + result );
7071 }
71- }else {
72+ } else {
7273 payloadBulkList .add (payload );
7374 if (payloadBulkList .size () >= bulkSize ) {
7475 writeOutAllRecords ();
@@ -82,32 +83,30 @@ public void flush() {
8283 }
8384
8485 @ Override
85- public void close () throws SecurityException {
86-
87- }
86+ public void close () throws SecurityException {}
8887
8988 @ Override
9089 public boolean isLoggable (LogRecord logEntry ) {
9190 final int levelValue = getLevel ().intValue ();
9291 if (logEntry .getLevel ().intValue () < levelValue || levelValue == offValue ) {
9392 return false ;
9493 }
95- for (FilterConfig oneConfig : filterConfigs ) {
94+ for (FilterConfig oneConfig : filterConfigs ) {
9695
9796 List <String > servers = oneConfig .getServers ();
98- if (servers .size () == 0 ){
99- if (oneConfig .getQuery () != null ){
100- if (applyFilter (oneConfig , (WLLogRecord )logEntry , null )) {
97+ if (servers .size () == 0 ) {
98+ if (oneConfig .getQuery () != null ) {
99+ if (applyFilter (oneConfig , (WLLogRecord ) logEntry , null )) {
101100 continue ;
102- }else {
101+ } else {
103102 return false ;
104103 }
105104 }
106- }else {
107- for (String server : servers ){
108- if (applyFilter (oneConfig , (WLLogRecord )logEntry , server )) {
105+ } else {
106+ for (String server : servers ) {
107+ if (applyFilter (oneConfig , (WLLogRecord ) logEntry , server )) {
109108 continue ;
110- }else {
109+ } else {
111110 return false ;
112111 }
113112 }
@@ -116,13 +115,13 @@ public boolean isLoggable(LogRecord logEntry) {
116115 return true ;
117116 }
118117
119-
120118 private boolean applyFilter (FilterConfig oneConfig , WLLogRecord logEntry , String serverName ) {
121119 try {
122120 if ((serverName == null ) || (serverName .equals (logEntry .getServerName ()))) {
123- return oneConfig .getQuery ().executeQuery (
124- LogVariablesImpl .getInstance ().getLogVariablesResolver (logEntry ));
125- }else {
121+ return oneConfig
122+ .getQuery ()
123+ .executeQuery (LogVariablesImpl .getInstance ().getLogVariablesResolver (logEntry ));
124+ } else {
126125 return true ;
127126 }
128127 } catch (QueryException ex ) {
@@ -140,9 +139,9 @@ private String dataAsJson(String fieldName, long data) {
140139 return "\" " + fieldName + "\" : " + data ;
141140 }
142141
143- private void writeOutAllRecords (){
142+ private void writeOutAllRecords () {
144143 StringBuilder buffer = new StringBuilder ();
145- for (String oneRecord : payloadBulkList ){
144+ for (String oneRecord : payloadBulkList ) {
146145 buffer .append (INDEX );
147146 buffer .append ("\n " );
148147 buffer .append (oneRecord );
@@ -152,18 +151,20 @@ private void writeOutAllRecords(){
152151 Result result = executePutOrPostOnUrl (bulkURL , buffer .toString (), true );
153152 if (!result .successful ) {
154153 System .out .println (
155- "<weblogic.logging.exporter.LogExportHandler> logging of " + buffer .toString () + " got result " + result );
154+ "<weblogic.logging.exporter.LogExportHandler> logging of "
155+ + buffer .toString ()
156+ + " got result "
157+ + result );
156158 }
157159 }
158160
159- private Result executePutOrPostOnUrl (
160- String url , String payload , boolean post ) {
161+ private Result executePutOrPostOnUrl (String url , String payload , boolean post ) {
161162 WebTarget target = httpClient .target (url );
162- Invocation .Builder invocationBuilder =
163- target
164- . request ()
165- . accept ( "application/ json" );
166- Response response = post ? invocationBuilder . post ( Entity . json ( payload )) : invocationBuilder .put (Entity .json (payload ));
163+ Invocation .Builder invocationBuilder = target . request (). accept ( "application/json" );
164+ Response response =
165+ post
166+ ? invocationBuilder . post ( Entity . json ( payload ))
167+ : invocationBuilder .put (Entity .json (payload ));
167168 String responseString = null ;
168169 int status = response .getStatus ();
169170 boolean successful = false ;
@@ -177,80 +178,123 @@ private Result executePutOrPostOnUrl(
177178 }
178179
179180 private String recordToPayload (WLLogRecord wlLogRecord ) {
180- return
181- "{" +
182- dataAsJson ("domainUID" , domainUID ) + "," +
183- dataAsJson ("messageID" , wlLogRecord .getId ()) + "," +
184- dataAsJson ("message" , wlLogRecord .getMessage ()) + "," +
185- dataAsJson ("timestamp" , wlLogRecord .getMillis ()) + "," +
186- dataAsJson ("serverName" , wlLogRecord .getServerName ()) + "," +
187- dataAsJson ("threadName" , wlLogRecord .getThreadName ()) + "," +
188- dataAsJson ("severity" , wlLogRecord .getSeverityString ()) + "," +
189- dataAsJson ("userId" , wlLogRecord .getUserId ()) + "," +
190- dataAsJson ("level" , wlLogRecord .getLevel ().toString ()) + "," +
191- dataAsJson ("loggerName" , wlLogRecord .getLoggerName ()) + "," +
192- dataAsJson ("formattedDate" , wlLogRecord .getFormattedDate ()) + "," +
193- dataAsJson ("subSystem" , wlLogRecord .getSubsystem ()) + "," +
194- dataAsJson ("machineName" , wlLogRecord .getMachineName ()) + "," +
195- dataAsJson ("transactionId" , wlLogRecord .getTransactionId ()) + "," +
196- dataAsJson ("diagnosticContextId" , wlLogRecord .getDiagnosticContextId ()) + "," +
197- dataAsJson ("sequenceNumber" , wlLogRecord .getSequenceNumber ()) +
198- "}" ;
181+ return "{"
182+ + dataAsJson ("domainUID" , domainUID )
183+ + ","
184+ + dataAsJson ("messageID" , wlLogRecord .getId ())
185+ + ","
186+ + dataAsJson ("message" , wlLogRecord .getMessage ())
187+ + ","
188+ + dataAsJson ("timestamp" , wlLogRecord .getMillis ())
189+ + ","
190+ + dataAsJson ("serverName" , wlLogRecord .getServerName ())
191+ + ","
192+ + dataAsJson ("threadName" , wlLogRecord .getThreadName ())
193+ + ","
194+ + dataAsJson ("severity" , wlLogRecord .getSeverityString ())
195+ + ","
196+ + dataAsJson ("userId" , wlLogRecord .getUserId ())
197+ + ","
198+ + dataAsJson ("level" , wlLogRecord .getLevel ().toString ())
199+ + ","
200+ + dataAsJson ("loggerName" , wlLogRecord .getLoggerName ())
201+ + ","
202+ + dataAsJson ("formattedDate" , wlLogRecord .getFormattedDate ())
203+ + ","
204+ + dataAsJson ("subSystem" , wlLogRecord .getSubsystem ())
205+ + ","
206+ + dataAsJson ("machineName" , wlLogRecord .getMachineName ())
207+ + ","
208+ + dataAsJson ("transactionId" , wlLogRecord .getTransactionId ())
209+ + ","
210+ + dataAsJson ("diagnosticContextId" , wlLogRecord .getDiagnosticContextId ())
211+ + ","
212+ + dataAsJson ("sequenceNumber" , wlLogRecord .getSequenceNumber ())
213+ + "}" ;
199214 }
200215
201-
202- private void initialize (Config config ){
216+ private void initialize (Config config ) {
203217
204218 elasticSearchHost = config .getHost ();
205219 elasticSearchPort = config .getPort ();
206220 enabled = config .getEnabled ();
207221 String severity = config .getSeverity ();
208- if (severity != null ){
222+ if (severity != null ) {
209223 setLevel (WLLevel .getLevel (Severities .severityStringToNum (severity )));
210224 }
211225 indexName = config .getIndexName ();
212226 bulkSize = config .getBulkSize ();
213227 filterConfigs = config .getFilterConfigs ();
214- httpHostPort = "http://" + elasticSearchHost + ":" + elasticSearchPort ;
215- singleURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/?pretty" ;
216- bulkURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/_bulk?pretty" ;
228+ httpHostPort = "http://" + elasticSearchHost + ":" + elasticSearchPort ;
229+ singleURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/?pretty" ;
230+ bulkURL = httpHostPort + "/" + indexName + "/" + DOC_TYPE + "/_bulk?pretty" ;
217231 domainUID = config .getDomainUID ();
218232 }
219233
220-
221- private void createMappings (){
234+ private void createMappings () {
222235 // create mapping for wls elasticsearch document
223- final String mappings = "{"
224- + " \" mappings\" : {"
225- + " \" " + DOC_TYPE + "\" : {"
226- + " \" properties\" : {"
227- + " \" domainUID\" : {" + "\" type\" : \" keyword\" " + "},"
228- + " \" timestamp\" : {" + "\" type\" : \" date\" " + "},"
229- + " \" sequenceNumber\" : {" + "\" type\" : \" keyword\" " + "},"
230- + " \" severity\" : {" + "\" type\" : \" keyword\" " + "},"
231- + " \" level\" : {" + "\" type\" : \" keyword\" " + "},"
232- + " \" serverName\" : {" + "\" type\" : \" keyword\" " + "},"
233- + " \" threadName\" : {" + "\" type\" : \" keyword\" " + "},"
234- + " \" userId\" : {" + "\" type\" : \" keyword\" " + "},"
235- + " \" loggerName\" : {" + "\" type\" : \" keyword\" " + "},"
236- + " \" subSystem\" : {" + "\" type\" : \" keyword\" " + "},"
237- + " \" machineName\" : {" + "\" type\" : \" keyword\" " + "},"
238- + " \" transactionId\" : {" + "\" type\" : \" keyword\" " + "},"
239- + " \" messageID\" : {" + "\" type\" : \" keyword\" " + "}"
240- + " }"
241- + " }"
242- + " }"
243- + "}" ;
236+ final String mappings =
237+ "{"
238+ + " \" mappings\" : {"
239+ + " \" "
240+ + DOC_TYPE
241+ + "\" : {"
242+ + " \" properties\" : {"
243+ + " \" domainUID\" : {"
244+ + "\" type\" : \" keyword\" "
245+ + "},"
246+ + " \" timestamp\" : {"
247+ + "\" type\" : \" date\" "
248+ + "},"
249+ + " \" sequenceNumber\" : {"
250+ + "\" type\" : \" keyword\" "
251+ + "},"
252+ + " \" severity\" : {"
253+ + "\" type\" : \" keyword\" "
254+ + "},"
255+ + " \" level\" : {"
256+ + "\" type\" : \" keyword\" "
257+ + "},"
258+ + " \" serverName\" : {"
259+ + "\" type\" : \" keyword\" "
260+ + "},"
261+ + " \" threadName\" : {"
262+ + "\" type\" : \" keyword\" "
263+ + "},"
264+ + " \" userId\" : {"
265+ + "\" type\" : \" keyword\" "
266+ + "},"
267+ + " \" loggerName\" : {"
268+ + "\" type\" : \" keyword\" "
269+ + "},"
270+ + " \" subSystem\" : {"
271+ + "\" type\" : \" keyword\" "
272+ + "},"
273+ + " \" machineName\" : {"
274+ + "\" type\" : \" keyword\" "
275+ + "},"
276+ + " \" transactionId\" : {"
277+ + "\" type\" : \" keyword\" "
278+ + "},"
279+ + " \" messageID\" : {"
280+ + "\" type\" : \" keyword\" "
281+ + "}"
282+ + " }"
283+ + " }"
284+ + " }"
285+ + "}" ;
244286
245287 Result result = executePutOrPostOnUrl (httpHostPort + "/" + indexName , mappings , false );
246288 if (!result .successful ) {
247- if (result .getStatus () == HttpURLConnection .HTTP_BAD_REQUEST ){
248- //ignore. this is the case where the index has been created in elastic search.
249- }else {
289+ if (result .getStatus () == HttpURLConnection .HTTP_BAD_REQUEST ) {
290+ // ignore. this is the case where the index has been created in elastic search.
291+ } else {
250292 System .out .println (
251- "<weblogic.logging.exporter.LogExportHandler> issue of " + mappings + " got result " + result );
293+ "<weblogic.logging.exporter.LogExportHandler> issue of "
294+ + mappings
295+ + " got result "
296+ + result );
252297 }
253298 }
254299 }
255-
256300}
0 commit comments