@@ -53,6 +53,13 @@ class BaseLoggerBuilder<T extends BaseLoggerBuilder<T>> { | |||||
return (T)this; | return (T)this; | ||||
} | } | ||||
@SuppressWarnings("unchecked") | |||||
public T configLogFilePath(String filePath) { | |||||
EagleEyeCoreUtils.checkNotNullEmpty(filePath, "filePath"); | |||||
this.filePath = filePath; | |||||
return (T)this; | |||||
} | |||||
@SuppressWarnings("unchecked") | @SuppressWarnings("unchecked") | ||||
public T maxFileSizeMB(long maxFileSizeMB) { | public T maxFileSizeMB(long maxFileSizeMB) { | ||||
if (maxFileSize < 10) { | if (maxFileSize < 10) { | ||||
@@ -24,8 +24,8 @@ import java.util.logging.Logger; | |||||
*/ | */ | ||||
public class CommandCenterLog extends LogBase { | 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; | private static Handler logHandler = null; | ||||
static { | static { | ||||
@@ -38,10 +38,28 @@ public class LogBase { | |||||
static { | static { | ||||
// first use -D, then use user home. | // first use -D, then use user home. | ||||
String logDir = System.getProperty(LOG_DIR); | String logDir = System.getProperty(LOG_DIR); | ||||
if (logDir == null || logDir.isEmpty()) { | if (logDir == null || logDir.isEmpty()) { | ||||
logDir = System.getProperty(USER_HOME); | 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; | 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) { | protected static Handler makeLogger(String logName, Logger heliumRecordLog) { | ||||
CspFormatter formatter = new CspFormatter(); | CspFormatter formatter = new CspFormatter(); | ||||
String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid(); | String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid(); | ||||
@@ -15,28 +15,26 @@ | |||||
*/ | */ | ||||
package com.alibaba.csp.sentinel.slots.logger; | package com.alibaba.csp.sentinel.slots.logger; | ||||
import java.io.File; | |||||
import com.alibaba.csp.sentinel.eagleeye.EagleEye; | import com.alibaba.csp.sentinel.eagleeye.EagleEye; | ||||
import com.alibaba.csp.sentinel.eagleeye.StatLogger; | import com.alibaba.csp.sentinel.eagleeye.StatLogger; | ||||
import com.alibaba.csp.sentinel.log.LogBase; | |||||
public class EagleEyeLogUtil { | 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; | private static StatLogger statLogger; | ||||
static { | 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) | .intervalSeconds(1) | ||||
.entryDelimiter('|') | .entryDelimiter('|') | ||||
.keyDelimiter(',') | .keyDelimiter(',') | ||||
.valueDelimiter(',') | .valueDelimiter(',') | ||||
.maxEntryCount(6000) | .maxEntryCount(6000) | ||||
.baseLogFilePath(path) | |||||
.configLogFilePath(path) | |||||
.maxFileSizeMB(300) | .maxFileSizeMB(300) | ||||
.maxBackupIndex(3) | .maxBackupIndex(3) | ||||
.buildSingleton(); | .buildSingleton(); | ||||
@@ -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()); | |||||
} | |||||
} |