[ENT-2678] PluginRegistrationTest is failing (fix) (OS part) (#4153)

This commit is contained in:
Michele Sollecito 2018-11-02 11:36:28 +00:00 committed by GitHub
parent 66116e8d20
commit 24e9ceac48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 18 deletions

View File

@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Configuration status="info" packages="net.corda.cliutils">
<Properties>
<Property name="log-path">${sys:log-path:-logs}</Property>
<Property name="log-name">node-${hostName}</Property>
<Property name="archive">${log-path}/archive</Property>
<Property name="defaultLogLevel">${sys:log4j2.level:-info}</Property>
<Property name="defaultLogLevel">${sys:defaultLogLevel:-info}</Property>
<Property name="consoleLogLevel">${sys:consoleLogLevel:-error}</Property>
<Property name="fileLogLevel">${sys:fileLogLevel:-${defaultLogLevel}}</Property>
</Properties>
<Appenders>
@ -78,7 +77,7 @@
<Loggers>
<Root level="${defaultLogLevel}">
<AppenderRef ref="Console-Appender" level="${consoleLogLevel}"/>
<AppenderRef ref="RollingFile-Appender" level="${fileLogLevel}"/>
<AppenderRef ref="RollingFile-Appender"/>
</Root>
<Logger name="BasicInfo" additivity="false">
<AppenderRef ref="Console-Appender-Println"/>

View File

@ -0,0 +1,48 @@
package net.corda.node.logging
import net.corda.core.flows.FlowLogic
import net.corda.core.flows.InitiatingFlow
import net.corda.core.flows.StartableByRPC
import net.corda.core.internal.div
import net.corda.core.messaging.FlowHandle
import net.corda.core.messaging.startFlow
import net.corda.core.utilities.getOrThrow
import net.corda.testing.driver.DriverParameters
import net.corda.testing.driver.NodeHandle
import net.corda.testing.driver.driver
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import java.io.File
class ErrorCodeLoggingTests {
@Test
fun `log entries with a throwable and ERROR or WARN get an error code appended`() {
driver(DriverParameters(notarySpecs = emptyList())) {
val node = startNode(startInSameProcess = false).getOrThrow()
node.rpc.startFlow(::MyFlow).waitForCompletion()
val logFile = node.logFile()
val linesWithErrorCode = logFile.useLines { lines -> lines.filter { line -> line.contains("[errorCode=") }.toList() }
assertThat(linesWithErrorCode).isNotEmpty
}
}
@StartableByRPC
@InitiatingFlow
class MyFlow : FlowLogic<String>() {
override fun call(): String {
throw IllegalArgumentException("Mwahahahah")
}
}
}
private fun FlowHandle<*>.waitForCompletion() {
try {
returnValue.getOrThrow()
} catch (e: Exception) {
// This is expected to throw an exception, using getOrThrow() just to wait until done.
}
}
private fun NodeHandle.logFile(): File = (baseDirectory / "logs").toFile().walk().filter { it.name.startsWith("node-") && it.extension == "log" }.single()

View File

@ -426,10 +426,9 @@ interface NodeStartupLogging {
}
fun CliWrapperBase.initLogging(baseDirectory: Path) {
val loggingLevel = loggingLevel.name.toLowerCase(Locale.ENGLISH)
System.setProperty("defaultLogLevel", loggingLevel) // These properties are referenced from the XML config file.
System.setProperty("defaultLogLevel", specifiedLogLevel) // These properties are referenced from the XML config file.
if (verbose) {
System.setProperty("consoleLogLevel", loggingLevel)
System.setProperty("consoleLogLevel", specifiedLogLevel)
Node.renderBasicInfoToConsole = false
}
System.setProperty("log-path", (baseDirectory / NodeCliCommand.LOGS_DIRECTORY_NAME).toString())

View File

@ -17,7 +17,7 @@ import java.nio.file.Path
internal class ValidateConfigurationCli : CliWrapperBase("validate-configuration", "Validate the configuration without starting the node.") {
internal companion object {
private val logger = loggerFor<ValidateConfigurationCli>()
private val logger by lazy { loggerFor<ValidateConfigurationCli>() }
internal fun logConfigurationErrors(errors: Iterable<Exception>, configFile: Path) {
errors.forEach { error ->
@ -56,7 +56,7 @@ internal class ValidateConfigurationCli : CliWrapperBase("validate-configuration
internal fun SharedNodeCmdLineOptions.nodeConfiguration(): Valid<NodeConfiguration> = NodeConfigurationParser.invoke(this)
private object NodeConfigurationParser : (SharedNodeCmdLineOptions) -> Valid<NodeConfiguration> {
private val logger = loggerFor<ValidateConfigurationCli>()
private val logger by lazy { loggerFor<ValidateConfigurationCli>() }
private val configRenderingOptions = ConfigRenderOptions.defaults().setComments(false).setOriginComments(false).setFormatted(true)

View File

@ -1,2 +1,2 @@
Log4jContextSelector=net.corda.node.utilities.logging.AsyncLoggerContextSelectorNoThreadLocal
log4jContextSelector=net.corda.node.utilities.logging.AsyncLoggerContextSelectorNoThreadLocal
AsyncLogger.RingBufferSize=262144

View File

@ -137,10 +137,9 @@ abstract class CliWrapperBase(val alias: String, val description: String) : Call
// This needs to be called before loggers (See: NodeStartup.kt:51 logger called by lazy, initLogging happens before).
// Node's logging is more rich. In corda configurations two properties, defaultLoggingLevel and consoleLogLevel, are usually used.
open fun initLogging() {
val loggingLevel = loggingLevel.name.toLowerCase(Locale.ENGLISH)
System.setProperty("defaultLogLevel", loggingLevel) // These properties are referenced from the XML config file.
System.setProperty("defaultLogLevel", specifiedLogLevel) // These properties are referenced from the XML config file.
if (verbose) {
System.setProperty("consoleLogLevel", loggingLevel)
System.setProperty("consoleLogLevel", specifiedLogLevel)
}
System.setProperty("log-path", Paths.get(".").toString())
}
@ -154,6 +153,8 @@ abstract class CliWrapperBase(val alias: String, val description: String) : Call
logger.info("Application Args: ${args.joinToString(" ")}")
return runProgram()
}
val specifiedLogLevel: String by lazy { System.getProperty("log4j2.level")?.toLowerCase(Locale.ENGLISH) ?: loggingLevel.name.toLowerCase(Locale.ENGLISH) }
}
/**

View File

@ -10,9 +10,8 @@ import org.apache.logging.log4j.core.config.xml.XmlConfiguration
import org.apache.logging.log4j.core.impl.LogEventFactory
@Plugin(name = "CordaLog4j2ConfigFactory", category = "ConfigurationFactory")
@Order(10)
@Order(Integer.MAX_VALUE)
class CordaLog4j2ConfigFactory : ConfigurationFactory() {
private companion object {
private val SUPPORTED_TYPES = arrayOf(".xml", "*")
}
@ -22,13 +21,13 @@ class CordaLog4j2ConfigFactory : ConfigurationFactory() {
override fun getSupportedTypes() = SUPPORTED_TYPES
private class ErrorCodeAppendingConfiguration(loggerContext: LoggerContext, source: ConfigurationSource) : XmlConfiguration(loggerContext, source) {
override fun doConfigure() {
super.doConfigure()
loggers.values.forEach {
val existingFactory = it.logEventFactory
it.logEventFactory = LogEventFactory { loggerName, marker, fqcn, level, message, properties, error -> existingFactory.createEvent(loggerName, marker, fqcn, level, message?.withErrorCodeFor(error, level), properties, error) }
it.logEventFactory = LogEventFactory { loggerName, marker, fqcn, level, message, properties, error ->
existingFactory.createEvent(loggerName, marker, fqcn, level, message?.withErrorCodeFor(error, level), properties, error)
}
}
}
}