|
@@ -0,0 +1,89 @@
|
|
|
+package cn.com.qmth.examcloud.core.basic.api.controller;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.slf4j.MDC;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
|
|
|
+import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.DateUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 日志服务
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年11月30日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("${$rmp.ctr.basic}/log")
|
|
|
+public class LogController extends ControllerSupport {
|
|
|
+
|
|
|
+ private static Map<String, ExamCloudLog> loggersHolder = Maps.newConcurrentMap();
|
|
|
+
|
|
|
+ private static Object lock = new Object();
|
|
|
+
|
|
|
+ @ApiOperation(value = "学生客户端日志")
|
|
|
+ @PostMapping("studentClient/{level}/{code}")
|
|
|
+ public String logByStudentClient(@PathVariable String level, @PathVariable String code,
|
|
|
+ @RequestBody String info) {
|
|
|
+ log("STU_CLIENT_LOGGER", level, code, info);
|
|
|
+ return DateUtil.getNowISO();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写日志
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param logger
|
|
|
+ * @param level
|
|
|
+ * @param code
|
|
|
+ * @param info
|
|
|
+ */
|
|
|
+ private void log(String logger, String level, String code, String info) {
|
|
|
+ ExamCloudLog log = getLogger(logger);
|
|
|
+ MDC.put("CODE", code);
|
|
|
+ if ("debug".equalsIgnoreCase(level)) {
|
|
|
+ log.debug(info);
|
|
|
+ } else if ("info".equalsIgnoreCase(level)) {
|
|
|
+ log.info(info);
|
|
|
+ } else if ("error".equalsIgnoreCase(level)) {
|
|
|
+ log.error(info);
|
|
|
+ } else {
|
|
|
+ throw new StatusException("B-500001", "level must be [debug,info,error]");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取日志对象
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param logger
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private ExamCloudLog getLogger(String logger) {
|
|
|
+ ExamCloudLog log = loggersHolder.get(logger);
|
|
|
+ if (null == log) {
|
|
|
+ synchronized (lock) {
|
|
|
+ if (null == log) {
|
|
|
+ log = ExamCloudLogFactory.getLog(logger);
|
|
|
+ loggersHolder.put(logger, log);
|
|
|
+ }
|
|
|
+ return log;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return log;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|