Selaa lähdekoodia

Use System property to config log base dir

Signed-off-by: Carpenter Lee <hooleeucas@163.com>
master
Carpenter Lee 6 vuotta sitten
vanhempi
commit
4610bd3da6
4 muutettua tiedostoa jossa 32 lisäystä ja 18 poistoa
  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 Näytä tiedosto

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


+ 9
- 2
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java Näytä tiedosto

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

/**


+ 0
- 8
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/RecordLog.java Näytä tiedosto

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


+ 23
- 0
sentinel-core/src/test/java/com/alibaba/csp/sentinel/RecordLogTest.java Näytä tiedosto

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

}

Loading…
Peruuta
Tallenna