diff options
author | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-02-16 10:39:30 +0800 |
---|---|---|
committer | HuabingZhao <zhao.huabing@zte.com.cn> | 2017-02-16 10:40:09 +0800 |
commit | 0025ff09a7574fe2bf935e66a1c81be81e207f92 (patch) | |
tree | 2659f427190193fee1acf03219c5563971df8fea /logging-sdk/src/main/java | |
parent | d4fc540886aeb23f4d483bcc78c5584821b47f0a (diff) |
Add logback to logstash test code
Issue-Id: OCS-163
Change-Id: I5d66978bb32192f44f047b0e381a01661ac9cb91
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'logging-sdk/src/main/java')
5 files changed, 115 insertions, 36 deletions
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<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 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 @@ +<?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 |