Scala/Java: How to convert a stack trace to a string for printing with a logger

As a quick note, I just got a little bit better about logging stack traces when writing Java or Scala code. In Scala I used to get the text from a stack trace and then log it like this:

// this works, but it's not too useful/readable

In that code, getStackTrace returns a sequence, which I convert to a String before printing it.

A Scala Spring Framework dependency injection example

Curious about how well Scala would play with the Spring Framework, I created a small Scala/Spring dependency injection example project, which I'm sharing here.

The short answer is that Scala worked just fine with Spring, but it also showed me that I still have plenty to learn about inheritance in Scala.

My Spring applicationContext.xml file

I copied a Spring applicationContext.xml file from another project, then whittled it down to these bare essentials:

A Log4J format example

Log4J formatting FAQ: Can you share a Log4J output/logging format example?

Sure. I'll share a Java Log4J format example that I'm pretty happy with.

I've used the following Log4J logging format quite a bit lately, as I've been working on a headless Java app that can be deployed on thousands of computers, and I was looking for a good Log4J format that was easily readable by humans, and also easy to parse by computers. Here's what my Log4J output format look like these days:

How to print an exception stack trace using Log4J (or Commons Logging)

Log4J exception FAQ: "How do I print the stack trace of an exception using Log4J or Commons Logging?"

Printing the stack trace of a Log4J exception seems to be something of a trick question. In reviewing Java code from different developers at different organizations I see a lot of people working very hard to print a stack trace using Log4J, including a lot of variations of calling e.printStackTrace() method. In this short tutorial I’ll show how to solve this problem.

A Java Log4J example

Java Log4J FAQ: Can you share a simple Log4J example?

I don't have a lot of time today to get into all the Log4J properties and configuration options you can use, but I wanted to share at least one Java Log4J example here, so here goes.

A Log4J example Java class

The following Java Log4J example class is a simple example that initializes, and then uses, the Log4J logging library for Java applications. As you can see the configuration is pretty simple.