From 0025ff09a7574fe2bf935e66a1c81be81e207f92 Mon Sep 17 00:00:00 2001 From: HuabingZhao Date: Thu, 16 Feb 2017 10:39:30 +0800 Subject: [PATCH] Add logback to logstash test code Issue-Id: OCS-163 Change-Id: I5d66978bb32192f44f047b0e381a01661ac9cb91 Signed-off-by: HuabingZhao --- logging-sdk/pom.xml | 38 ++++++++++--- .../main/java/org/openo/log/api/LogService.java | 4 +- .../main/java/org/openo/log/api/SysLogMessage.java | 4 +- .../main/java/org/openo/log/impl/Facitility.java | 63 +++++++++++----------- .../org/openo/log/test/TestLogback2Logstash.java | 58 ++++++++++++++++++++ .../src/main/java/org/openo/log/test/logback.xml | 22 ++++++++ 6 files changed, 145 insertions(+), 44 deletions(-) create mode 100644 logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java create mode 100644 logging-sdk/src/main/java/org/openo/log/test/logback.xml diff --git a/logging-sdk/pom.xml b/logging-sdk/pom.xml index d617ebf..c616c74 100644 --- a/logging-sdk/pom.xml +++ b/logging-sdk/pom.xml @@ -22,19 +22,41 @@ - org.codehaus.jackson - jackson-mapper-asl - 1.9.9 + com.fasterxml.jackson.core + jackson-core + 2.7.3 + - org.slf4j - slf4j-api - 1.7.10 + com.fasterxml.jackson.core + jackson-databind + 2.7.3 + com.fasterxml.jackson.core - jackson-databind - 2.5.1 + jackson-annotations + 2.7.3 + + + ch.qos.logback + logback-classic + 1.1.3 + + + ch.qos.logback + logback-core + 1.1.3 + + + ch.qos.logback + logback-access + 1.1.3 + + + net.logstash.logback + logstash-logback-encoder + 4.8 diff --git a/logging-sdk/src/main/java/org/openo/log/api/LogService.java b/logging-sdk/src/main/java/org/openo/log/api/LogService.java index 737d4e2..a25e10a 100644 --- a/logging-sdk/src/main/java/org/openo/log/api/LogService.java +++ b/logging-sdk/src/main/java/org/openo/log/api/LogService.java @@ -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)) { diff --git a/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java b/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java index 6c389bf..5b1a6d9 100644 --- a/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java +++ b/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java @@ -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)); diff --git a/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java b/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java index 1205ce7..82445bb 100644 --- a/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java +++ b/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java @@ -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 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> elements = jsonValue.fields(); - while (elements.hasNext()) { - Entry 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> elements = jsonValue.fields(); +// while (elements.hasNext()) { +// Entry 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 index 0000000..ed4993a --- /dev/null +++ b/logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java @@ -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 index 0000000..4c41c2a --- /dev/null +++ b/logging-sdk/src/main/java/org/openo/log/test/logback.xml @@ -0,0 +1,22 @@ + + + + 127.0.0.1:4560 + + + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + \ No newline at end of file -- 2.16.6