Skip to content

Commit

Permalink
Change eagleEye log path when log base configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarpenterLee committed Oct 8, 2018
1 parent 4610bd3 commit 8554022
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ private T configLogFilePath(String filePathToConfig, String basePath) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
*/
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -53,23 +71,6 @@ public static String getLogBaseDir() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 8554022

Please sign in to comment.