Corda-1155 - Jolokia logging via slf4j (#2753)

* Jolokia logging through slf4j

* review remarks
This commit is contained in:
Andrzej Grzesik 2018-03-07 14:12:15 +01:00 committed by Katelyn Baker
parent 5bd1ba0f39
commit 0d4e99d4f1
6 changed files with 106 additions and 8 deletions

View File

@ -60,5 +60,9 @@
<Logger name="org.apache.activemq.artemis.core.server" level="error" additivity="false">
<AppenderRef ref="RollingFile-Appender"/>
</Logger>
<Logger name="org.jolokia" additivity="true" level="warn">
<AppenderRef ref="Console-Appender-Println"/>
<AppenderRef ref="RollingFile-Appender" />
</Logger>
</Loggers>
</Configuration>

View File

@ -44,7 +44,7 @@ fun main(args: Array<String>) {
println("Finished starting nodes")
}
private abstract class JarType(internal val jarName: String) {
private abstract class JarType(internal val jarName: String, internal val jolokiaLogHandler: String? = null) {
internal abstract fun acceptNodeConf(nodeConf: File): Boolean
internal fun acceptDirAndStartProcess(dir: File, headless: Boolean, javaArgs: List<String>, jvmArgs: List<String>): Process? {
if (!File(dir, jarName).exists()) {
@ -64,12 +64,12 @@ private abstract class JarType(internal val jarName: String) {
internal abstract val headlessArgs: List<String>
}
private object NodeJarType : JarType("corda.jar") {
private object NodeJarType : JarType("corda.jar", jolokiaLogHandler = "net.corda.node.JolokiaSlf4Adapter") {
override fun acceptNodeConf(nodeConf: File) = true
override val headlessArgs = listOf("--no-local-shell")
}
private object WebJarType : JarType("corda-webserver.jar") {
private object WebJarType : JarType(jarName = "corda-webserver.jar", jolokiaLogHandler = "net.corda.webserver.JolokiaSlf4Adapter") {
// TODO: Add a webserver.conf, or use TypeSafe config instead of this hack
override fun acceptNodeConf(nodeConf: File) = Files.lines(nodeConf.toPath()).anyMatch { "webAddress" in it }
override val headlessArgs = emptyList<String>()
@ -82,7 +82,8 @@ private abstract class JavaCommand(
monitoringPort: Int?,
internal val nodeName: String,
args: List<String>,
jvmArgs: List<String>
jvmArgs: List<String>,
jolokiaLoggerClass: String?
) {
private val jolokiaJar by lazy {
File("$dir/drivers").listFiles { _, filename ->
@ -94,9 +95,18 @@ private abstract class JavaCommand(
add(getJavaPath())
addAll(jvmArgs)
add("-Dname=$nodeName")
val jvmArgs: MutableList<String> = mutableListOf()
null != debugPort && jvmArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$debugPort")
null != monitoringPort && jvmArgs.add("-javaagent:drivers/$jolokiaJar=port=$monitoringPort")
if(null != monitoringPort) {
val jolokiaConfig = java.lang.StringBuilder()
jolokiaConfig.append("-javaagent:drivers/$jolokiaJar=port=$monitoringPort")
if(jolokiaLoggerClass != null) {
jolokiaConfig.append(",logHandlerClass=${jolokiaLoggerClass}")
}
jvmArgs.add(jolokiaConfig.toString())
}
if (jvmArgs.isNotEmpty()) {
add("-Dcapsule.jvm.args=${jvmArgs.joinToString(separator = " ")}")
}
@ -111,13 +121,13 @@ private abstract class JavaCommand(
}
private class HeadlessJavaCommand(jarType: JarType, dir: File, debugPort: Int?, monitoringPort: Int?, args: List<String>, jvmArgs: List<String>)
: JavaCommand(jarType.jarName, dir, debugPort, monitoringPort, dir.name, jarType.headlessArgs + args, jvmArgs) {
: JavaCommand(jarType.jarName, dir, debugPort, monitoringPort, dir.name, jarType.headlessArgs + args, jvmArgs, jarType.jolokiaLogHandler) {
override fun processBuilder() = ProcessBuilder(command).redirectError(File("error.$nodeName.log")).inheritIO()
override fun getJavaPath() = File(File(System.getProperty("java.home"), "bin"), "java").path
}
private class TerminalWindowJavaCommand(jarType: JarType, dir: File, debugPort: Int?, monitoringPort: Int?, args: List<String>, jvmArgs: List<String>)
: JavaCommand(jarType.jarName, dir, debugPort, monitoringPort, "${dir.name}-${jarType.jarName}", args, jvmArgs) {
: JavaCommand(jarType.jarName, dir, debugPort, monitoringPort, "${dir.name}-${jarType.jarName}", args, jvmArgs, jarType.jolokiaLogHandler) {
override fun processBuilder() = ProcessBuilder(when (os) {
OS.MACOS -> {
listOf("osascript", "-e", """tell app "Terminal"

View File

@ -182,7 +182,7 @@ dependencies {
testCompile "org.glassfish.jersey.containers:jersey-container-jetty-http:${jersey_version}"
// Jolokia JVM monitoring agent
runtime "org.jolokia:jolokia-jvm:${jolokia_version}:agent"
compile "org.jolokia:jolokia-jvm:${jolokia_version}:agent"
}
task integrationTest(type: Test) {

View File

@ -0,0 +1,40 @@
package net.corda.node
import org.jolokia.util.LogHandler
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* This also exists in webserver, because depends on jolokia
**/
class JolokiaSlf4Adapter : LogHandler {
companion object {
val log: Logger = LoggerFactory.getLogger("org.jolokia")
}
override fun error(message: String?, t: Throwable?) {
if (message != null) {
if (t != null) {
log.error(message, t)
} else {
log.error(message)
}
} else if (t != null) {
log.error("Exception without a comment", t)
}
}
override fun debug(message: String?) {
if (message != null) {
log.debug(message)
}
}
override fun info(message: String?) {
if (message != null) {
log.info(message)
}
}
}

View File

@ -25,5 +25,9 @@
<Logger name="org.hibernate.SQL" level="info" additivity="false">
<AppenderRef ref="Console-Appender"/>
</Logger>
<Logger name="org.jolokia" additivity="true" level="warn">
<AppenderRef ref="Console-Appender-Println"/>
<AppenderRef ref="RollingFile-Appender" />
</Logger>
</Loggers>
</Configuration>

View File

@ -0,0 +1,40 @@
package net.corda.webserver
import org.jolokia.util.LogHandler
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* This also exists in node, because depends on jolokia
**/
class JolokiaSlf4Adapter : LogHandler {
companion object {
val log: Logger = LoggerFactory.getLogger("org.jolokia")
}
override fun error(message: String?, t: Throwable?) {
if (message != null) {
if (t != null) {
log.error(message, t)
} else {
log.error(message)
}
} else if (t != null) {
log.error("Exception without a comment", t)
}
}
override fun debug(message: String?) {
if (message != null) {
log.debug(message)
}
}
override fun info(message: String?) {
if (message != null) {
log.info(message)
}
}
}