From 85540229e936abbd1481bc0c28f5bdcc958078ab Mon Sep 17 00:00:00 2001 From: Carpenter Lee Date: Mon, 8 Oct 2018 19:46:38 +0800 Subject: [PATCH] Change eagleEye log path when log base configured. --- .../sentinel/eagleeye/BaseLoggerBuilder.java | 7 ++++ .../csp/sentinel/log/CommandCenterLog.java | 4 +- .../com/alibaba/csp/sentinel/log/LogBase.java | 37 ++++++++++--------- .../slots/logger/EagleEyeLogUtil.java | 12 +++--- .../slots/logger/EagleEyeLogUtilTest.java | 36 ++++++++++++++++++ 5 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/eagleeye/BaseLoggerBuilder.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/eagleeye/BaseLoggerBuilder.java index 870bd92b..0f9b2059 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/eagleeye/BaseLoggerBuilder.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/eagleeye/BaseLoggerBuilder.java @@ -53,6 +53,13 @@ class BaseLoggerBuilder> { return (T)this; } + @SuppressWarnings("unchecked") + public T configLogFilePath(String filePath) { + EagleEyeCoreUtils.checkNotNullEmpty(filePath, "filePath"); + this.filePath = filePath; + return (T)this; + } + @SuppressWarnings("unchecked") public T maxFileSizeMB(long maxFileSizeMB) { if (maxFileSize < 10) { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/CommandCenterLog.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/CommandCenterLog.java index fc02c2ff..e80c59ef 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/CommandCenterLog.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/CommandCenterLog.java @@ -24,8 +24,8 @@ import java.util.logging.Logger; */ public class CommandCenterLog extends LogBase { - private static final Logger heliumRecordLog = Logger.getLogger("cspMetricLog"); - private static final String FILE_NAME = "metricStat.log"; + private static final Logger heliumRecordLog = Logger.getLogger("cspCommandCenterLog"); + private static final String FILE_NAME = "commandCenter.log"; private static Handler logHandler = null; static { diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java index 4e07d758..78f2bae1 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java @@ -38,10 +38,28 @@ public class LogBase { static { // first use -D, then use user home. String logDir = System.getProperty(LOG_DIR); + if (logDir == null || logDir.isEmpty()) { logDir = System.getProperty(USER_HOME); + logDir = addSeparator(logDir) + DIR_NAME + File.separator; + } + logDir = addSeparator(logDir); + File dir = new File(logDir); + if (!dir.exists()) { + if (!dir.mkdirs()) { + System.err.println("ERROR: create log base dir error: " + logDir); + } } - setLogBaseDir(logDir); + // logBaseDir must end with File.separator + logBaseDir = logDir; + System.out.println("INFO: log base dir is: " + logBaseDir); + } + + private static String addSeparator(String logDir) { + if (!logDir.endsWith(File.separator)) { + logDir += File.separator; + } + return logDir; } /** @@ -53,23 +71,6 @@ public class LogBase { return logBaseDir; } - /** - * Change log dir, the dir will be created if not exits - * - * @param baseDir - */ - protected static void setLogBaseDir(String baseDir) { - if (!baseDir.endsWith(File.separator)) { - baseDir += File.separator; - } - String path = baseDir + DIR_NAME + File.separator; - File dir = new File(path); - if (!dir.exists()) { - dir.mkdirs(); - } - logBaseDir = path; - } - protected static Handler makeLogger(String logName, Logger heliumRecordLog) { CspFormatter formatter = new CspFormatter(); String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid(); diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java index 7cca5c17..c599bb8f 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java @@ -15,28 +15,26 @@ */ package com.alibaba.csp.sentinel.slots.logger; -import java.io.File; - import com.alibaba.csp.sentinel.eagleeye.EagleEye; import com.alibaba.csp.sentinel.eagleeye.StatLogger; +import com.alibaba.csp.sentinel.log.LogBase; public class EagleEyeLogUtil { - private static final String DIR_NAME = "csp"; - private static final String FILE_NAME = "sentinel-block.log"; + public static final String FILE_NAME = "sentinel-block.log"; private static StatLogger statLogger; static { - String path = DIR_NAME + File.separator + FILE_NAME; + String path = LogBase.getLogBaseDir() + FILE_NAME; - statLogger = EagleEye.statLoggerBuilder("sentinel-block-record") + statLogger = EagleEye.statLoggerBuilder("sentinel-block-log") .intervalSeconds(1) .entryDelimiter('|') .keyDelimiter(',') .valueDelimiter(',') .maxEntryCount(6000) - .baseLogFilePath(path) + .configLogFilePath(path) .maxFileSizeMB(300) .maxBackupIndex(3) .buildSingleton(); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java new file mode 100644 index 00000000..b2b769d0 --- /dev/null +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java @@ -0,0 +1,36 @@ +package com.alibaba.csp.sentinel.slots.logger; + +import java.io.File; + +import com.alibaba.csp.sentinel.log.LogBase; +import com.alibaba.csp.sentinel.log.RecordLog; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Carpenter Lee + */ +public class EagleEyeLogUtilTest { + + @Test + public void testWriteLog() throws Exception { + EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1); + Thread.sleep(1100); + String file = RecordLog.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME; + assertTrue(new File(file).exists()); + } + + @Test + public void testChangeLogBase() throws Exception { + String userHome = System.getProperty("user.home"); + String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis(); + System.setProperty(LogBase.LOG_DIR, newLogBase); + + EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1); + Thread.sleep(1100); + String file = RecordLog.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME; + assertTrue(new File(file).exists()); + } +} \ No newline at end of file