优化LoggingSearchHandler

This commit is contained in:
Redkale
2022-12-11 19:24:13 +08:00
parent 93ce83f137
commit b6a0885927
4 changed files with 46 additions and 37 deletions

View File

@@ -254,8 +254,17 @@ public final class Application {
this.config = config;
this.configFromCache = "true".equals(config.getValue("[config-from-cache]"));
this.environment = new Environment(this.envProperties);
System.setProperty("redkale.version", Redkale.getDotedVersion());
{ //设置系统变量
System.setProperty("redkale.version", Redkale.getDotedVersion());
int nid = config.getIntValue("nodeid", 0);
this.nodeid = nid;
this.resourceFactory.register(RESNAME_APP_NODEID, nid);
System.setProperty(RESNAME_APP_NODEID, "" + nid);
this.name = checkName(config.getValue("name", ""));
this.resourceFactory.register(RESNAME_APP_NAME, name);
System.setProperty(RESNAME_APP_NAME, name);
}
final File root = new File(System.getProperty(RESNAME_APP_HOME));
this.resourceFactory.register(RESNAME_APP_TIME, long.class, this.startTime);
this.resourceFactory.register(RESNAME_APP_HOME, Path.class, root.toPath());
@@ -299,16 +308,7 @@ public final class Application {
this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath());
}
this.resourceFactory.register(Environment.class, environment);
{ //设置系统变量
int nid = config.getIntValue("nodeid", 0);
this.nodeid = nid;
this.resourceFactory.register(RESNAME_APP_NODEID, nid);
System.setProperty(RESNAME_APP_NODEID, "" + nid);
this.name = checkName(config.getValue("name", ""));
this.resourceFactory.register(RESNAME_APP_NAME, name);
System.setProperty(RESNAME_APP_NAME, name);
}
{ //初始化ClassLoader
ClassLoader currClassLoader = Thread.currentThread().getContextClassLoader();
if (currClassLoader instanceof RedkaleClassLoader) {
@@ -878,7 +878,7 @@ public final class Application {
properties.setProperty("java.util.logging.ConsoleHandler.formatter", LoggingFileHandler.LoggingFormater.class.getName());
}
}
if (properties.getProperty("java.util.logging.ConsoleHandler.denyreg") != null && !compileMode) {
if (!compileMode) { //ConsoleHandler替换成LoggingConsoleHandler
final String handlers = properties.getProperty("handlers");
if (handlers != null && handlers.contains("java.util.logging.ConsoleHandler")) {
final String consoleHandlerClass = LoggingFileHandler.LoggingConsoleHandler.class.getName();
@@ -1530,7 +1530,7 @@ public final class Application {
String ms = String.valueOf(intms);
int repeat = ms.length() > 7 ? 0 : (7 - ms.length()) / 2;
logger.info(colorMessage(logger, 36, 1, "-".repeat(repeat) + "------------------------ Redkale started in " + ms + " ms " + (ms.length() / 2 == 0 ? " " : "") + "-".repeat(repeat) + "------------------------") + "\r\n");
LoggingFileHandler.traceflag = true;
LoggingBaseHandler.traceflag = true;
for (ApplicationListener listener : this.listeners) {
listener.postStart(this);

View File

@@ -2,7 +2,8 @@
*/
package org.redkale.boot;
import java.util.logging.Handler;
import java.util.logging.*;
import org.redkale.util.Traces;
/**
* Handler基类
@@ -14,4 +15,21 @@ import java.util.logging.Handler;
*/
public abstract class LoggingBaseHandler extends Handler {
static boolean traceflag = false; //防止设置system.property前调用Traces类导致enable提前初始化
protected static void fillLogRecord(LogRecord log) {
if (traceflag && Traces.enable()) {
String traceid = Traces.currTraceid();
if (traceid == null || traceid.isEmpty()) {
traceid = "[TID:N/A] ";
} else {
traceid = "[TID:" + traceid + "] ";
}
if (log.getMessage() == null) {
log.setMessage(traceid);
} else {
log.setMessage(traceid + log.getMessage());
}
}
}
}

View File

@@ -31,8 +31,6 @@ public class LoggingFileHandler extends LoggingBaseHandler {
//public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s%n%5$s%6$s%n";
public static final String FORMATTER_FORMAT = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%tL %4$s %2$s\r\n%5$s%6$s\r\n";
static boolean traceflag = false; //防止设置system.property前调用Traces类导致enable提前初始化
/**
* SNCP的日志输出Handler
*/
@@ -46,7 +44,7 @@ public class LoggingFileHandler extends LoggingBaseHandler {
public static class LoggingConsoleHandler extends ConsoleHandler {
private Pattern denyreg;
private Pattern denyRegx;
public LoggingConsoleHandler() {
super();
@@ -56,10 +54,13 @@ public class LoggingFileHandler extends LoggingBaseHandler {
private void configure() {
LogManager manager = LogManager.getLogManager();
String denyregstr = manager.getProperty("java.util.logging.ConsoleHandler.denyreg");
String denyregstr = manager.getProperty(LoggingConsoleHandler.class.getName() + ".denyreg");
if (denyregstr == null) {
denyregstr = manager.getProperty("java.util.logging.ConsoleHandler.denyreg");
}
try {
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
denyreg = Pattern.compile(denyregstr);
this.denyRegx = Pattern.compile(denyregstr);
}
} catch (Exception e) {
}
@@ -67,20 +68,8 @@ public class LoggingFileHandler extends LoggingBaseHandler {
@Override
public void publish(LogRecord log) {
if (denyreg != null && denyreg.matcher(log.getMessage()).find()) return;
if (traceflag && Traces.enable()) {
String traceid = Traces.currTraceid();
if (traceid == null || traceid.isEmpty()) {
traceid = "[TID:N/A] ";
} else {
traceid = "[TID:" + traceid + "] ";
}
if (log.getMessage() == null) {
log.setMessage(traceid);
} else {
log.setMessage(traceid + log.getMessage());
}
}
if (denyRegx != null && denyRegx.matcher(log.getMessage()).find()) return;
fillLogRecord(log);
super.publish(log);
}
}

View File

@@ -10,6 +10,7 @@ import java.util.logging.*;
import java.util.logging.Formatter;
import java.util.regex.Pattern;
import javax.persistence.*;
import static org.redkale.boot.Application.RESNAME_APP_NAME;
import org.redkale.convert.*;
import org.redkale.convert.json.JsonConvert;
import org.redkale.source.*;
@@ -37,7 +38,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
protected String pattern;
protected Pattern denyreg;
protected Pattern denyRegx;
protected String sourceResourceName;
@@ -134,8 +135,8 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
String tagstr = manager.getProperty(cname + ".tag");
if (tagstr != null && !tagstr.isEmpty()) {
if (!checkTagName(tagstr.replaceAll("\\$\\{.+\\}", ""))) throw new RuntimeException("found illegal logging.property " + cname + ".tag = " + tagstr);
this.tag = tagstr;
if (tagstr.contains("%")) {
this.tag = tagstr.replace("${" + RESNAME_APP_NAME + "}", System.getProperty(RESNAME_APP_NAME, ""));
if (this.tag.contains("%")) {
this.tagDateFormat = this.tag;
Utility.formatTime(this.tagDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确
}
@@ -178,7 +179,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
String denyregstr = manager.getProperty(cname + ".denyreg");
try {
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
denyreg = Pattern.compile(denyregstr);
denyRegx = Pattern.compile(denyregstr);
}
} catch (Exception e) {
}
@@ -197,8 +198,9 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
break;
}
}
if (denyreg != null && denyreg.matcher(log.getMessage()).find()) return;
if (denyRegx != null && denyRegx.matcher(log.getMessage()).find()) return;
String rawTag = tagDateFormat == null ? tag : Utility.formatTime(tagDateFormat, -1, log.getInstant().toEpochMilli());
fillLogRecord(log);
logqueue.offer(new SearchLogRecord(rawTag, log));
}