mirror of
https://github.com/corda/corda.git
synced 2025-02-01 08:48:09 +00:00
Add logging utilities using slf4j
This commit is contained in:
parent
e5a36580da
commit
ea4a6d8f06
@ -33,6 +33,10 @@ dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
|
||||
compile "com.google.guava:guava:18.0"
|
||||
compile "com.esotericsoftware:kryo:3.0.3"
|
||||
compile "com.google.code.findbugs:jsr305:3.0.1"
|
||||
|
||||
// Logging
|
||||
compile "org.slf4j:slf4j-jdk14:1.7.13"
|
||||
|
||||
// For visualisation
|
||||
compile "org.graphstream:gs-core:1.3"
|
||||
|
74
src/main/kotlin/core/utilities/Logging.kt
Normal file
74
src/main/kotlin/core/utilities/Logging.kt
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2015 Distributed Ledger Group LLC. Distributed as Licensed Company IP to DLG Group Members
|
||||
* pursuant to the August 7, 2015 Advisory Services Agreement and subject to the Company IP License terms
|
||||
* set forth therein.
|
||||
*
|
||||
* All other rights reserved.
|
||||
*/
|
||||
|
||||
package core.utilities
|
||||
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
import java.text.MessageFormat
|
||||
import java.util.*
|
||||
import java.util.logging.Formatter
|
||||
import java.util.logging.Level
|
||||
import java.util.logging.LogRecord
|
||||
import java.util.logging.Logger
|
||||
|
||||
// A couple of inlined utility functions: the first is just a syntax convenience, the second lets us use
|
||||
// Kotlin's string interpolation efficiently: the message is never calculated/concatenated together unless
|
||||
// logging at that level is enabled.
|
||||
inline fun <reified T : Any> loggerFor(): org.slf4j.Logger = LoggerFactory.getLogger(T::class.java)
|
||||
inline fun org.slf4j.Logger.trace(msg: () -> String) {
|
||||
if (isTraceEnabled) trace(msg())
|
||||
}
|
||||
|
||||
/**
|
||||
* A Java logging formatter that writes more compact output than the default.
|
||||
*/
|
||||
class BriefLogFormatter : Formatter() {
|
||||
override fun format(logRecord: LogRecord): String {
|
||||
val arguments = arrayOfNulls<Any>(6)
|
||||
arguments[0] = logRecord.threadID
|
||||
val fullClassName = logRecord.sourceClassName
|
||||
val lastDot = fullClassName.lastIndexOf('.')
|
||||
val className = fullClassName.substring(lastDot + 1)
|
||||
arguments[1] = className
|
||||
arguments[2] = logRecord.sourceMethodName
|
||||
arguments[3] = Date(logRecord.millis)
|
||||
arguments[4] = logRecord.message
|
||||
if (logRecord.thrown != null) {
|
||||
val result = StringWriter()
|
||||
logRecord.thrown.printStackTrace(PrintWriter(result))
|
||||
arguments[5] = result.toString()
|
||||
} else {
|
||||
arguments[5] = ""
|
||||
}
|
||||
return messageFormat.format(arguments)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val messageFormat = MessageFormat("{3,date,HH:mm:ss} {0} {1}.{2}: {4}\n{5}")
|
||||
|
||||
// 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
|
||||
// keep a reference to our custom logger around.
|
||||
private lateinit var loggerRef: Logger
|
||||
|
||||
/** Configures JDK logging to use this class for everything. */
|
||||
fun init() {
|
||||
loggerRef = Logger.getLogger("")
|
||||
val handlers = loggerRef.handlers
|
||||
handlers[0].formatter = BriefLogFormatter()
|
||||
}
|
||||
|
||||
fun initVerbose() {
|
||||
init()
|
||||
loggerRef.level = Level.ALL
|
||||
loggerRef.handlers[0].level = Level.ALL
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user