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