Skip to content

新增 csp.sentinel.log.level 启动配置项,可设置为OFF,进行关闭日志 #2514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.util.Properties;
import java.util.logging.Level;

import static com.alibaba.csp.sentinel.util.ConfigUtil.addSeparator;

Expand All @@ -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.
Expand All @@ -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 {
Expand All @@ -75,6 +79,7 @@ private static void initializeDefault() {
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() {
Expand Down Expand Up @@ -103,6 +108,17 @@ private static void loadProperties() {
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);
}


Expand Down Expand Up @@ -142,4 +158,7 @@ public static String getLogCharset() {
return logCharSet;
}

public static Level getLogLevel() {
return logLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected Handler makeLoggingHandler(String logName, Logger heliumRecordLog) {
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();
}
Expand All @@ -79,6 +80,7 @@ protected Handler makeLoggingHandler(String logName, Logger heliumRecordLog) {
handler = new ConsoleHandler();
handler.setFormatter(formatter);
handler.setEncoding(logCharSet);
handler.setLevel(LogBase.getLogLevel());
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -92,7 +94,7 @@ protected Handler makeLoggingHandler(String logName, Logger heliumRecordLog) {
}

// Set log level to INFO by default
heliumRecordLog.setLevel(Level.INFO);
heliumRecordLog.setLevel(LogBase.getLogLevel());
return handler;
}

Expand Down