浏览代码

Use System property to config log base dir

Signed-off-by: Carpenter Lee <hooleeucas@163.com>
master
Carpenter Lee 6 年前
父节点
当前提交
4610bd3da6
共有 4 个文件被更改,包括 32 次插入18 次删除
  1. +0
    -8
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/CommandCenterLog.java
  2. +9
    -2
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java
  3. +0
    -8
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java
  4. +23
    -0
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java

+ 0
- 8
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); 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);


+ 9
- 2
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; 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);
} }


/** /**


+ 0
- 8
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); 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);


+ 23
- 0
sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java 查看文件

@@ -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")));
}

} }

正在加载...
取消
保存