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.

This commit is contained in:
Matthew Nesbit 2016-07-12 16:06:51 +01:00
parent 54042db7bc
commit 8f0671f828
3 changed files with 27 additions and 22 deletions

View File

@ -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)
}

View File

@ -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<String>) {
exitProcess(runIRSDemo(args))
}
@ -276,7 +280,7 @@ fun runIRSDemo(args: Array<String>): 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())

View File

@ -30,6 +30,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
@ -66,6 +68,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<String>) {
exitProcess(runTraderDemo(args))
}
@ -82,7 +86,7 @@ fun runTraderDemo(args: Array<String>): Int {
val options = try {
parser.parse(*args)
} catch (e: Exception) {
println(e.message)
log.error(e.message)
printHelp(parser)
return 1
}
@ -112,7 +116,7 @@ fun runTraderDemo(args: Array<String>): 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 {
@ -147,7 +151,7 @@ fun runTraderDemo(args: Array<String>): 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()
}