Logging: mention if a log message is a warning or error.

This commit is contained in:
Mike Hearn 2016-02-03 14:13:04 +01:00
parent af116f6990
commit f683f50eb5

View File

@ -31,27 +31,32 @@ inline fun org.slf4j.Logger.trace(msg: () -> String) {
*/ */
class BriefLogFormatter : Formatter() { class BriefLogFormatter : Formatter() {
override fun format(logRecord: LogRecord): String { override fun format(logRecord: LogRecord): String {
val arguments = arrayOfNulls<Any>(6) val arguments = arrayOfNulls<Any>(7)
arguments[0] = logRecord.threadID arguments[0] = logRecord.threadID
arguments[1] = when (logRecord.level) {
Level.SEVERE -> " **ERROR** "
Level.WARNING -> " (warning) "
else -> ""
}
val fullClassName = logRecord.sourceClassName val fullClassName = logRecord.sourceClassName
val dollarIndex = fullClassName.indexOf('$') val dollarIndex = fullClassName.indexOf('$')
val className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1, if (dollarIndex == -1) fullClassName.length else dollarIndex) val className = fullClassName.substring(fullClassName.lastIndexOf('.') + 1, if (dollarIndex == -1) fullClassName.length else dollarIndex)
arguments[1] = className arguments[2] = className
arguments[2] = logRecord.sourceMethodName arguments[3] = logRecord.sourceMethodName
arguments[3] = Date(logRecord.millis) arguments[4] = Date(logRecord.millis)
arguments[4] = if (logRecord.parameters != null) MessageFormat.format(logRecord.message, *logRecord.parameters) else logRecord.message arguments[5] = if (logRecord.parameters != null) MessageFormat.format(logRecord.message, *logRecord.parameters) else logRecord.message
if (logRecord.thrown != null) { if (logRecord.thrown != null) {
val result = StringWriter() val result = StringWriter()
logRecord.thrown.printStackTrace(PrintWriter(result)) logRecord.thrown.printStackTrace(PrintWriter(result))
arguments[5] = result.toString() arguments[6] = result.toString()
} else { } else {
arguments[5] = "" arguments[6] = ""
} }
return messageFormat.format(arguments) return messageFormat.format(arguments)
} }
companion object { companion object {
private val messageFormat = MessageFormat("{3,date,HH:mm:ss} {0} {1}.{2}: {4}\n{5}") private val messageFormat = MessageFormat("{4,date,HH:mm:ss} {0} {1}{2}.{3}: {5}\n{6}")
// OpenJDK made a questionable, backwards incompatible change to the Logger implementation. It internally uses // OpenJDK made a questionable, backwards incompatible change to the Logger implementation. It internally uses
// weak references now which means simply fetching the logger and changing its configuration won't work. We must // weak references now which means simply fetching the logger and changing its configuration won't work. We must