From 07c8193bb579d4febd330cbec71eafb2e2b0102a Mon Sep 17 00:00:00 2001 From: t1mon <178317391@qq.com> Date: Thu, 23 Jul 2020 10:57:56 +0800 Subject: [PATCH] [Feature-2930][api] Displays path and host on the instance log panel. #2930 (#3154) * Optimize PropertyUtils instantiation. * Fix info error. * [Feature-2930][api] Displays path and host on the instance log panel. #2930 * [update] Add log-head in download log. * [update] clear code smell. --- .../api/service/LoggerService.java | 48 +++++++++++++------ .../dolphinscheduler/common/Constants.java | 5 ++ 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java index 2f44dee30..3c7b421d5 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/LoggerService.java @@ -16,6 +16,9 @@ */ package org.apache.dolphinscheduler.api.service; +import java.nio.charset.StandardCharsets; +import javax.annotation.PreDestroy; +import org.apache.commons.lang.ArrayUtils; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; @@ -29,8 +32,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import javax.annotation.PreDestroy; - /** * log service */ @@ -39,17 +40,19 @@ public class LoggerService { private static final Logger logger = LoggerFactory.getLogger(LoggerService.class); + private static final String LOG_HEAD_FORMAT = "[LOG-PATH]: %s, [HOST]: %s%s"; + @Autowired private ProcessService processService; private final LogClientService logClient; - public LoggerService(){ + public LoggerService() { logClient = new LogClientService(); } @PreDestroy - public void close(){ + public void close() { logClient.close(); } @@ -65,24 +68,34 @@ public class LoggerService { TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId); - if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())){ - return new Result(Status.TASK_INSTANCE_NOT_FOUND.getCode(), Status.TASK_INSTANCE_NOT_FOUND.getMsg()); + if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) { + return Result.error(Status.TASK_INSTANCE_NOT_FOUND); } String host = getHost(taskInstance.getHost()); Result result = new Result(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg()); - logger.info("log host : {} , logPath : {} , logServer port : {}",host,taskInstance.getLogPath(),Constants.RPC_PORT); + logger.info("log host : {} , logPath : {} , logServer port : {}", host, taskInstance.getLogPath(), + Constants.RPC_PORT); + + StringBuilder log = new StringBuilder(); + if (skipLineNum == 0) { + String head = String.format(LOG_HEAD_FORMAT, + taskInstance.getLogPath(), + host, + Constants.SYSTEM_LINE_SEPARATOR); + log.append(head); + } + + log.append(logClient + .rollViewLog(host, Constants.RPC_PORT, taskInstance.getLogPath(), skipLineNum, limit)); - String log = logClient.rollViewLog(host, Constants.RPC_PORT, taskInstance.getLogPath(),skipLineNum,limit); result.setData(log); return result; } - - /** * get log size * @@ -91,22 +104,27 @@ public class LoggerService { */ public byte[] getLogBytes(int taskInstId) { TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId); - if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())){ + if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) { throw new RuntimeException("task instance is null or host is null"); } String host = getHost(taskInstance.getHost()); - - return logClient.getLogBytes(host, Constants.RPC_PORT, taskInstance.getLogPath()); + byte[] head = String.format(LOG_HEAD_FORMAT, + taskInstance.getLogPath(), + host, + Constants.SYSTEM_LINE_SEPARATOR).getBytes(StandardCharsets.UTF_8); + return ArrayUtils.addAll(head, + logClient.getLogBytes(host, Constants.RPC_PORT, taskInstance.getLogPath())); } /** * get host + * * @param address address * @return old version return true ,otherwise return false */ - private String getHost(String address){ - if (Host.isOldVersion(address)){ + private String getHost(String address) { + if (Host.isOldVersion(address)) { return address; } return Host.of(address).getIp(); diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java index 57491c96b..f2494a68c 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java @@ -978,6 +978,11 @@ public final class Constants { public static final int NORAML_NODE_STATUS = 0; public static final int ABNORMAL_NODE_STATUS = 1; + /** + * system line separator + */ + public static final String SYSTEM_LINE_SEPARATOR = System.getProperty("line.separator"); + /** * net system properties */ -- GitLab