Creating a JSON log stream from Corda

David Awad
Corda
Published in
3 min readMar 30, 2021

An incredibly useful tool for node operators to integrate with other services

If you’ve done a lot of work with log aggregation tools like Splunk, you’ve probably come against many issues with trying to export logs from all of your distributed systems in a way that your aggregator can understand. The industry has all but settled on JSON. Naturally, a lot of the services developed over the past couple of years all support JSON.

This is a common question we’ve received as developers try to integrate Corda into their existing monitoring setups. We’ve made strides in this by working on integrations with Prometheus, Grafana, and Truffle. Still, today I’m excited to share a developer sample I’ve put together in collaboration with Splunk to enable developers to create a stream of logs in JSON.

Using Log4J2 in Corda

Internally Corda uses an open-source logging utility called Apache Log4j2. This utility is quite powerful because it’s so customizable. If you look at our template CorDapp, Corda typically supplies an XML configuration file with some sane defaults. However, it’s possible to customize this configuration file to output JSON and modify your code to produce arbitrary JSON objects.

If you’re looking to grab the XML file and be on your way, here’s the configuration file.

You’ll want to know how on line 13, we specify an actual filename for the node to output this JSON log.

The other note is that on line 14 in our JsonLayout tag we’ve specified an attribute of properties="true" . This is important for us because we want to add additional properties to these JSON objects as they get logged out from our CorDapps.

If you’re trying to specify the config file alone, you can do that with a quick command line flag:

java -Dlog4j.configurationFile=logging-cordapp/build/resources/main/log4j2.xml -jar corda.jar

Logging Arbitrary Objects

Once you’ve got your logging configuration setup correctly, using the logger and adding arbitrary information is relatively easy.

You can create your loggers and add properties to the ThreadContext object, and they’ll be logged as part of the entities.

If this small example makes sense, you have everything you need to get more robust output from your CorDapps!

Happy coding ~

A special thanks to Jayson Jacobs from Splunk for his help putting this together.

For early access to Splunk Connect for Corda, reach out to blockchain@splunk.com.

You can find the example CorDapp in both java and kotlin on Github.

Happy logging ~

Want to learn more about building rock-solid distributed blockchains on Corda? Be sure to visit corda.net, check out our community page to learn how to connect with other Corda developers, and sign up for one of our newsletters for the latest updates.

— David Awad is a Developer Evangelist at R3, an enterprise blockchain software firm working with a global ecosystem of more than 350 participants across multiple industries from both the private and public sectors to develop on Corda, its open-source blockchain platform, and Corda Enterprise, a commercial version of Corda for enterprise usage.

Follow David on Twitter here.

--

--