| 1 |
/* |
| 2 |
* Copyright 2015-2020 the original author or authors |
| 3 |
* |
| 4 |
* This software is licensed under the Apache License, Version 2.0, |
| 5 |
* the GNU Lesser General Public License version 2 or later ("LGPL") |
| 6 |
* and the WTFPL. |
| 7 |
* You may choose either license to govern your use of this software only |
| 8 |
* upon the condition that you accept all of the terms of either |
| 9 |
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL. |
| 10 |
*/ |
| 11 |
package org.minidns.jul; |
| 12 |
|
| 13 |
import java.io.ByteArrayInputStream; |
| 14 |
import java.io.IOException; |
| 15 |
import java.io.InputStream; |
| 16 |
import java.io.PrintWriter; |
| 17 |
import java.io.StringWriter; |
| 18 |
import java.nio.charset.StandardCharsets; |
| 19 |
import java.text.SimpleDateFormat; |
| 20 |
import java.util.Date; |
| 21 |
import java.util.logging.ConsoleHandler; |
| 22 |
import java.util.logging.Formatter; |
| 23 |
import java.util.logging.Handler; |
| 24 |
import java.util.logging.Level; |
| 25 |
import java.util.logging.LogManager; |
| 26 |
import java.util.logging.LogRecord; |
| 27 |
import java.util.logging.Logger; |
| 28 |
|
| 29 |
@SuppressWarnings("DateFormatConstant") |
| 30 |
public class MiniDnsJul { |
| 31 |
|
| 32 |
private static final Logger LOGGER = Logger.getLogger(MiniDnsJul.class.getName()); |
| 33 |
|
| 34 |
private static final InputStream LOG_MANAGER_CONFIG = new ByteArrayInputStream(( |
| 35 |
// @formatter:off |
| 36 |
"org.minidns.level=FINEST" + '\n' |
| 37 |
).getBytes(StandardCharsets.UTF_8) |
| 38 |
); |
| 39 |
// @formatter:on |
| 40 |
|
| 41 |
private static final SimpleDateFormat LONG_LOG_TIME_FORMAT = new SimpleDateFormat("hh:mm:ss.SSS"); |
| 42 |
|
| 43 |
private static final SimpleDateFormat SHORT_LOG_TIME_FORMAT = new SimpleDateFormat("mm:ss.SSS"); |
| 44 |
|
| 45 |
private static final Handler CONSOLE_HANDLER = new ConsoleHandler(); |
| 46 |
|
| 47 |
private static boolean shortLog = true; |
| 48 |
|
| 49 |
static { |
| 50 |
try { |
| 51 |
LogManager.getLogManager().readConfiguration(LOG_MANAGER_CONFIG); |
| 52 |
} catch (SecurityException | IOException e) { |
| 53 |
LOGGER.log(Level.SEVERE, "Could not apply MiniDNS JUL configuration", e); |
| 54 |
} |
| 55 |
|
| 56 |
CONSOLE_HANDLER.setLevel(Level.OFF); |
| 57 |
CONSOLE_HANDLER.setFormatter(new Formatter() { |
| 58 |
@Override |
| 59 |
public String format(LogRecord logRecord) { |
| 60 |
StringBuilder sb = new StringBuilder(256); |
| 61 |
|
| 62 |
Date date = new Date(logRecord.getMillis()); |
| 63 |
String dateString; |
| 64 |
if (shortLog) { |
| 65 |
synchronized (SHORT_LOG_TIME_FORMAT) { |
| 66 |
dateString = SHORT_LOG_TIME_FORMAT.format(date); |
| 67 |
} |
| 68 |
} else { |
| 69 |
synchronized (LONG_LOG_TIME_FORMAT) { |
| 70 |
dateString = LONG_LOG_TIME_FORMAT.format(date); |
| 71 |
} |
| 72 |
} |
| 73 |
sb.append(dateString).append(' '); |
| 74 |
|
| 75 |
String level = logRecord.getLevel().toString(); |
| 76 |
if (shortLog) { |
| 77 |
level = level.substring(0, 1); |
| 78 |
} |
| 79 |
sb.append(level).append(' '); |
| 80 |
|
| 81 |
String loggerName = logRecord.getLoggerName(); |
| 82 |
if (shortLog) { |
| 83 |
String[] parts = loggerName.split("\\."); |
| 84 |
loggerName = parts[parts.length > 1 ? parts.length - 1 : 0]; |
| 85 |
} |
| 86 |
sb.append(loggerName); |
| 87 |
sb.append(' ').append(logRecord.getSourceMethodName()); |
| 88 |
|
| 89 |
if (shortLog) { |
| 90 |
sb.append(' '); |
| 91 |
} else { |
| 92 |
sb.append('\n'); |
| 93 |
} |
| 94 |
|
| 95 |
sb.append(formatMessage(logRecord)); |
| 96 |
if (logRecord.getThrown() != null) { |
| 97 |
StringWriter sw = new StringWriter(); |
| 98 |
PrintWriter pw = new PrintWriter(sw); |
| 99 |
// CHECKSTYLE:OFF |
| 100 |
pw.println(); |
| 101 |
logRecord.getThrown().printStackTrace(pw); |
| 102 |
// CHECKSTYLE:ON |
| 103 |
pw.close(); |
| 104 |
sb.append(sw); |
| 105 |
} |
| 106 |
sb.append('\n'); |
| 107 |
return sb.toString(); |
| 108 |
} |
| 109 |
}); |
| 110 |
Logger.getLogger("org.minidns").addHandler(CONSOLE_HANDLER); |
| 111 |
} |
| 112 |
|
| 113 |
public static void enableMiniDnsTrace() { |
| 114 |
enableMiniDnsTrace(true); |
| 115 |
} |
| 116 |
|
| 117 |
public static void enableMiniDnsTrace(boolean shortLog) { |
| 118 |
MiniDnsJul.shortLog = shortLog; |
| 119 |
CONSOLE_HANDLER.setLevel(Level.FINEST); |
| 120 |
} |
| 121 |
|
| 122 |
public static void disableMiniDnsTrace() { |
| 123 |
CONSOLE_HANDLER.setLevel(Level.OFF); |
| 124 |
} |
| 125 |
} |