diff options
Diffstat (limited to 'wso2/logging-sdk')
12 files changed, 1555 insertions, 0 deletions
diff --git a/wso2/logging-sdk/pom.xml b/wso2/logging-sdk/pom.xml new file mode 100644 index 0000000..c616c74 --- /dev/null +++ b/wso2/logging-sdk/pom.xml @@ -0,0 +1,85 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- 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. --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.openo.common-services.common-utilities</groupId> + <artifactId>common-setting</artifactId> + <version>1.1.0-SNAPSHOT</version> + </parent> + <artifactId>logging-sdk</artifactId> + <name>common-services-common-utilities/logging-sdk</name> + <properties> + </properties> + + + <dependencies> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-core</artifactId> + <version>2.7.3</version> + </dependency> + + <dependency> + <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-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> + <plugins> + <plugin> + <artifactId>maven-compiler-plugin</artifactId> + </plugin> + <plugin> + <artifactId>maven-enforcer-plugin</artifactId> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-source-plugin</artifactId> + <executions> + <execution> + <id>attach-sources</id> + <goals> + <goal>jar</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/api/LogMessage.java b/wso2/logging-sdk/src/main/java/org/openo/log/api/LogMessage.java new file mode 100644 index 0000000..4a21584 --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/api/LogMessage.java @@ -0,0 +1,132 @@ +/** + * 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.api; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.slf4j.Logger; + +import org.openo.log.impl.Facitility; +import org.slf4j.LoggerFactory; + + +/** + * + * @author Huabing Zhao + * + */ + +public class LogMessage implements Serializable { + private static final Logger LOGGER = LoggerFactory.getLogger(LogMessage.class.getName()); + + protected long id = -1; + + + protected String detail = null; + + protected String hostname = ""; + + + + private String logType = ""; + + + protected HashMap<String, Object> extendedFields = null; + + private HashMap<String, String> localeMap = null; + + + + private static final long serialVersionUID = 200408041651009L; + + public String getDetail() { + return detail; + } + + + public long getId() { + return id; + } + + public String getHostname() { + return hostname; + } + + + + public LogMessage() {} + + + + public String getLogType() { + return logType; + } + + public void setLogType(String logType) { + this.logType = logType; + } + + public HashMap<String, Object> getExtendedFields() { + return extendedFields; + } + + public void setExtendedFields(HashMap<String, Object> extendedFields) { + this.extendedFields = extendedFields; + } + + public HashMap<String, String> getLocaleMap() { + return this.localeMap; + } + + public void setLocalMap(HashMap<String, String> localeMap) { + this.localeMap = localeMap; + } + + protected void set(String condName, Object condValue) { + if (condValue != null && !"".equals(condValue)) { + if (localeMap == null) { + localeMap = new HashMap<String, String>(); + } + localeMap.put(condName, condValue.toString()); + + } + } + + + protected void setLocale(String condName, String condValue) { + + if (condValue != null && !"".equals(condValue)) { + if (localeMap == null) { + localeMap = new HashMap<String, String>(); + } + + + + Map<String, String> mapJson = Facitility.readJson2Map(condValue); + for (Entry<String, String> entry : mapJson.entrySet()) { + String key = entry.getKey(); + + String value = entry.getValue().replace("\"", "\'"); + localeMap.put(condName + "_" + key, value); + } + } + + } + + +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/api/LogService.java b/wso2/logging-sdk/src/main/java/org/openo/log/api/LogService.java new file mode 100644 index 0000000..a25e10a --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/api/LogService.java @@ -0,0 +1,140 @@ +/** + * 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.api; + +import org.openo.log.impl.InsertLogHandler; +import org.openo.log.impl.LogConst; +import org.openo.log.impl.LogIdTool; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LogService { + + private static final Logger LOGGER = LoggerFactory.getLogger(LogService.class.getName()); + + + public static final String OPERLOG_SUCCESS = "log_success"; + + public static final String OPERLOG_ERROR = "log_fail"; + + + public static final String OPERLOG_RANK_NORMAL = "operlog_rank_normal"; + + public static final String OPERLOG_RANK_NOTICE = "operlog_rank_notice"; + + public static final String OPERLOG_RANK_IMPORTANT = "operlog_rank_important"; + + public static final String OPERLOG_RANK_VERYIMPORTANT = " operlog_rank_veryimportant"; + + + public static final String SYSLOG_RANK_INFORM = "syslog_rank_inform"; + + public static final String SYSLOG_RANK_NOTICE = "syslog_rank_notice"; + + + public static final String SYSLOG_RANK_WARN = "syslog_rank_warn"; + + public static final String SYSLOG_RANK_ERROR = "syslog_rank_error"; + + + public static final String SYSLOG_RANK_ALERT = "syslog_rank_alert"; + + + public static final String SYSLOG_RANK_EMERG = "syslog_rank_emerg"; + + + public static final String CONNECT_TYPE_WEB = "WEB"; + + public static final String CONNECT_TYPE_TELNET = "TELNET"; + + public static final String CONNECT_TYPE_SSH = "SSH"; + + public static final String CONNECT_TYPE_EM = "EM"; + + public static final short LOG_DISPLAY_NOT = 0; + + public static final short LOG_DISPLAY = 1; + + public static final int LOG_ROOT_LINK_ID = -1; + + private static final int LOG_NO_LINK_ID = 0; + + private static LogService recordLogHandler = null; + + public static LogService getInstance() { + if (recordLogHandler == null) { + recordLogHandler = new LogService(); + } + return recordLogHandler; + } + + public long recordOperLog(OperLogMessage logMessage) { + // TODO Auto-generated method stub + LOGGER.info("receive a insert operLog message"); + InsertLogHandler insertLogHandler = new InsertLogHandler(); + if (insertLogHandler.checkCmdLog((OperLogMessage) logMessage)) { + + long id = + LogIdTool.getRandomID(LogConst.CMD_LOG_FLAG, logMessage.getLogStartDate().getTime()); + logMessage.setId(id); + insertLogHandler.insertLog(logMessage, LogConst.AUDITLOG_OPERATION, LogConst.OPERLOG_TYPE); + + return logMessage.getId(); + } + + return 0; + } + + public long recordSecLog(SecLogMessage logMessage) { + // TODO Auto-generated method stub + //LOGGER.info("receive a insert sec log message"); + InsertLogHandler insertLogHandler = new InsertLogHandler(); + if (insertLogHandler.checkSecLog((SecLogMessage) logMessage)) { + + long id = LogIdTool.getRandomID(LogConst.SECRET_LOG_FLAG, logMessage.getLogDate().getTime()); + logMessage.setId(id); + + insertLogHandler.insertLog(logMessage, LogConst.AUDITLOG_SECURITY, LogConst.SERLOG_TYPE); + + return logMessage.getId(); + } + return 0; + } + + + + public long recordSysLog(SysLogMessage logMessage) { + //LOGGER.info("receive a insert sys log message"); + InsertLogHandler insertLogHandler = new InsertLogHandler(); + if (insertLogHandler.checkSysLog((SysLogMessage) logMessage)) { + + long id = + LogIdTool.getRandomID(LogConst.SYS_LOG_FLAG, logMessage.getLogStartDate().getTime()); + logMessage.setId(id); + insertLogHandler.insertLog(logMessage, LogConst.AUDITLOG_SYSTEM, LogConst.SYSLOG_TYPE); + return logMessage.getId(); + } + return 0; + } + + public static void main(String[] args) + { + LogService service = new LogService(); + OperLogMessage logMessage = new OperLogMessage(); + + service.recordOperLog(logMessage ); + } +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/api/OperLogMessage.java b/wso2/logging-sdk/src/main/java/org/openo/log/api/OperLogMessage.java new file mode 100644 index 0000000..567cfd0 --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/api/OperLogMessage.java @@ -0,0 +1,286 @@ +/** + * 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.api; + +import java.util.Date; + +import org.openo.log.impl.Facitility; +import org.openo.log.impl.LogConst; + + + +public class OperLogMessage extends LogMessage { + private static final long serialVersionUID = 200408041704009L; + + + public OperLogMessage() { + super(); + this.setLogType(LogConst.OPERLOG_TYPE); + } + + + public OperLogMessage(String userName, String operation, String descriptionInfo, String hostname, + String operateResult, String rank, Date logStartDate, Date logEndDate, String detail) { + super(); + this.setLogType(LogConst.OPERLOG_TYPE); + + this.userName = userName; + this.set("userName", userName); + + this.descriptionInfo = descriptionInfo; + this.setLocale("descriptionInfo", descriptionInfo); + + this.operation = operation; + this.setLocale("operation", operation); + + this.logStartDate = logStartDate; + this.set("logStartDate", Facitility.dateFormat(logStartDate)); + + this.logEndDate = logEndDate; + this.set("logEndDate", Facitility.dateFormat(logEndDate)); + + this.hostname = hostname; + this.set("hostname", hostname); + + this.operateResult = operateResult; + this.set("operateResult", operateResult); + + this.rank = rank; + this.set("rank", rank); + + this.detail = detail; + this.setLocale("detail", detail); + } + + + protected String userName = ""; + + + protected String operation = null; + + + protected String descriptionInfo = null; + + + private String operationType = "other"; + + protected String failReason = null; + + + protected String operateResult = LogService.OPERLOG_SUCCESS; + + + protected String connectMode = LogService.CONNECT_TYPE_WEB; + + + protected String[] resourceIP; + + + protected Date logStartDate = null; + + + protected Date logEndDate = null; + + + protected long linkId = -100; + + protected int isDisplay = LogService.LOG_DISPLAY; + + protected String mac = ""; + + /** + * name(id)|name(id) + */ + private String[] operateResource = null; + + + protected String rank = LogService.OPERLOG_RANK_NORMAL; + + public String getMac() { + return mac; + } + + public OperLogMessage setMac(String mac) { + this.set("mac", mac); + this.mac = mac; + return this; + } + + + public String getUserName() { + return userName; + } + + public OperLogMessage setUserName(String name) { + this.userName = name; + this.set("userName", name); + return this; + } + + public String getFailReason() { + return failReason; + } + + public OperLogMessage setFailReason(String reason) { + this.setLocale("failReason", reason); + this.failReason = reason; + return this; + } + + public String getConnectMode() { + return connectMode; + + } + + public OperLogMessage setConnectMode(String mode) { + this.set("connectMode", mode); + this.connectMode = mode; + return this; + } + + public long getLinkId() { + return linkId; + } + + public OperLogMessage setLinkId(long linkId) { + this.set("linkId", linkId); + this.linkId = linkId; + return this; + } + + public String getOperateResult() { + return operateResult; + } + + public OperLogMessage setOperateResult(String operateResult) { + this.set("operateResult", operateResult); + this.operateResult = operateResult; + return this; + } + + public String getRank() { + return rank; + } + + public OperLogMessage setRank(String rank) { + this.set("rank", rank); + this.rank = rank; + return this; + } + + public String getDescriptionInfo() { + return descriptionInfo; + } + + public OperLogMessage setDescriptionInfo(String desc) { + this.setLocale("descriptionInfo", desc); + descriptionInfo = desc; + return this; + } + + public String getOperation() { + return operation; + } + + public OperLogMessage setOperation(String operation) { + this.setLocale("operation", operation); + this.operation = operation; + return this; + } + + public String[] getResourceIP() { + return resourceIP; + } + + public OperLogMessage setResourceIP(String[] resourceIP) { + this.set("resourceIP", resourceIP); + this.resourceIP = resourceIP; + return this; + } + + public Date getLogStartDate() { + return logStartDate; + } + + public OperLogMessage setLogStartDate(Date logStartDate) { + this.set("logStartDate", Facitility.dateFormat(logStartDate)); + this.logStartDate = logStartDate; + return this; + } + + public Date getLogEndDate() { + return logEndDate; + } + + public OperLogMessage setLogEndDate(Date logEndDate) { + this.set("logEndDate", Facitility.dateFormat(logEndDate)); + this.logEndDate = logEndDate; + return this; + } + + public int getIsDisplay() { + return isDisplay; + } + + public OperLogMessage setIsDisplay(int isDisplay) { + this.set("isDisplay", isDisplay); + this.isDisplay = isDisplay; + return this; + } + + public String[] getOperateResource() { + return operateResource; + } + + public OperLogMessage setOperateResource(String[] operateResource) { + this.set("operateResource", operateResource); + this.operateResource = operateResource; + return this; + } + + public String getOperationType() { + return operationType; + } + + public OperLogMessage setOperationType(String operationType) { + + this.operationType = operationType; + return this; + } + + + + public OperLogMessage setHostname(String hostname) { + this.set("hostname", hostname); + this.hostname = hostname; + return this; + } + + + public LogMessage setId(long id) { + this.set("id", id); + this.id = id; + return this; + } + + + public LogMessage setDetail(String detail) { + this.detail = detail; + this.setLocale("detail", detail); + return this; + } + +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/api/SecLogMessage.java b/wso2/logging-sdk/src/main/java/org/openo/log/api/SecLogMessage.java new file mode 100644 index 0000000..7eafbf7 --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/api/SecLogMessage.java @@ -0,0 +1,146 @@ +/** + * 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.api; + +import java.util.Date; + +import org.openo.log.impl.Facitility; +import org.openo.log.impl.LogConst; + +public class SecLogMessage extends LogMessage { + + private static final long serialVersionUID = 201603100924009L; + + + public SecLogMessage() { + super(); + this.setLogType(LogConst.SERLOG_TYPE); + } + + + public SecLogMessage(String userName, String hostname, String detail, String logName, + Date logDate) { + super(); + this.setLogType(LogConst.SERLOG_TYPE); + + this.userName = userName; + this.set("userName", userName); + + this.hostname = hostname; + this.set("hostname", hostname); + + this.logName = logName; + this.setLocale("logName", logName); + + this.detail = detail; + this.setLocale("detail", detail); + + this.logDate = logDate; + this.set("logDate", Facitility.dateFormat(logDate)); + } + + protected String userName = ""; + + + protected String logName = null; + + + protected String connectMode = ""; + + + protected String systemID = ""; + + protected String mac = ""; + + public String getMac() { + return mac; + } + + public SecLogMessage setMac(String mac) { + this.set("mac", mac); + this.mac = mac; + return this; + } + + + protected Date logDate = null; + + + public String getUserName() { + + return userName; + } + + + public SecLogMessage setUserName(String userName) { + this.set("userName", userName); + this.userName = userName; + return this; + } + + + public String getConnectMode() { + return connectMode; + } + + + public SecLogMessage setConnectMode(String connectMode) { + this.set("connectMode", connectMode); + this.connectMode = connectMode; + return this; + } + + public String getLogName() { + return logName; + } + + public SecLogMessage setLogName(String logName) { + this.setLocale("logName", logName); + this.logName = logName; + return this; + } + + public Date getLogDate() { + return logDate; + } + + public SecLogMessage setLogDate(Date logDate) { + this.set("logDate", Facitility.dateFormat(logDate)); + this.logDate = logDate; + return this; + } + + public SecLogMessage setHostname(String hostname) { + this.set("hostname", hostname); + this.hostname = hostname; + return this; + } + + + public SecLogMessage setId(long id) { + this.set("id", id); + this.id = id; + return this; + } + + + public SecLogMessage setDetail(String detail) { + this.detail = detail; + this.setLocale("detail", detail); + return this; + } + +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java b/wso2/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java new file mode 100644 index 0000000..5b1a6d9 --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/api/SysLogMessage.java @@ -0,0 +1,180 @@ +/** + * 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.api; + +import java.util.Date; + +import org.openo.log.impl.Facitility; +import org.openo.log.impl.LogConst; + + +public class SysLogMessage extends LogMessage { + private static final long serialVersionUID = 200408041707009L; + + + protected String rank = LogService.SYSLOG_RANK_INFORM; + + protected String source = null; + + protected String sourceId = ""; + + protected String logName = null; + + + protected Date logStartDate = null; + + protected Date logEndDate = null; + + protected int isDisplay = LogService.LOG_DISPLAY; + + protected long linkId = -100; + + + public SysLogMessage() { + super(); + this.setLogType(LogConst.SYSLOG_TYPE); + } + + + public SysLogMessage(String logName, String hostname, String rank, String detail, + Date logStartDate, Date logEndDate) { + super(); + this.setLogType(LogConst.SYSLOG_TYPE); + + this.logName = logName; + this.set("logName", logName); + + this.rank = rank; + this.set("rank", rank); + + this.hostname = hostname; + this.set("hostname", hostname); + + this.detail = detail; + this.set("detail", detail); + + this.logStartDate = logStartDate; + this.set("logStartDate", Facitility.dateFormat(logStartDate)); + + this.logEndDate = logEndDate; + this.set("logEndDate", Facitility.dateFormat(logEndDate)); + } + + public Date getLogStartDate() { + return logStartDate; + } + + public SysLogMessage setLogStartDate(Date logStartDate) { + this.set("logStartDate", Facitility.dateFormat(logStartDate)); + this.logStartDate = logStartDate; + return this; + } + + public Date getLogEndDate() { + return logEndDate; + } + + public SysLogMessage setLogEndDate(Date logEndDate) { + this.set("logEndDate", Facitility.dateFormat(logEndDate)); + this.logEndDate = logEndDate; + return this; + } + + public String getRank() { + return rank; + } + + public SysLogMessage setRank(String rank) { + this.set("rank", rank); + this.rank = rank; + return this; + } + + public String getLogName() { + return logName; + } + + public SysLogMessage setLogName(String logName) { + this.setLocale("logName", logName); + this.logName = logName; + return this; + } + + + public final String getSource() { + return source; + } + + + public SysLogMessage setSource(String source) { + this.setLocale("source", source); + this.source = source; + return this; + } + + public int getIsDisplay() { + return isDisplay; + } + + public SysLogMessage setIsDisplay(int isDisplay) { + this.set("isDisplay", isDisplay); + this.isDisplay = isDisplay; + return this; + } + + public long getLinkId() { + return linkId; + } + + public String getSourceId() { + return sourceId; + } + + public SysLogMessage setSourceId(String sourceId) { + this.set("sourceId", sourceId); + this.sourceId = sourceId; + return this; + } + + public SysLogMessage setLinkId(long linkId) { + this.set("linkId", linkId); + this.linkId = linkId; + return this; + } + + public SysLogMessage setHostname(String hostname) { + this.set("hostname", hostname); + this.hostname = hostname; + return this; + } + + + public SysLogMessage setId(long id) { + this.set("id", id); + this.id = id; + return this; + } + + + public SysLogMessage setDetail(String detail) { + this.detail = detail; + this.setLocale("detail", detail); + return this; + } + + + +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java b/wso2/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java new file mode 100644 index 0000000..82445bb --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/impl/Facitility.java @@ -0,0 +1,137 @@ +/** + * 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.impl; + +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Facitility { + private static final Logger LOGGER = LoggerFactory.getLogger(Facitility.class.getName()); + + private Facitility() { + + } + + public static String oToJ(Object o) { + ObjectMapper om = new ObjectMapper(); + Writer w = new StringWriter(); + String json = null; + try { + + om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")); + om.writeValue(w, o); + json = w.toString(); + w.close(); + } catch (IOException e) { + LOGGER.error("IOException", e); + } + return json; + } + + public static Map<String, String> readJson2Map(String json) { + ObjectMapper objectMapper = new ObjectMapper(); + try { + Map<String, String> maps = objectMapper.readValue(json, Map.class); + return maps; + } catch (Exception e) { + LOGGER.error("IOException", e); + return null; + } + } + + public static String hashMapToJson(HashMap<String, String> map) { + String string = "{"; + for (Iterator it = map.entrySet().iterator(); it.hasNext();) { + Entry e = (Entry) it.next(); + string += "\"" + e.getKey() + "\":"; + string += "\"" + e.getValue() + "\","; + } + string = string.substring(0, string.lastIndexOf(",")); + string += "}"; + return string; + } + + public static String dateFormat(Date date) { + SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); + return time.format(date); + } + + public static String checkRequiredParam(HashMap<String, Object> checkParamsMap) { + StringBuilder errMsg = new StringBuilder(); + java.util.Iterator<String> hashMapIt = checkParamsMap.keySet().iterator(); + int count = 0; + while (hashMapIt.hasNext()) { + String key = hashMapIt.next(); + Object value = checkParamsMap.get(key); + if (value == null || "".equals(value)) { + errMsg.append(key); + count++; + if (count < checkParamsMap.size() - 1) { + errMsg.append(" and "); + } + } + + } + if (count > 0) { + errMsg.append(" can't be null or \"\"!\n "); + } + return errMsg.toString(); + } + + public static String checkRequiredJsonParam(String jsonParam, String key) { + 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/wso2/logging-sdk/src/main/java/org/openo/log/impl/InsertLogHandler.java b/wso2/logging-sdk/src/main/java/org/openo/log/impl/InsertLogHandler.java new file mode 100644 index 0000000..ea5570f --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/impl/InsertLogHandler.java @@ -0,0 +1,265 @@ +/** + * 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.impl; + +import java.util.HashMap; +import org.slf4j.Logger; + +import org.openo.log.api.LogMessage; +import org.openo.log.api.LogService; +import org.openo.log.api.OperLogMessage; +import org.openo.log.api.SecLogMessage; +import org.openo.log.api.SysLogMessage; +import org.slf4j.LoggerFactory; + + + +public class InsertLogHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(InsertLogHandler.class.getName()); + + public void insertLog(LogMessage msg, String logIndex, String logType) { + String logJson = Facitility.hashMapToJson(msg.getLocaleMap()); + msg.setLocalMap(null); + + if (msg.getExtendedFields() != null) { + String exendedFieldsJson = Facitility.oToJ(msg.getExtendedFields()); + logJson = logJson.substring(0, logJson.lastIndexOf("}")) + "," + + exendedFieldsJson.substring(1) + "\n"; + msg.setExtendedFields(null); + } + LOGGER.info(logJson, logIndex, logType); + + + } + + public boolean checkCmdLog(OperLogMessage msg) { + StringBuilder errMsg = new StringBuilder(); + boolean errFlag = false; + HashMap<String, Object> checkParamsMap = new HashMap<>(); + checkParamsMap.put("userName", msg.getUserName()); + checkParamsMap.put("operation", msg.getOperation()); + checkParamsMap.put("descriptionInfo", msg.getDescriptionInfo()); + checkParamsMap.put("hostname", msg.getHostname()); + checkParamsMap.put("detail", msg.getDetail()); + checkParamsMap.put("logStartDate", msg.getLogStartDate()); + checkParamsMap.put("logEndDate", msg.getLogEndDate()); + errMsg.append(Facitility.checkRequiredParam(checkParamsMap)); + + + errMsg.append(checkCmdRank(msg.getRank())); + errMsg.append(checkConnectMode(msg.getConnectMode())); + errMsg.append(checkOperateResult(msg.getOperateResult())); + errMsg.append(checkIsDisplay(msg.getIsDisplay())); + + if (msg.getOperation() != null && !("").equals(msg.getOperation())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getOperation(), "operation")); + } + if (msg.getDescriptionInfo() != null && !("").equals(msg.getDescriptionInfo())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getDescriptionInfo(), "descriptionInfo")); + } + if (msg.getDetail() != null && !("").equals(msg.getDetail())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getDetail(), "detail")); + } + if (msg.getFailReason() != null && !("").equals(msg.getFailReason())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getFailReason(), "failReason")); + } + if (!"".equals(errMsg.toString())) { + LOGGER.warn(errMsg.toString()); + printCmdLog(msg); + errFlag = true; + } + return !errFlag; + } + + private String checkIsDisplay(int isDisplay) { + StringBuilder errMsg = new StringBuilder(); + if (isDisplay != LogService.LOG_DISPLAY && isDisplay != LogService.LOG_DISPLAY_NOT) { + errMsg.append("isDisplay must be 'LogService.LOG_DISPLAY'"); + errMsg.append(" or 'LogService.LOG_DISPLAY_NOT'\n"); + + } + return errMsg.toString(); + } + + private String checkOperateResult(String operateResult) { + StringBuilder errMsg = new StringBuilder(); + if ((operateResult != LogService.OPERLOG_SUCCESS) + && (operateResult != LogService.OPERLOG_ERROR)) { + errMsg.append("operateResult must be ' LogService.OPERLOG_ERROR'"); + errMsg.append(" or 'LogService.OPERLOG_SUCCESS'\n"); + + } + return errMsg.toString(); + } + + private String checkConnectMode(String connectMode) { + StringBuilder errMsg = new StringBuilder(); + if (!(connectMode.equals(LogService.CONNECT_TYPE_WEB) + || connectMode.equals(LogService.CONNECT_TYPE_TELNET) + || connectMode.equals(LogService.CONNECT_TYPE_SSH) + || connectMode.equals(LogService.CONNECT_TYPE_EM))) { + errMsg.append("connectMode must be 'LogService.CONNECT_TYPE_WEB'"); + errMsg.append(" or 'LogService.CONNECT_TYPE_TELNET'"); + errMsg.append(" or 'LogService.CONNECT_TYPE_SSH'"); + errMsg.append(" or 'LogService.CONNECT_TYPE_EM'\n"); + + } + + return errMsg.toString(); + } + + private String checkCmdRank(String rank) { + StringBuilder errMsg = new StringBuilder(); + boolean isExist = false; + String[] ranks = {LogService.OPERLOG_RANK_NOTICE, LogService.OPERLOG_RANK_NORMAL, + LogService.OPERLOG_RANK_IMPORTANT, LogService.OPERLOG_RANK_VERYIMPORTANT}; + for (int i = 0; i < ranks.length; i++) { + if (rank.equalsIgnoreCase(ranks[i])) { + isExist = true; + break; + } + } + if (!isExist) { + errMsg.append("rank must be 'LogService.OPERLOG_RANK_NORMAL'"); + errMsg.append(" or 'LogService.OPERLOG_RANK_NOTICE'"); + errMsg.append(" or 'LogService.OPERLOG_RANK_IMPORTANT'"); + errMsg.append(" or 'LogService.OPERLOG_RANK_VERYIMPORTANT'\n"); + + } + + return errMsg.toString(); + } + + public void printCmdLog(OperLogMessage msg) { + LOGGER.warn("CMD Log Error Detail msg"); + LOGGER.warn("UserName :" + msg.getUserName()); + LOGGER.warn("operation :" + msg.getOperation()); + LOGGER.warn("hostname :" + msg.getHostname()); + LOGGER.warn("descriptionInfo :" + msg.getDescriptionInfo()); + LOGGER.warn("operateResult :" + msg.getOperateResult()); + LOGGER.warn("FailReason :" + msg.getFailReason()); + LOGGER.warn("detail :" + msg.getDetail()); + LOGGER.warn("ConnectMode:" + msg.getConnectMode()); + LOGGER.warn("OperateResource :" + msg.getOperateResource()); + LOGGER.warn("logStartDate :" + msg.getOperateResource()); + LOGGER.warn("logEndDate :" + msg.getLogEndDate()); + LOGGER.warn("Rank :" + msg.getRank()); + } + + /** + * 检查日志�数是�符��求 + * + * @return true + */ + public boolean checkSecLog(SecLogMessage msg) { + StringBuilder errMsg = new StringBuilder(); + boolean errFlag = false; + HashMap<String, Object> checkParamsMap = new HashMap<>(); + checkParamsMap.put("userName", msg.getUserName()); + checkParamsMap.put("logName", msg.getLogName()); + checkParamsMap.put("hostname", msg.getHostname()); + checkParamsMap.put("detail", msg.getDetail()); + checkParamsMap.put("logDate", msg.getLogDate()); + errMsg.append(Facitility.checkRequiredParam(checkParamsMap)); + errMsg.append(checkConnectMode(msg.getConnectMode())); + + if (msg.getLogName() != null && !("").equals(msg.getLogName())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getLogName(), "logName")); + } + if (msg.getDetail() != null && !("").equals(msg.getDetail())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getDetail(), "detail")); + } + if (!"".equals(errMsg.toString())) { + LOGGER.warn(errMsg.toString()); + this.printSecLog(msg); + errFlag = true; + } + return !errFlag; + } + + public void printSecLog(SecLogMessage secLog) { + LOGGER.warn("SCRT Log Error Detail msg"); + LOGGER.warn("logDate :" + secLog.getLogDate()); + LOGGER.warn("logName :" + secLog.getLogName()); + LOGGER.warn("userName :" + secLog.getUserName()); + LOGGER.warn("hostname :" + secLog.getHostname()); + LOGGER.warn("connectMode:" + secLog.getConnectMode()); + LOGGER.warn("detail :" + secLog.getDetail()); + } + + public boolean checkSysLog(SysLogMessage msg) { + StringBuilder errMsg = new StringBuilder(); + boolean errFlag = false; + HashMap<String, Object> checkParamsMap = new HashMap<>(); + + checkParamsMap.put("logName", msg.getLogName()); + checkParamsMap.put("logStartDate", msg.getLogStartDate()); + checkParamsMap.put("logEndDate", msg.getLogEndDate()); + checkParamsMap.put("hostname", msg.getHostname()); + checkParamsMap.put("detail", msg.getDetail()); + errMsg.append(Facitility.checkRequiredParam(checkParamsMap)); + errMsg.append(checkSysRank(msg.getRank())); + if (msg.getLogName() != null && !("").equals(msg.getLogName())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getLogName(), "logName")); + } + if (msg.getDetail() != null && !("").equals(msg.getDetail())) { + errMsg.append(Facitility.checkRequiredJsonParam(msg.getDetail(), "detail")); + } + if (!"".equals(errMsg.toString())) { + LOGGER.warn(errMsg.toString()); + printSysLog(msg); + errFlag = true; + } + return !errFlag; + } + + private String checkSysRank(String rank) { + StringBuilder errMsg = new StringBuilder(); + boolean isExist = false; + String[] ranks = {LogService.SYSLOG_RANK_INFORM, LogService.SYSLOG_RANK_NOTICE, + LogService.SYSLOG_RANK_WARN, LogService.SYSLOG_RANK_ERROR, LogService.SYSLOG_RANK_ALERT, + LogService.SYSLOG_RANK_EMERG}; + for (int i = 0; i < ranks.length; i++) { + if (rank.equalsIgnoreCase(ranks[i])) { + isExist = true; + break; + } + } + if (!isExist) { + errMsg.append("rank must be 'LogService.SYSLOG_INFORM'"); + errMsg.append(" or 'LogService.SYSLOG_NOTICE'"); + errMsg.append(" or 'LogService.SYSLOG_WARN'"); + errMsg.append(" or 'LogService.SYSLOG_ERROR'"); + errMsg.append(" or 'LogService.SYSLOG_ALTERT'"); + errMsg.append(" or 'LogService.SYSLOG_EMERG'\n"); + + } + + return errMsg.toString(); + } + + public void printSysLog(SysLogMessage msg) { + LOGGER.warn("System Log Error Detail msg"); + LOGGER.warn("logName :" + msg.getLogName()); + LOGGER.warn("rank :" + msg.getRank()); + LOGGER.warn("source :" + msg.getSource()); + LOGGER.warn("logStartDate :" + msg.getLogStartDate()); + LOGGER.warn("logEndDate :" + msg.getLogEndDate()); + LOGGER.warn("detail :" + msg.getDetail()); + } + +} + diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogConst.java b/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogConst.java new file mode 100644 index 0000000..1e45313 --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogConst.java @@ -0,0 +1,37 @@ +/** + * 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.impl; + +public class LogConst { + private LogConst() { + + } + + public final static int CMD_LOG_FLAG = 1; + public final static int SYS_LOG_FLAG = 2; + public final static int SECRET_LOG_FLAG = 3; + + public final static String AUDITLOG_OPERATION = "auditlog_operation"; + public final static String AUDITLOG_SECURITY = "auditlog_security"; + public final static String AUDITLOG_SYSTEM = "auditlog_system"; + + public final static String OPERLOG_TYPE = "operlog"; + public final static String SERLOG_TYPE = "seclog"; + public final static String SYSLOG_TYPE = "syslog"; + + + +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogIdTool.java b/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogIdTool.java new file mode 100644 index 0000000..4dea43b --- /dev/null +++ b/wso2/logging-sdk/src/main/java/org/openo/log/impl/LogIdTool.java @@ -0,0 +1,67 @@ +/** + * 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.impl; + +import java.util.Date; +import java.util.Random; + + + +public class LogIdTool { + private static Random cmdRand = new Random(); + + private static Random sysRand = new Random(); + + private static Random secRand = new Random(); + + private LogIdTool() { + + } + + public static long getRandomID(int logType, long startDate) { + String strCtime = "" + startDate; + if (strCtime.length() > 13) { + strCtime = strCtime.substring(strCtime.length() - 13); + } + int seed1; + if (logType == LogConst.CMD_LOG_FLAG) { + seed1 = cmdRand.nextInt(100000); + } else if (logType == LogConst.SYS_LOG_FLAG) { + seed1 = sysRand.nextInt(100000); + } else if (logType == LogConst.SECRET_LOG_FLAG) { + seed1 = secRand.nextInt(100000); + } else { + return 0; + } + String seed2 = "" + seed1; + int shouldFillLen = 5 - seed2.length(); + for (int i = 0; i < shouldFillLen; i++) { + seed2 = "0" + seed2; + } + return Long.parseLong(strCtime + seed2); + } + + public static long transTimeCond2ID(Date time, boolean isStart) { + long timeLong = time.getTime(); + if (isStart) { + timeLong = timeLong * 100000; + } else { + timeLong = timeLong * 100000 + 99999; + } + return timeLong; + + } +} diff --git a/wso2/logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java b/wso2/logging-sdk/src/main/java/org/openo/log/test/TestLogback2Logstash.java new file mode 100644 index 0000000..ed4993a --- /dev/null +++ b/wso2/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/wso2/logging-sdk/src/main/java/org/openo/log/test/logback.xml b/wso2/logging-sdk/src/main/java/org/openo/log/test/logback.xml new file mode 100644 index 0000000..4c41c2a --- /dev/null +++ b/wso2/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 |