support specify log file path

This commit is contained in:
金戟 2021-04-05 14:09:44 +08:00
parent 01e9890234
commit a46e73a421
4 changed files with 56 additions and 18 deletions

View File

@ -17,6 +17,7 @@ public class PreMain {
private static final String AND = "&";
private static final String USE_THREAD_POOL = "useThreadPool";
private static final String LOG_LEVEL = "logLevel";
private static final String LOG_FILE = "logFile";
private static final String DUMP_PATH = "dumpPath";
private static final String PKG_PREFIX = "pkgPrefix";
private static final String MOCK_SCOPE = "mockScope";
@ -24,8 +25,8 @@ public class PreMain {
private static boolean enhanceThreadLocal = false;
public static void premain(String agentArgs, Instrumentation inst) {
GlobalConfig.setupLogRootPath();
parseArgs(agentArgs);
GlobalConfig.setupLogRootPath();
if (enhanceThreadLocal) {
// add transmittable thread local transformer
TtlAgent.premain(agentArgs, inst);
@ -47,6 +48,8 @@ public class PreMain {
String v = a.substring(i + 1);
if (k.equals(LOG_LEVEL)) {
GlobalConfig.setLogLevel(v);
} else if (k.equals(LOG_FILE)) {
GlobalConfig.setLogFile(v);
} else if (k.equals(DUMP_PATH)) {
GlobalConfig.setDumpPath(v);
} else if (k.equals(PKG_PREFIX)) {

View File

@ -15,23 +15,27 @@ public class GlobalConfig {
private static final String DEBUG = "debug";
private static final String VERBOSE = "verbose";
private static final String USER_DIR = "user.dir";
private static final String DISABLE_LOG_FILE = "null";
private static final String TESTABLE_AGENT_LOG = "testable-agent.log";
private static String logFile = null;
private static String dumpPath = null;
private static String pkgPrefix = null;
private static MockScope defaultMockScope = MockScope.GLOBAL;
public static boolean setLogLevel(String level) {
public static void setLogLevel(String level) {
if (level.equals(MUTE)) {
LogUtil.setDefaultLevel(LogLevel.DISABLE);
return true;
} else if (level.equals(DEBUG)) {
LogUtil.setDefaultLevel(LogLevel.ENABLE);
return true;
} else if (level.equals(VERBOSE)) {
LogUtil.setDefaultLevel(LogLevel.VERBOSE);
return true;
}
return false;
}
public static void setLogFile(String path) {
logFile = path;
}
public static String getDumpPath() {
@ -59,7 +63,14 @@ public class GlobalConfig {
}
public static void setupLogRootPath() {
LogUtil.setGlobalLogPath(
PathUtil.getFirstLevelFolder(System.getProperty(USER_DIR), Object.class.getResource("/").getPath()));
if (logFile == null) {
String baseFolder = PathUtil.getFirstLevelFolder(System.getProperty(USER_DIR),
Object.class.getResource("/").getPath());
if (!baseFolder.isEmpty()) {
LogUtil.setGlobalLogPath(PathUtil.join(baseFolder, TESTABLE_AGENT_LOG));
}
} else if (!DISABLE_LOG_FILE.equals(logFile)) {
LogUtil.setGlobalLogPath(PathUtil.join(System.getProperty(USER_DIR), logFile));
}
}
}

View File

@ -2,7 +2,6 @@ package com.alibaba.testable.core.util;
import com.alibaba.testable.core.model.LogLevel;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@ -13,8 +12,6 @@ import java.util.Date;
*/
public class LogUtil {
private static final String TESTABLE_AGENT_LOG = "testable-agent.log";
private static LogLevel defaultLogLevel = LogLevel.DEFAULT;
private static LogLevel currentLogLevel = LogLevel.DEFAULT;
private static FileOutputStream logFileStream = null;
@ -73,14 +70,12 @@ public class LogUtil {
currentLogLevel = defaultLogLevel;
}
public static void setGlobalLogPath(String logFolderPath) {
if (logFolderPath.isEmpty()) {
return;
}
String logFilePath = logFolderPath + File.separator + TESTABLE_AGENT_LOG;
public static void setGlobalLogPath(String logFilePath) {
try {
logFileStream = new FileOutputStream(logFilePath);
diagnose("Start at %s", new Date().toString());
if (PathUtil.createFolder(PathUtil.getFolder(logFilePath))) {
logFileStream = new FileOutputStream(logFilePath);
diagnose("Start at %s", new Date().toString());
}
} catch (FileNotFoundException e) {
warn("Failed to create log file %s", logFilePath);
}

View File

@ -0,0 +1,29 @@
package com.alibaba.testable.core.util;
import java.io.File;
public class PathUtil {
/**
* Create folder recursively
* @param folderPath folder to create
* @return whether creation success
*/
public static boolean createFolder(String folderPath) {
File folder = new File(folderPath);
if (folder.isDirectory()) {
return true;
}
return folder.mkdirs();
}
/**
* Get parent folder of specified path
* @param path path of file or folder
* @return parent folder path
*/
public static String getFolder(String path) {
return new File(path).getParent();
}
}