Signed-off-by: Eric Zhao <sczyh16@gmail.com>master
@@ -24,8 +24,8 @@ import java.util.Properties; | |||
import java.util.concurrent.ConcurrentHashMap; | |||
/** | |||
* The universal local config center of Sentinel. The config is retrieved from command line arguments | |||
* and {@code ${user.home}/logs/csp/${appName}.properties} file by default. | |||
* The universal local configuration center of Sentinel. The config is retrieved from command line arguments | |||
* and customized properties file by default. | |||
* | |||
* @author leyou | |||
* @author Eric Zhao | |||
@@ -49,7 +49,6 @@ public class SentinelConfig { | |||
public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor"; | |||
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt"; | |||
static final String DEFAULT_CHARSET = "UTF-8"; | |||
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50; | |||
static final int DEFAULT_TOTAL_METRIC_FILE_COUNT = 6; | |||
@@ -98,7 +97,6 @@ public class SentinelConfig { | |||
for (Object key : properties.keySet()) { | |||
setConfig((String) key, (String) properties.get(key)); | |||
} | |||
} | |||
/** | |||
@@ -28,22 +28,19 @@ import java.util.concurrent.CopyOnWriteArraySet; | |||
import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; | |||
/** | |||
* <p> | |||
* class responsible for loading the Sentinel Core configuration | |||
* </p> | |||
* <p>The loader that responsible for loading Sentinel common configurations.</p> | |||
* | |||
* @author lianglin | |||
* @since 1.7.0 | |||
*/ | |||
public class SentinelConfigLoader { | |||
public final class SentinelConfigLoader { | |||
public static final String SENTINEL_CONFIG = "csp.sentinel.config.file"; | |||
private static final String DIR_NAME = "logs" + File.separator + "csp"; | |||
private static final String USER_HOME = "user.home"; | |||
public static final String SENTINEL_CONFIG = "csp.sentinel.config.file"; | |||
private static String DEFAULT_SENTINEL_CONFIG_FILE = "classpath:sentinel.properties"; | |||
private static final String DEFAULT_SENTINEL_CONFIG_FILE = "classpath:sentinel.properties"; | |||
private static Properties properties = new Properties(); | |||
@@ -51,9 +48,7 @@ public class SentinelConfigLoader { | |||
load(); | |||
} | |||
private static void load() { | |||
String fileName = System.getProperty(SENTINEL_CONFIG); | |||
if (StringUtil.isBlank(fileName)) { | |||
fileName = DEFAULT_SENTINEL_CONFIG_FILE; | |||
@@ -61,7 +56,7 @@ public class SentinelConfigLoader { | |||
Properties p = ConfigUtil.loadProperties(fileName); | |||
//old version config file | |||
// Compatible with legacy config file path. | |||
if (p == null) { | |||
String path = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator; | |||
fileName = path + AppNameUtil.getAppName() + ".properties"; | |||
@@ -72,6 +67,7 @@ public class SentinelConfigLoader { | |||
} | |||
if (p != null && !p.isEmpty()) { | |||
RecordLog.info("[SentinelConfigLoader] Loading Sentinel config from " + fileName); | |||
properties.putAll(p); | |||
} | |||
@@ -81,7 +77,8 @@ public class SentinelConfigLoader { | |||
String oldConfigValue = properties.getProperty(configKey); | |||
properties.put(configKey, newConfigValue); | |||
if (oldConfigValue != null) { | |||
RecordLog.info("[SentinelConfig] JVM parameter overrides {0}: {1} -> {2}", configKey, oldConfigValue, newConfigValue); | |||
RecordLog.info("[SentinelConfigLoader] JVM parameter overrides {0}: {1} -> {2}", | |||
configKey, oldConfigValue, newConfigValue); | |||
} | |||
} | |||
} | |||
@@ -27,39 +27,43 @@ import java.util.logging.Logger; | |||
import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator; | |||
/** | |||
* Default log base dir is ${user.home}/logs/csp/, we can use {@link #LOG_DIR} System property to override it. | |||
* Default log file name dose not contain pid, but if multi instances of the same app are running in the same | |||
* machine, we may want to distinguish the log file by pid number, in this case, {@link #LOG_NAME_USE_PID} | |||
* System property could be configured as "true" to turn on this switch. | |||
* <p>The base class for logging.</p> | |||
* | |||
* <p> | |||
* The default log base directory is {@code ${user.home}/logs/csp/}. We can use the {@link #LOG_DIR} | |||
* property to override it. The default log file name dose not contain pid, but if multi-instances of the same service | |||
* are running in the same machine, we may want to distinguish the log file by process ID number. | |||
* In this case, {@link #LOG_NAME_USE_PID} property could be configured as "true" to turn on this switch. | |||
* </p> | |||
* | |||
* @author leyou | |||
*/ | |||
public class LogBase { | |||
private static final String DIR_NAME = "logs" + File.separator + "csp"; | |||
public static final String LOG_DIR = "csp.sentinel.log.dir"; | |||
public static final String LOG_NAME_USE_PID = "csp.sentinel.log.use.pid"; | |||
public static final String LOG_OUTPUT_TYPE = "csp.sentinel.log.output.type"; | |||
public static final String LOG_CHARSET = "csp.sentinel.log.charset"; | |||
public static final String USER_HOME = "user.home"; | |||
/** | |||
* Output biz log(RecordLog,CommandCenterLog) to file | |||
* Output biz log (e.g. RecordLog and CommandCenterLog) to file. | |||
*/ | |||
public static final String LOG_OUTPUT_TYPE_FILE = "file"; | |||
/** | |||
* Output biz log(RecordLog,CommandCenterLog) to console | |||
* Output biz log (e.g. RecordLog and CommandCenterLog) to console. | |||
*/ | |||
public static final String LOG_OUTPUT_TYPE_CONSOLE = "console"; | |||
public static final String LOG_CHARSET_UTF8 = "utf-8"; | |||
private static final String DIR_NAME = "logs" + File.separator + "csp"; | |||
private static final String USER_HOME = "user.home"; | |||
private static boolean logNameUsePid; | |||
private static String logOutputType; | |||
private static String logBaseDir; | |||
private static String logCharSet; | |||
static { | |||
try { | |||
initialize(); | |||
@@ -78,15 +82,13 @@ public class LogBase { | |||
} | |||
private static void loadProperties() { | |||
Properties properties = LogConfigLoader.getProperties(); | |||
logOutputType = properties.get(LOG_OUTPUT_TYPE) == null ? logOutputType : properties.getProperty(LOG_OUTPUT_TYPE); | |||
if (!LOG_OUTPUT_TYPE_FILE.equalsIgnoreCase(logOutputType) && !LOG_OUTPUT_TYPE_CONSOLE.equalsIgnoreCase(logOutputType)) { | |||
logOutputType = LOG_OUTPUT_TYPE_FILE; | |||
} | |||
System.out.println("INFO: log out type is: " + logOutputType); | |||
System.out.println("INFO: log output type is: " + logOutputType); | |||
logCharSet = properties.getProperty(LOG_CHARSET) == null ? logCharSet : properties.getProperty(LOG_CHARSET); | |||
System.out.println("INFO: log charset is: " + logCharSet); | |||
@@ -106,48 +108,45 @@ public class LogBase { | |||
String usePid = properties.getProperty(LOG_NAME_USE_PID); | |||
logNameUsePid = "true".equalsIgnoreCase(usePid); | |||
System.out.println("INFO: log name use pid is: " + logNameUsePid); | |||
} | |||
/** | |||
* Whether log file name should contain pid. This switch is configured by {@link #LOG_NAME_USE_PID} System property. | |||
* Whether log file name should contain pid. This switch is configured by {@link #LOG_NAME_USE_PID} system property. | |||
* | |||
* @return if log file name should contain pid, return true, otherwise return false. | |||
* @return true if log file name should contain pid, return true, otherwise false | |||
*/ | |||
public static boolean isLogNameUsePid() { | |||
return logNameUsePid; | |||
} | |||
/** | |||
* Get log file base directory path, the returned path is guaranteed end with {@link File#separator} | |||
* Get the log file base directory path, which is guaranteed ended with {@link File#separator}. | |||
* | |||
* @return log file base directory path. | |||
* @return log file base directory path | |||
*/ | |||
public static String getLogBaseDir() { | |||
return logBaseDir; | |||
} | |||
/** | |||
* Get log file output type the default value is "file" | |||
* Get the log file output type. | |||
* | |||
* @return | |||
* @return log output type, "file" by default | |||
*/ | |||
public static String getLogOutputType() { | |||
return logOutputType; | |||
} | |||
/** | |||
* Get log file charSet the default value is utf-8 | |||
* Get the log file charset. | |||
* | |||
* @return | |||
* @return the log file charset, "utf-8" by default | |||
*/ | |||
public static String getLogCharset() { | |||
return logCharSet; | |||
} | |||
protected static void log(Logger logger, Handler handler, Level level, String detail, Object... params) { | |||
if (detail == null) { | |||
return; | |||
@@ -209,5 +208,4 @@ public class LogBase { | |||
return handler; | |||
} | |||
} |
@@ -23,21 +23,17 @@ import java.util.Properties; | |||
import java.util.concurrent.CopyOnWriteArraySet; | |||
/** | |||
* <p> | |||
* class responsible for loading the Log configuration. | |||
* </p> | |||
* <p>The loader that responsible for loading Sentinel log configurations.</p> | |||
* | |||
* @author lianglin | |||
* @since 1.7.0 | |||
*/ | |||
public class LogConfigLoader { | |||
public static final String LOG_CONFIG = "csp.sentinel.config.file"; | |||
private static final String DEFAULT_LOG_CONFIG_FILE = "classpath:sentinel.properties"; | |||
private static final Properties properties = new Properties(); | |||
static { | |||
@@ -45,7 +41,6 @@ public class LogConfigLoader { | |||
} | |||
private static void load() { | |||
String file = System.getProperty(LOG_CONFIG); | |||
if (StringUtil.isBlank(file)) { | |||
file = DEFAULT_LOG_CONFIG_FILE; | |||
@@ -60,19 +55,11 @@ public class LogConfigLoader { | |||
for (Map.Entry<Object, Object> entry : copy) { | |||
String configKey = entry.getKey().toString(); | |||
String newConfigValue = entry.getValue().toString(); | |||
String oldConfigValue = properties.getProperty(configKey); | |||
properties.put(configKey, newConfigValue); | |||
if (oldConfigValue != null) { | |||
System.out.println("[LogConfig] JVM parameter overrides: " + configKey + " " + oldConfigValue + " -> " + newConfigValue); | |||
} | |||
} | |||
} | |||
public static Properties getProperties() { | |||
return properties; | |||
} | |||
} |
@@ -26,7 +26,7 @@ import java.util.Properties; | |||
/** | |||
* <p> | |||
* util for loading config | |||
* Util class for loading configuration from file or command arguments. | |||
* </p> | |||
* | |||
* @author lianglin | |||
@@ -34,14 +34,14 @@ import java.util.Properties; | |||
*/ | |||
public final class ConfigUtil { | |||
public static final String CLASSPATH_FILE_FLAG = "classpath:"; | |||
/** | |||
* Return null if the file not exist | |||
* <p>Load the properties from provided file.</p> | |||
* <p>Currently it supports reading from classpath file or local file.</p> | |||
* | |||
* @param fileName | |||
* @return | |||
* @param fileName valid file path | |||
* @return the retrieved properties from the file; null if the file not exist | |||
*/ | |||
public static Properties loadProperties(String fileName) { | |||
if (StringUtil.isNotBlank(fileName)) { | |||
@@ -66,12 +66,9 @@ public final class ConfigUtil { | |||
return null; | |||
} | |||
FileInputStream input = new FileInputStream(file); | |||
try { | |||
try (FileInputStream input = new FileInputStream(file)) { | |||
properties = new Properties(); | |||
properties.load(input); | |||
} finally { | |||
input.close(); | |||
} | |||
} catch (Throwable e) { | |||
e.printStackTrace(); | |||
@@ -81,13 +78,12 @@ public final class ConfigUtil { | |||
private static Properties loadPropertiesFromClasspathFile(String fileName) { | |||
fileName = fileName.substring(CLASSPATH_FILE_FLAG.length()).trim(); | |||
List<URL> list = new ArrayList<URL>(); | |||
List<URL> list = new ArrayList<>(); | |||
try { | |||
Enumeration<URL> urls = getClassLoader().getResources(fileName); | |||
list = new ArrayList<URL>(); | |||
list = new ArrayList<>(); | |||
while (urls.hasMoreElements()) { | |||
list.add(urls.nextElement()); | |||
} | |||
@@ -143,5 +139,5 @@ public final class ConfigUtil { | |||
return dir; | |||
} | |||
private ConfigUtil() {} | |||
} |