优化LoggingSearchHandler
This commit is contained in:
@@ -254,8 +254,17 @@ public final class Application {
|
|||||||
this.config = config;
|
this.config = config;
|
||||||
this.configFromCache = "true".equals(config.getValue("[config-from-cache]"));
|
this.configFromCache = "true".equals(config.getValue("[config-from-cache]"));
|
||||||
this.environment = new Environment(this.envProperties);
|
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));
|
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_TIME, long.class, this.startTime);
|
||||||
this.resourceFactory.register(RESNAME_APP_HOME, Path.class, root.toPath());
|
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(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath());
|
||||||
}
|
}
|
||||||
this.resourceFactory.register(Environment.class, environment);
|
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
|
||||||
ClassLoader currClassLoader = Thread.currentThread().getContextClassLoader();
|
ClassLoader currClassLoader = Thread.currentThread().getContextClassLoader();
|
||||||
if (currClassLoader instanceof RedkaleClassLoader) {
|
if (currClassLoader instanceof RedkaleClassLoader) {
|
||||||
@@ -878,7 +878,7 @@ public final class Application {
|
|||||||
properties.setProperty("java.util.logging.ConsoleHandler.formatter", LoggingFileHandler.LoggingFormater.class.getName());
|
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");
|
final String handlers = properties.getProperty("handlers");
|
||||||
if (handlers != null && handlers.contains("java.util.logging.ConsoleHandler")) {
|
if (handlers != null && handlers.contains("java.util.logging.ConsoleHandler")) {
|
||||||
final String consoleHandlerClass = LoggingFileHandler.LoggingConsoleHandler.class.getName();
|
final String consoleHandlerClass = LoggingFileHandler.LoggingConsoleHandler.class.getName();
|
||||||
@@ -1530,7 +1530,7 @@ public final class Application {
|
|||||||
String ms = String.valueOf(intms);
|
String ms = String.valueOf(intms);
|
||||||
int repeat = ms.length() > 7 ? 0 : (7 - ms.length()) / 2;
|
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");
|
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) {
|
for (ApplicationListener listener : this.listeners) {
|
||||||
listener.postStart(this);
|
listener.postStart(this);
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.boot;
|
package org.redkale.boot;
|
||||||
|
|
||||||
import java.util.logging.Handler;
|
import java.util.logging.*;
|
||||||
|
import org.redkale.util.Traces;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handler基类
|
* Handler基类
|
||||||
@@ -14,4 +15,21 @@ import java.util.logging.Handler;
|
|||||||
*/
|
*/
|
||||||
public abstract class LoggingBaseHandler extends 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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%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";
|
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
|
* SNCP的日志输出Handler
|
||||||
*/
|
*/
|
||||||
@@ -46,7 +44,7 @@ public class LoggingFileHandler extends LoggingBaseHandler {
|
|||||||
|
|
||||||
public static class LoggingConsoleHandler extends ConsoleHandler {
|
public static class LoggingConsoleHandler extends ConsoleHandler {
|
||||||
|
|
||||||
private Pattern denyreg;
|
private Pattern denyRegx;
|
||||||
|
|
||||||
public LoggingConsoleHandler() {
|
public LoggingConsoleHandler() {
|
||||||
super();
|
super();
|
||||||
@@ -56,10 +54,13 @@ public class LoggingFileHandler extends LoggingBaseHandler {
|
|||||||
|
|
||||||
private void configure() {
|
private void configure() {
|
||||||
LogManager manager = LogManager.getLogManager();
|
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 {
|
try {
|
||||||
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
|
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
|
||||||
denyreg = Pattern.compile(denyregstr);
|
this.denyRegx = Pattern.compile(denyregstr);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -67,20 +68,8 @@ public class LoggingFileHandler extends LoggingBaseHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void publish(LogRecord log) {
|
public void publish(LogRecord log) {
|
||||||
if (denyreg != null && denyreg.matcher(log.getMessage()).find()) return;
|
if (denyRegx != null && denyRegx.matcher(log.getMessage()).find()) return;
|
||||||
if (traceflag && Traces.enable()) {
|
fillLogRecord(log);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
super.publish(log);
|
super.publish(log);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import java.util.logging.*;
|
|||||||
import java.util.logging.Formatter;
|
import java.util.logging.Formatter;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import static org.redkale.boot.Application.RESNAME_APP_NAME;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import org.redkale.convert.json.JsonConvert;
|
import org.redkale.convert.json.JsonConvert;
|
||||||
import org.redkale.source.*;
|
import org.redkale.source.*;
|
||||||
@@ -37,7 +38,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
|
|||||||
|
|
||||||
protected String pattern;
|
protected String pattern;
|
||||||
|
|
||||||
protected Pattern denyreg;
|
protected Pattern denyRegx;
|
||||||
|
|
||||||
protected String sourceResourceName;
|
protected String sourceResourceName;
|
||||||
|
|
||||||
@@ -134,8 +135,8 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
|
|||||||
String tagstr = manager.getProperty(cname + ".tag");
|
String tagstr = manager.getProperty(cname + ".tag");
|
||||||
if (tagstr != null && !tagstr.isEmpty()) {
|
if (tagstr != null && !tagstr.isEmpty()) {
|
||||||
if (!checkTagName(tagstr.replaceAll("\\$\\{.+\\}", ""))) throw new RuntimeException("found illegal logging.property " + cname + ".tag = " + tagstr);
|
if (!checkTagName(tagstr.replaceAll("\\$\\{.+\\}", ""))) throw new RuntimeException("found illegal logging.property " + cname + ".tag = " + tagstr);
|
||||||
this.tag = tagstr;
|
this.tag = tagstr.replace("${" + RESNAME_APP_NAME + "}", System.getProperty(RESNAME_APP_NAME, ""));
|
||||||
if (tagstr.contains("%")) {
|
if (this.tag.contains("%")) {
|
||||||
this.tagDateFormat = this.tag;
|
this.tagDateFormat = this.tag;
|
||||||
Utility.formatTime(this.tagDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确
|
Utility.formatTime(this.tagDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确
|
||||||
}
|
}
|
||||||
@@ -178,7 +179,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
|
|||||||
String denyregstr = manager.getProperty(cname + ".denyreg");
|
String denyregstr = manager.getProperty(cname + ".denyreg");
|
||||||
try {
|
try {
|
||||||
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
|
if (denyregstr != null && !denyregstr.trim().isEmpty()) {
|
||||||
denyreg = Pattern.compile(denyregstr);
|
denyRegx = Pattern.compile(denyregstr);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -197,8 +198,9 @@ public class LoggingSearchHandler extends LoggingBaseHandler {
|
|||||||
break;
|
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());
|
String rawTag = tagDateFormat == null ? tag : Utility.formatTime(tagDateFormat, -1, log.getInstant().toEpochMilli());
|
||||||
|
fillLogRecord(log);
|
||||||
logqueue.offer(new SearchLogRecord(rawTag, log));
|
logqueue.offer(new SearchLogRecord(rawTag, log));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user