Add logback to logstash test code
authorHuabingZhao <zhao.huabing@zte.com.cn>
Thu, 16 Feb 2017 02:39:30 +0000 (10:39 +0800)
committerHuabingZhao <zhao.huabing@zte.com.cn>
Thu, 16 Feb 2017 02:40:09 +0000 (10:40 +0800)
Issue-Id: OCS-163
Change-Id: I5d66978bb32192f44f047b0e381a01661ac9cb91
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
logging-sdk/pom.xml
logging-sdk/src/main/java/org/openo/log/api/LogService.java
logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java
logging-sdk/src/main/java/org/openo/log/impl/Facitility.java
logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java [new file with mode: 0644]
logging-sdk/src/main/java/org/openo/log/test/logback.xml [new file with mode: 0644]

index d617ebf..c616c74 100644 (file)
 
        <dependencies>
                <dependency>
-                       <groupId>org.codehaus.jackson</groupId>
-                       <artifactId>jackson-mapper-asl</artifactId>
-                       <version>1.9.9</version>
+                       <groupId>com.fasterxml.jackson.core</groupId>
+                       <artifactId>jackson-core</artifactId>
+                       <version>2.7.3</version>
                </dependency>
+
                <dependency>
-                       <groupId>org.slf4j</groupId>
-                       <artifactId>slf4j-api</artifactId>
-                       <version>1.7.10</version>
+                       <groupId>com.fasterxml.jackson.core</groupId>
+                       <artifactId>jackson-databind</artifactId>
+                       <version>2.7.3</version>
                </dependency>
+
                <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
-                       <artifactId>jackson-databind</artifactId>
-                       <version>2.5.1</version>
+                       <artifactId>jackson-annotations</artifactId>
+                       <version>2.7.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>ch.qos.logback</groupId>
+                       <artifactId>logback-classic</artifactId>
+                       <version>1.1.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>ch.qos.logback</groupId>
+                       <artifactId>logback-core</artifactId>
+                       <version>1.1.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>ch.qos.logback</groupId>
+                       <artifactId>logback-access</artifactId>
+                       <version>1.1.3</version>
+               </dependency>
+               <dependency>
+                       <groupId>net.logstash.logback</groupId>
+                       <artifactId>logstash-logback-encoder</artifactId>
+                       <version>4.8</version>
                </dependency>
        </dependencies>
        <build>
index 737d4e2..a25e10a 100644 (file)
@@ -100,7 +100,7 @@ public class LogService {
 
   public long recordSecLog(SecLogMessage logMessage) {
     // TODO Auto-generated method stub
-    LOGGER.info("receive a insert sec log message");
+    //LOGGER.info("receive a insert sec log message");
     InsertLogHandler insertLogHandler = new InsertLogHandler();
     if (insertLogHandler.checkSecLog((SecLogMessage) logMessage)) {
 
@@ -117,7 +117,7 @@ public class LogService {
 
 
   public long recordSysLog(SysLogMessage logMessage) {
-    LOGGER.info("receive a insert sys log message");
+    //LOGGER.info("receive a insert sys log message");
     InsertLogHandler insertLogHandler = new InsertLogHandler();
     if (insertLogHandler.checkSysLog((SysLogMessage) logMessage)) {
 
index 6c389bf..5b1a6d9 100644 (file)
@@ -55,7 +55,7 @@ public class SysLogMessage extends LogMessage {
     this.setLogType(LogConst.SYSLOG_TYPE);
 
     this.logName = logName;
-    this.setLocale("logName", logName);
+    this.set("logName", logName);
 
     this.rank = rank;
     this.set("rank", rank);
@@ -64,7 +64,7 @@ public class SysLogMessage extends LogMessage {
     this.set("hostname", hostname);
 
     this.detail = detail;
-    this.setLocale("detail", detail);
+    this.set("detail", detail);
 
     this.logStartDate = logStartDate;
     this.set("logStartDate", Facitility.dateFormat(logStartDate));
index 1205ce7..82445bb 100644 (file)
@@ -25,11 +25,9 @@ import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
 
-import org.codehaus.jackson.JsonProcessingException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class Facitility {
@@ -50,7 +48,7 @@ public class Facitility {
       json = w.toString();
       w.close();
     } catch (IOException e) {
-      LOGGER.error("IOException" + e);
+      LOGGER.error("IOException", e);
     }
     return json;
   }
@@ -61,7 +59,7 @@ public class Facitility {
       Map<String, String> maps = objectMapper.readValue(json, Map.class);
       return maps;
     } catch (Exception e) {
-      LOGGER.error("IOException" + e);
+      LOGGER.error("IOException", e);
       return null;
     }
   }
@@ -106,33 +104,34 @@ public class Facitility {
   }
 
   public static String checkRequiredJsonParam(String jsonParam, String key) {
-    StringBuilder errMsg = new StringBuilder();
-    try {
-      ObjectMapper mapper = new ObjectMapper();
-      JsonNode jsonValue;
-
-
-      jsonValue = mapper.readTree(jsonParam.toString());
-      Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
-      while (elements.hasNext()) {
-        Entry<String, JsonNode> node = elements.next();
-        String childValue = node.getValue().asText();
-        if (childValue == null || "".equals(childValue)) {
-          errMsg.append(
-              "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
-          break;
-        }
-      }
-
-      return errMsg.toString();
-    } catch (JsonProcessingException e) {
-      // TODO Auto-generated catch block
-      LOGGER.error("JsonProcessingException" + e);
-      return errMsg.toString();
-    } catch (IOException e) {
-      // TODO Auto-generated catch block
-      LOGGER.error("IOException" + e);
-      return errMsg.toString();
-    }
+    return "";
+//    StringBuilder errMsg = new StringBuilder();
+//    try {
+//      ObjectMapper mapper = new ObjectMapper();
+//      JsonNode jsonValue;
+//
+//
+//      jsonValue = mapper.readTree(jsonParam.toString());
+//      Iterator<Entry<String, JsonNode>> elements = jsonValue.fields();
+//      while (elements.hasNext()) {
+//        Entry<String, JsonNode> node = elements.next();
+//        String childValue = node.getValue().asText();
+//        if (childValue == null || "".equals(childValue)) {
+//          errMsg.append(
+//              "Both Chinese and English descriptions of this field cannot be empty: " + key + "/n");
+//          break;
+//        }
+//      }
+//
+//      return errMsg.toString();
+//    } catch (JsonProcessingException e) {
+//      // TODO Auto-generated catch block
+//      LOGGER.error("JsonProcessingException" , e);
+//      return errMsg.toString();
+//    } catch (IOException e) {
+//      // TODO Auto-generated catch block
+//      LOGGER.error("IOException" , e);
+//      return errMsg.toString();
+//    }
   }
 }
diff --git a/logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java b/logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java
new file mode 100644 (file)
index 0000000..ed4993a
--- /dev/null
@@ -0,0 +1,58 @@
+/**
+ * Copyright 2017 ZTE Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openo.log.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Date;
+
+import org.openo.log.api.LogService;
+import org.openo.log.api.SysLogMessage;
+import org.slf4j.LoggerFactory;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+
+/**
+ * @author Huabing Zhao
+ *
+ */
+public class TestLogback2Logstash {
+
+  /**
+   * @param args
+   * @throws JoranException
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  public static void main(String[] args) throws JoranException, IOException, InterruptedException {
+    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
+    loggerContext.reset();
+    JoranConfigurator configurator = new JoranConfigurator();
+    InputStream configStream = TestLogback2Logstash.class.getResourceAsStream("logback.xml");
+    configurator.setContext(loggerContext);
+    configurator.doConfigure(configStream); // loads logback file
+    configStream.close();
+
+    for (int i = 0; i < 10; i++) {
+      SysLogMessage sysLogMessage = new SysLogMessage("a sys log example", "www.open-o.org",
+          LogService.SYSLOG_RANK_WARN, "logdetails", new Date(), new Date());
+      LogService.getInstance().recordSysLog(sysLogMessage);
+    }
+    loggerContext.stop();
+  }
+}
diff --git a/logging-sdk/src/main/java/org/openo/log/test/logback.xml b/logging-sdk/src/main/java/org/openo/log/test/logback.xml
new file mode 100644 (file)
index 0000000..4c41c2a
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration>
+  <appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
+      <destination>127.0.0.1:4560</destination>
+
+      <!-- encoder is required -->
+      <encoder class="net.logstash.logback.encoder.LogstashEncoder" />
+  </appender>
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+               <layout class="ch.qos.logback.classic.PatternLayout">
+                       <Pattern>
+                               %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
+                       </Pattern>
+               </layout>
+       </appender>
+
+  <root level="debug">
+      <appender-ref ref="stash" />
+      <appender-ref ref="STDOUT" />
+  </root>
+
+</configuration>
\ No newline at end of file