From 9d182343830ee022c33f6abe860ac18c678561fd Mon Sep 17 00:00:00 2001 From: Matthew Nesbit Date: Tue, 12 Jul 2016 16:06:51 +0100 Subject: [PATCH] Consistently use an sl4j logger with timestamp, rather than console println within the node. Note that the current protocol events published via the ProgressTracker are sent to the console without a timestamp. --- .../kotlin/com/r3corda/node/internal/Node.kt | 7 ++-- src/main/kotlin/com/r3corda/demos/IRSDemo.kt | 32 +++++++++++-------- .../kotlin/com/r3corda/demos/TraderDemo.kt | 10 ++++-- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt index 205ffc736e..75b217bc62 100644 --- a/node/src/main/kotlin/com/r3corda/node/internal/Node.kt +++ b/node/src/main/kotlin/com/r3corda/node/internal/Node.kt @@ -47,9 +47,6 @@ class ConfigurationException(message: String) : Exception(message) * network map service, while bootstrapping a network. * @param advertisedServices The services this node advertises. This must be a subset of the services it runs, * but nodes are not required to advertise services they run (hence subset). - * @param clientAPIs A list of JAX-RS annotated classes to register - * which will be used to register any extra client web interfaces the node requires for demos to use. - * Listed clientAPI classes are assumed to have to take a single APIServer constructor parameter. * @param clock The clock used within the node and by all protocols etc. */ class Node(dir: Path, val p2pAddr: HostAndPort, val webServerAddr: HostAndPort, configuration: NodeConfiguration, @@ -177,8 +174,8 @@ class Node(dir: Path, val p2pAddr: HostAndPort, val webServerAddr: HostAndPort, val f = RandomAccessFile(file, "rw") val l = f.channel.tryLock() if (l == null) { - println("It appears there is already a node running with the specified data directory $dir") - println("Shut that other node down and try again. It may have process ID ${file.readText()}") + log.error("It appears there is already a node running with the specified data directory $dir") + log.error("Shut that other node down and try again. It may have process ID ${file.readText()}") System.exit(1) } diff --git a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt index 4056eea3d7..58afaae302 100644 --- a/src/main/kotlin/com/r3corda/demos/IRSDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/IRSDemo.kt @@ -37,6 +37,8 @@ import java.util.* import kotlin.concurrent.fixedRateTimer import kotlin.system.exitProcess import com.r3corda.demos.utilities.* +import org.slf4j.Logger +import org.slf4j.LoggerFactory // IRS DEMO // @@ -268,6 +270,8 @@ private class NotSetupException: Throwable { constructor(message: String): super(message) {} } +private val log: Logger = LoggerFactory.getLogger("IRSDemo") + fun main(args: Array) { exitProcess(runIRSDemo(args)) } @@ -276,7 +280,7 @@ fun runIRSDemo(args: Array): Int { val cliParams = try { CliParams.parse(CliParamsSpec.parser.parse(*args)) } catch (e: Exception) { - println(e) + log.error(e.message) printHelp(CliParamsSpec.parser) return 1 } @@ -343,7 +347,7 @@ private fun runNode(cliParams: CliParams.RunNode): Int { node.stop() } } catch (e: NotSetupException) { - println(e.message) + log.error(e.message) return 1 } @@ -351,28 +355,28 @@ private fun runNode(cliParams: CliParams.RunNode): Int { } private fun runDateChange(cliParams: CliParams.DateChange): Int { - println("Changing date to " + cliParams.dateString) + log.info("Changing date to " + cliParams.dateString) val url = URL("http://${cliParams.apiAddress}/api/irs/demodate") if (putJson(url, "\"" + cliParams.dateString + "\"")) { - println("Date changed") + log.info("Date changed") return 0 } else { - println("Date failed to change") + log.error("Date failed to change") return 1 } } private fun runTrade(cliParams: CliParams.Trade): Int { - println("Uploading tradeID " + cliParams.tradeId) + log.info("Uploading tradeID " + cliParams.tradeId) // Note: the getResourceAsStream is an ugly hack to get the jvm to search in the right location val fileContents = IOUtils.toString(CliParams::class.java.getResourceAsStream("example-irs-trade.json")) val tradeFile = fileContents.replace("tradeXXX", cliParams.tradeId) val url = URL("http://${cliParams.apiAddress}/api/irs/deals") if (postJson(url, tradeFile)) { - println("Trade sent") + log.info("Trade sent") return 0 } else { - println("Trade failed to send") + log.error("Trade failed to send") return 1 } } @@ -397,7 +401,7 @@ private fun startNode(params: CliParams.RunNode, networkMap: SingleMessageRecipi } } - val node = logElapsedTime("Node startup") { + val node = logElapsedTime("Node startup", log) { Node(params.dir, params.networkAddress, params.apiAddress, config, networkMapId, advertisedServices, DemoClock()).start() } @@ -417,7 +421,7 @@ private fun nodeInfo(recipient: SingleMessageRecipient, identityFile: Path, adve val party = parsePartyFromFile(identityFile) return NodeInfo(recipient, party, advertisedServices) } catch (e: Exception) { - println("Could not find identify file $identityFile.") + log.error("Could not find identify file $identityFile.") throw e } } @@ -431,12 +435,12 @@ private fun runUploadRates(host: HostAndPort) { val url = URL("http://${host.toString()}/upload/interest-rates") if (uploadFile(url, fileContents)) { timer!!.cancel() - println("Rates uploaded successfully") + log.info("Rates uploaded successfully") } else { - print("Could not upload rates. Retrying in 5 seconds. ") + log.error("Could not upload rates. Retrying in 5 seconds. ") } } catch (e: Exception) { - println("Could not upload rates due to exception. Retrying in 5 seconds") + log.error("Could not upload rates due to exception. Retrying in 5 seconds") } }) } @@ -457,7 +461,7 @@ private fun getNodeConfig(cliParams: CliParams.RunNode): NodeConfiguration { private fun loadConfigFile(configFile: File, defaultLegalName: String): NodeConfiguration { if (!configFile.exists()) { createDefaultConfigFile(configFile, defaultLegalName) - println("Default config created at $configFile.") + log.warn("Default config created at $configFile.") } val config = ConfigFactory.parseFile(configFile).withFallback(ConfigFactory.load()) diff --git a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt index 04d9f8871a..ca2c357cfe 100644 --- a/src/main/kotlin/com/r3corda/demos/TraderDemo.kt +++ b/src/main/kotlin/com/r3corda/demos/TraderDemo.kt @@ -31,6 +31,8 @@ import com.r3corda.protocols.NotaryProtocol import com.r3corda.protocols.TwoPartyTradeProtocol import com.typesafe.config.ConfigFactory import joptsimple.OptionParser +import org.slf4j.Logger +import org.slf4j.LoggerFactory import java.nio.file.Files import java.nio.file.Path import java.nio.file.Paths @@ -67,6 +69,8 @@ enum class Role { // which holds things like checkpoints, keys, databases, message logs etc. val DEFAULT_BASE_DIRECTORY = "./build/trader-demo" +private val log: Logger = LoggerFactory.getLogger("TraderDemo") + fun main(args: Array) { exitProcess(runTraderDemo(args)) } @@ -83,7 +87,7 @@ fun runTraderDemo(args: Array): Int { val options = try { parser.parse(*args) } catch (e: Exception) { - println(e.message) + log.error(e.message) printHelp(parser) return 1 } @@ -113,7 +117,7 @@ fun runTraderDemo(args: Array): Int { BriefLogFormatter.initVerbose("+demo.buyer", "+demo.seller", "-org.apache.activemq") val directory = Paths.get(baseDirectory, role.name.toLowerCase()) - println("Using base demo directory $directory") + log.info("Using base demo directory $directory") // Override the default config file (which you can find in the file "reference.conf") to give each node a name. val config = run { @@ -148,7 +152,7 @@ fun runTraderDemo(args: Array): Int { } // And now construct then start the node object. It takes a little while. - val node = logElapsedTime("Node startup") { + val node = logElapsedTime("Node startup", log) { Node(directory, myNetAddr, apiNetAddr, config, networkMapId, advertisedServices).setup().start() }