Signed-off-by: Carpenter Lee <hooleeucas@163.com>master
@@ -32,14 +32,6 @@ public class CommandCenterLog extends LogBase { | |||||
logHandler = makeLogger(FILE_NAME, heliumRecordLog); | 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) { | public static void info(String msg) { | ||||
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); | LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); | ||||
heliumRecordLog.log(Level.INFO, msg); | heliumRecordLog.log(Level.INFO, msg); | ||||
@@ -24,17 +24,24 @@ import java.util.logging.Logger; | |||||
import com.alibaba.csp.sentinel.util.PidUtil; | 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 | * @author leyou | ||||
*/ | */ | ||||
public class LogBase { | public class LogBase { | ||||
public static final String LOG_CHARSET = "utf-8"; | public static final String LOG_CHARSET = "utf-8"; | ||||
private static final String DIR_NAME = "logs" + File.separator + "csp"; | private static final String DIR_NAME = "logs" + File.separator + "csp"; | ||||
private static final String USER_HOME = "user.home"; | private static final String USER_HOME = "user.home"; | ||||
public static final String LOG_DIR = "csp.sentinel.log.dir"; | |||||
private static String logBaseDir; | private static String logBaseDir; | ||||
static { | 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); | |||||
} | } | ||||
/** | /** | ||||
@@ -33,14 +33,6 @@ public class RecordLog extends LogBase { | |||||
logHandler = makeLogger(FILE_NAME, heliumRecordLog); | 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) { | public static void info(String detail) { | ||||
LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); | LoggerUtils.disableOtherHandlers(heliumRecordLog, logHandler); | ||||
heliumRecordLog.log(Level.INFO, detail); | heliumRecordLog.log(Level.INFO, detail); | ||||
@@ -15,10 +15,15 @@ | |||||
*/ | */ | ||||
package com.alibaba.csp.sentinel; | package com.alibaba.csp.sentinel; | ||||
import java.io.File; | |||||
import com.alibaba.csp.sentinel.log.LogBase; | |||||
import com.alibaba.csp.sentinel.log.RecordLog; | import com.alibaba.csp.sentinel.log.RecordLog; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import static org.junit.Assert.assertTrue; | |||||
/** | /** | ||||
* @author xuyue | * @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"))); | |||||
} | |||||
} | } |