增加 java.util.logging.FileHandler.unusual 特性
This commit is contained in:
@@ -16,6 +16,8 @@ java.util.logging.FileHandler.limit = 10485760
|
|||||||
java.util.logging.FileHandler.count = 100
|
java.util.logging.FileHandler.count = 100
|
||||||
java.util.logging.FileHandler.encoding = UTF-8
|
java.util.logging.FileHandler.encoding = UTF-8
|
||||||
java.util.logging.FileHandler.pattern = ${APP_HOME}/logs-%m/log-%u.log
|
java.util.logging.FileHandler.pattern = ${APP_HOME}/logs-%m/log-%u.log
|
||||||
|
#java.util.logging.FileHandler.unusual \u5c5e\u6027\u8868\u793a\u5c06 WARNING\u3001SEVERE \u7ea7\u522b\u7684\u65e5\u5fd7\u590d\u5236\u5199\u5165\u5355\u72ec\u7684\u6587\u4ef6\u4e2d
|
||||||
|
#java.util.logging.FileHandler.unusual = ${APP_HOME}/logs-%m/log-error-%u.log
|
||||||
java.util.logging.FileHandler.append = true
|
java.util.logging.FileHandler.append = true
|
||||||
|
|
||||||
#java.util.logging.ConsoleHandler.level = FINE
|
#java.util.logging.ConsoleHandler.level = FINE
|
||||||
|
|||||||
@@ -84,9 +84,13 @@ public class LogFileHandler extends Handler {
|
|||||||
|
|
||||||
private String pattern;
|
private String pattern;
|
||||||
|
|
||||||
|
private String unusual; //不为null表示将 WARNING、SEVERE 级别的日志写入单独的文件中
|
||||||
|
|
||||||
private int limit; //文件大小限制
|
private int limit; //文件大小限制
|
||||||
|
|
||||||
private final AtomicInteger index = new AtomicInteger();
|
private final AtomicInteger logindex = new AtomicInteger();
|
||||||
|
|
||||||
|
private final AtomicInteger logunusualindex = new AtomicInteger();
|
||||||
|
|
||||||
private int count = 1; //文件限制
|
private int count = 1; //文件限制
|
||||||
|
|
||||||
@@ -94,11 +98,17 @@ public class LogFileHandler extends Handler {
|
|||||||
|
|
||||||
private boolean append;
|
private boolean append;
|
||||||
|
|
||||||
private final AtomicLong length = new AtomicLong();
|
private final AtomicLong loglength = new AtomicLong();
|
||||||
|
|
||||||
|
private final AtomicLong logunusuallength = new AtomicLong();
|
||||||
|
|
||||||
private File logfile;
|
private File logfile;
|
||||||
|
|
||||||
private OutputStream stream;
|
private File logunusualfile;
|
||||||
|
|
||||||
|
private OutputStream logstream;
|
||||||
|
|
||||||
|
private OutputStream logunusualstream;
|
||||||
|
|
||||||
public LogFileHandler() {
|
public LogFileHandler() {
|
||||||
updateTomorrow();
|
updateTomorrow();
|
||||||
@@ -114,7 +124,7 @@ public class LogFileHandler extends Handler {
|
|||||||
cal.set(Calendar.MILLISECOND, 0);
|
cal.set(Calendar.MILLISECOND, 0);
|
||||||
cal.add(Calendar.DAY_OF_YEAR, 1);
|
cal.add(Calendar.DAY_OF_YEAR, 1);
|
||||||
long t = cal.getTimeInMillis();
|
long t = cal.getTimeInMillis();
|
||||||
if (this.tomorrow != t) index.set(0);
|
if (this.tomorrow != t) logindex.set(0);
|
||||||
this.tomorrow = t;
|
this.tomorrow = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,35 +141,59 @@ public class LogFileHandler extends Handler {
|
|||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
LogRecord record = records.take();
|
LogRecord record = records.take();
|
||||||
final boolean bigger = (limit > 0 && limit <= length.get());
|
final boolean bigger = (limit > 0 && limit <= loglength.get());
|
||||||
if (bigger || tomorrow <= record.getMillis()) {
|
final boolean changeday = tomorrow <= record.getMillis();
|
||||||
|
if (bigger || changeday) {
|
||||||
updateTomorrow();
|
updateTomorrow();
|
||||||
if (stream != null) {
|
if (logstream != null) {
|
||||||
stream.close();
|
logstream.close();
|
||||||
if (bigger) {
|
if (bigger) {
|
||||||
for (int i = Math.min(count - 2, index.get() - 1); i > 0; i--) {
|
for (int i = Math.min(count - 2, logindex.get() - 1); i > 0; i--) {
|
||||||
File greater = new File(logfile.getPath() + "." + i);
|
File greater = new File(logfile.getPath() + "." + i);
|
||||||
if (greater.exists()) Files.move(greater.toPath(), new File(logfile.getPath() + "." + (i + 1)).toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
if (greater.exists()) Files.move(greater.toPath(), new File(logfile.getPath() + "." + (i + 1)).toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
||||||
}
|
}
|
||||||
Files.move(logfile.toPath(), new File(logfile.getPath() + ".1").toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
Files.move(logfile.toPath(), new File(logfile.getPath() + ".1").toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
||||||
}
|
}
|
||||||
stream = null;
|
logstream = null;
|
||||||
|
}
|
||||||
|
if (unusual != null && logunusualstream != null) {
|
||||||
|
logunusualstream.close();
|
||||||
|
if (limit > 0 && limit <= logunusuallength.get()) {
|
||||||
|
for (int i = Math.min(count - 2, logunusualindex.get() - 1); i > 0; i--) {
|
||||||
|
File greater = new File(logunusualfile.getPath() + "." + i);
|
||||||
|
if (greater.exists()) Files.move(greater.toPath(), new File(logunusualfile.getPath() + "." + (i + 1)).toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
||||||
|
}
|
||||||
|
Files.move(logunusualfile.toPath(), new File(logunusualfile.getPath() + ".1").toPath(), REPLACE_EXISTING, ATOMIC_MOVE);
|
||||||
|
}
|
||||||
|
logunusualstream = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (stream == null) {
|
if (logstream == null) {
|
||||||
index.incrementAndGet();
|
logindex.incrementAndGet();
|
||||||
java.time.LocalDate date = LocalDate.now();
|
java.time.LocalDate date = LocalDate.now();
|
||||||
logfile = new File(pattern.replace("%m", String.valueOf((date.getYear() * 100 + date.getMonthValue()))).replace("%d", String.valueOf((date.getYear() * 10000 + date.getMonthValue() * 100 + date.getDayOfMonth()))));
|
logfile = new File(pattern.replace("%m", String.valueOf((date.getYear() * 100 + date.getMonthValue()))).replace("%d", String.valueOf((date.getYear() * 10000 + date.getMonthValue() * 100 + date.getDayOfMonth()))));
|
||||||
logfile.getParentFile().mkdirs();
|
logfile.getParentFile().mkdirs();
|
||||||
length.set(logfile.length());
|
loglength.set(logfile.length());
|
||||||
stream = new FileOutputStream(logfile, append);
|
logstream = new FileOutputStream(logfile, append);
|
||||||
|
}
|
||||||
|
if (unusual != null && logunusualstream == null) {
|
||||||
|
logunusualindex.incrementAndGet();
|
||||||
|
java.time.LocalDate date = LocalDate.now();
|
||||||
|
logunusualfile = new File(unusual.replace("%m", String.valueOf((date.getYear() * 100 + date.getMonthValue()))).replace("%d", String.valueOf((date.getYear() * 10000 + date.getMonthValue() * 100 + date.getDayOfMonth()))));
|
||||||
|
logunusualfile.getParentFile().mkdirs();
|
||||||
|
logunusuallength.set(logunusualfile.length());
|
||||||
|
logunusualstream = new FileOutputStream(logunusualfile, append);
|
||||||
}
|
}
|
||||||
//----------------------写日志-------------------------
|
//----------------------写日志-------------------------
|
||||||
String message = getFormatter().format(record);
|
String message = getFormatter().format(record);
|
||||||
String encoding = getEncoding();
|
String encoding = getEncoding();
|
||||||
byte[] bytes = encoding == null ? message.getBytes() : message.getBytes(encoding);
|
byte[] bytes = encoding == null ? message.getBytes() : message.getBytes(encoding);
|
||||||
stream.write(bytes);
|
logstream.write(bytes);
|
||||||
length.addAndGet(bytes.length);
|
loglength.addAndGet(bytes.length);
|
||||||
|
if (unusual != null && (record.getLevel() == Level.WARNING || record.getLevel() == Level.SEVERE)) {
|
||||||
|
logunusualstream.write(bytes);
|
||||||
|
logunusuallength.addAndGet(bytes.length);
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ErrorManager err = getErrorManager();
|
ErrorManager err = getErrorManager();
|
||||||
if (err != null) err.error(null, e, ErrorManager.WRITE_FAILURE);
|
if (err != null) err.error(null, e, ErrorManager.WRITE_FAILURE);
|
||||||
@@ -177,30 +211,39 @@ public class LogFileHandler extends Handler {
|
|||||||
private void configure() {
|
private void configure() {
|
||||||
LogManager manager = LogManager.getLogManager();
|
LogManager manager = LogManager.getLogManager();
|
||||||
String cname = LogFileHandler.class.getName();
|
String cname = LogFileHandler.class.getName();
|
||||||
pattern = manager.getProperty(cname + ".pattern");
|
this.pattern = manager.getProperty(cname + ".pattern");
|
||||||
if (pattern == null) {
|
if (this.pattern == null) {
|
||||||
pattern = "logs-%m/" + getPrefix() + "log-%d.log";
|
this.pattern = "logs-%m/" + getPrefix() + "log-%d.log";
|
||||||
} else {
|
} else {
|
||||||
int pos = pattern.lastIndexOf('/');
|
int pos = this.pattern.lastIndexOf('/');
|
||||||
if (pos > 0) {
|
if (pos > 0) {
|
||||||
pattern = pattern.substring(0, pos + 1) + getPrefix() + pattern.substring(pos + 1);
|
this.pattern = this.pattern.substring(0, pos + 1) + getPrefix() + this.pattern.substring(pos + 1);
|
||||||
} else {
|
} else {
|
||||||
pattern = getPrefix() + pattern;
|
this.pattern = getPrefix() + this.pattern;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String unusualstr = manager.getProperty(cname + ".unusual");
|
||||||
|
if (unusualstr != null) {
|
||||||
|
int pos = unusualstr.lastIndexOf('/');
|
||||||
|
if (pos > 0) {
|
||||||
|
this.unusual = unusualstr.substring(0, pos + 1) + getPrefix() + unusualstr.substring(pos + 1);
|
||||||
|
} else {
|
||||||
|
this.unusual = getPrefix() + unusualstr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String limitstr = manager.getProperty(cname + ".limit");
|
String limitstr = manager.getProperty(cname + ".limit");
|
||||||
try {
|
try {
|
||||||
if (limitstr != null) limit = Math.abs(Integer.decode(limitstr));
|
if (limitstr != null) this.limit = Math.abs(Integer.decode(limitstr));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
String countstr = manager.getProperty(cname + ".count");
|
String countstr = manager.getProperty(cname + ".count");
|
||||||
try {
|
try {
|
||||||
if (countstr != null) count = Math.max(1, Math.abs(Integer.decode(countstr)));
|
if (countstr != null) this.count = Math.max(1, Math.abs(Integer.decode(countstr)));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
String appendstr = manager.getProperty(cname + ".append");
|
String appendstr = manager.getProperty(cname + ".append");
|
||||||
try {
|
try {
|
||||||
if (appendstr != null) append = "true".equalsIgnoreCase(appendstr) || "1".equals(appendstr);
|
if (appendstr != null) this.append = "true".equalsIgnoreCase(appendstr) || "1".equals(appendstr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
String levelstr = manager.getProperty(cname + ".level");
|
String levelstr = manager.getProperty(cname + ".level");
|
||||||
@@ -256,7 +299,7 @@ public class LogFileHandler extends Handler {
|
|||||||
@Override
|
@Override
|
||||||
public void flush() {
|
public void flush() {
|
||||||
try {
|
try {
|
||||||
if (stream != null) stream.flush();
|
if (logstream != null) logstream.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ErrorManager err = getErrorManager();
|
ErrorManager err = getErrorManager();
|
||||||
if (err != null) err.error(null, e, ErrorManager.FLUSH_FAILURE);
|
if (err != null) err.error(null, e, ErrorManager.FLUSH_FAILURE);
|
||||||
@@ -266,7 +309,7 @@ public class LogFileHandler extends Handler {
|
|||||||
@Override
|
@Override
|
||||||
public void close() throws SecurityException {
|
public void close() throws SecurityException {
|
||||||
try {
|
try {
|
||||||
if (stream != null) stream.close();
|
if (logstream != null) logstream.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ErrorManager err = getErrorManager();
|
ErrorManager err = getErrorManager();
|
||||||
if (err != null) err.error(null, e, ErrorManager.CLOSE_FAILURE);
|
if (err != null) err.error(null, e, ErrorManager.CLOSE_FAILURE);
|
||||||
|
|||||||
@@ -473,7 +473,7 @@ public final class Utility {
|
|||||||
* @param offset 偏移量
|
* @param offset 偏移量
|
||||||
* @param len 长度
|
* @param len 长度
|
||||||
*
|
*
|
||||||
* @return
|
* @return 字节数组
|
||||||
*/
|
*/
|
||||||
public static byte[] hexToBin(char[] src, int offset, int len) {
|
public static byte[] hexToBin(char[] src, int offset, int len) {
|
||||||
final int size = (len + 1) / 2;
|
final int size = (len + 1) / 2;
|
||||||
|
|||||||
Reference in New Issue
Block a user