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 f5f34243..fc02c2ff 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 @@ -32,14 +32,6 @@ public class CommandCenterLog extends LogBase { logHandler = makeLogger(FILE_NAME, heliumRecordLog); } - /** - * Change log dir, the dir will be created if not exits - */ - public static void resetLogBaseDir(String baseDir) { - setLogBaseDir(baseDir); - logHandler = makeLogger(FILE_NAME, heliumRecordLog); - } - public static void info(String msg) { LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); heliumRecordLog.log(Level.INFO, msg); 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 3d8e58ff..4e07d758 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 @@ -24,17 +24,24 @@ import java.util.logging.Logger; import com.alibaba.csp.sentinel.util.PidUtil; /** + * Default log base dir is ${user.home}, we can use {@link #LOG_DIR} System property to override it. + * * @author leyou */ public class LogBase { public static final String LOG_CHARSET = "utf-8"; private static final String DIR_NAME = "logs" + File.separator + "csp"; private static final String USER_HOME = "user.home"; + public static final String LOG_DIR = "csp.sentinel.log.dir"; private static String logBaseDir; static { - String userHome = System.getProperty(USER_HOME); - setLogBaseDir(userHome); + // first use -D, then use user home. + String logDir = System.getProperty(LOG_DIR); + if (logDir == null || logDir.isEmpty()) { + logDir = System.getProperty(USER_HOME); + } + setLogBaseDir(logDir); } /** diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java index 3c4db12f..1832759c 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java @@ -33,14 +33,6 @@ public class RecordLog extends LogBase { logHandler = makeLogger(FILE_NAME, heliumRecordLog); } - /** - * Change log dir, the dir will be created if not exits - */ - public static void resetLogBaseDir(String baseDir) { - setLogBaseDir(baseDir); - logHandler = makeLogger(FILE_NAME, heliumRecordLog); - } - public static void info(String detail) { LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); heliumRecordLog.log(Level.INFO, detail); diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java index e6c9935f..9652f217 100755 --- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java +++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java @@ -15,10 +15,15 @@ */ package com.alibaba.csp.sentinel; +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.assertTrue; + /** * @author xuyue */ @@ -38,4 +43,22 @@ public class RecordLogTest { } } + @Test + public void testChangeLogBase() { + String userHome = System.getProperty("user.home"); + String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis(); + System.setProperty(LogBase.LOG_DIR, newLogBase); + + RecordLog.info("testChangeLogBase"); + String logFileName = RecordLog.getLogBaseDir(); + File[] files = new File(logFileName).listFiles(); + assertTrue(files != null && files.length > 0); + } + + @Test + public void testLogBaseDir() { + RecordLog.info("testLogBaseDir"); + assertTrue(RecordLog.getLogBaseDir().startsWith(System.getProperty("user.home"))); + } + } \ No newline at end of file