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 1 commit
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,13 @@ 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) {
logLevel = Level.parse(logLevelString);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the logLevel is invalid?

}
System.out.println("INFO: Sentinel log level is: " + logLevel);
}


Expand Down Expand Up @@ -142,4 +154,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.node.metric.MetricTimerListener;
import com.alibaba.csp.sentinel.property.DynamicSentinelProperty;
Expand All @@ -32,6 +33,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please remove the unused import?


/**
* <p>
Expand Down Expand Up @@ -80,7 +82,9 @@ private static void startMetricTimerListener() {
SentinelConfig.METRIC_FLUSH_INTERVAL);
return;
}
SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, flushInterval, TimeUnit.SECONDS);
if (!LogBase.getLogLevel().equals(Level.OFF)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metric的写日志逻辑实际已经通过csp.sentinel.metric.flush.interval参数进行控制了,这个日志打印我觉得更应该在日志层面调用去控制,而不是通过日志级别来控制metricTimerListener的启用。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,明白了,我调整一下

SCHEDULER.scheduleAtFixedRate(new MetricTimerListener(), 0, flushInterval, TimeUnit.SECONDS);
}
}

/**
Expand Down