Add log level configuration support for common logs (#2514)

* Add `csp.sentinel.log.level` property to support logging level for record logs (not for block log and metric log)
This commit is contained in:
zhaoxinhu 2022-01-14 17:22:01 +08:00 committed by GitHub
parent 2df682dad2
commit 794530f533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.log;
import java.io.File;
import java.util.Properties;
import java.util.logging.Level;
import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator;
@ -40,6 +41,7 @@ public class LogBase {
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 LOG_LEVEL = "csp.sentinel.log.level";
/**
* Output biz log (e.g. RecordLog and CommandCenterLog) to file.
@ -53,12 +55,14 @@ public class LogBase {
private static final String DIR_NAME = "logs" + File.separator + "csp";
private static final String USER_HOME = "user.home";
private static final Level LOG_DEFAULT_LEVEL = Level.INFO;
private static boolean logNameUsePid;
private static String logOutputType;
private static String logBaseDir;
private static String logCharSet;
private static Level logLevel;
static {
try {
@ -75,6 +79,7 @@ public class LogBase {
logOutputType = LOG_OUTPUT_TYPE_FILE;
logBaseDir = addSeparator(System.getProperty(USER_HOME)) + DIR_NAME + File.separator;
logCharSet = LOG_CHARSET_UTF8;
logLevel = LOG_DEFAULT_LEVEL;
}
private static void loadProperties() {
@ -103,6 +108,17 @@ public class LogBase {
String usePid = properties.getProperty(LOG_NAME_USE_PID);
logNameUsePid = "true".equalsIgnoreCase(usePid);
System.out.println("INFO: Sentinel log name use pid is: " + logNameUsePid);
// load log level
String logLevelString = properties.getProperty(LOG_LEVEL);
if (logLevelString != null && (logLevelString = logLevelString.trim()).length() > 0) {
try {
logLevel = Level.parse(logLevelString);
} catch (IllegalArgumentException e) {
System.out.println("Log level : " + logLevel + " is invalid. Use default : " + LOG_DEFAULT_LEVEL.toString());
}
}
System.out.println("INFO: Sentinel log level is: " + logLevel);
}
@ -142,4 +158,7 @@ public class LogBase {
return logCharSet;
}
public static Level getLogLevel() {
return logLevel;
}
}

View File

@ -70,6 +70,7 @@ public class BaseJulLogger {
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true);
handler.setFormatter(formatter);
handler.setEncoding(logCharSet);
handler.setLevel(LogBase.getLogLevel());
} catch (IOException e) {
e.printStackTrace();
}
@ -79,6 +80,7 @@ public class BaseJulLogger {
handler = new ConsoleHandler();
handler.setFormatter(formatter);
handler.setEncoding(logCharSet);
handler.setLevel(LogBase.getLogLevel());
} catch (IOException e) {
e.printStackTrace();
}
@ -92,7 +94,7 @@ public class BaseJulLogger {
}
// Set log level to INFO by default
heliumRecordLog.setLevel(Level.INFO);
heliumRecordLog.setLevel(LogBase.getLogLevel());
return handler;
}