Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java tip: Orthogonality by example

The principles of modular and maintainable design in Log4j

  • Print
  • Feedback

Orthogonality is a concept often used to describe modular and maintainable software, but it's more easily understood by way of a case study. In this article, Jens Dietrich demystifies orthogonality and some related design principles by demonstrating their use in the popular Log4j utility library. He also discusses how Log4j violates orthogonality in a couple of instances and discusses possible workarounds to the issues raised.

The concept of orthogonality is based on the Greek word orthogōnios, meaning "right-angled." It is often used to express the independence between different dimensions. When an object moves along the x-axis in a three-dimensional space, its y and z coordinates don't change. Change in one dimension does not cause change in another dimension, which means that one dimension cannot cause side-effects for others.

This explains why the concept of orthogonality is often used to describe modular and maintainable software design: thinking about systems as points in a multi-dimensional space (spawned by independent, orthogonal dimensions) helps software developers to ensure that our changes to one aspect of system will not have side-effects for another.

It happens that Log4j, a popular open source logging package for Java, is a good example of a modular design based on orthogonality.

The dimensions of Log4j

Logging is just a fancier version of the System.out.println() statement, and Log4j is a utility package that abstracts the mechanics of logging on the Java platform. Among other things, Log4j features allow developers to do the following:

  • Log to different appenders (not only the console but also to files, network locations, relational databases, operating system log utilities, and more)
  • Log at several levels (such as ERROR, WARN, INFO, and DEBUG)
  • Centrally control how much information is logged at a given logging level
  • Use different layouts to define how a logging event is rendered into a string

While Log4j does have other features, I will focus on these three dimensions of its functionality in order to explore the concept and benefits of orthogonality. Note that my discussion is based on Log4j version 1.2.17.

Log4j on JavaWorld

Get an overview of Log4j and learn how to write your own custom Log4j appenders. Want more Java tutorials? Get the Enterprise Java newsletter delivered to your inbox.

Considering Log4j types as aspects

Appenders, level, and layout are three aspects of Log4j that can be seen as independent dimensions. I use the term aspect here as a synonym for concern, meaning a piece of interest or focus in a program. In this case, it is easy to define these three concerns based on the questions that each addresses:

  • Appender: Where should the log event data be sent for display or storage?
  • Layout: How should a log event be presented?
  • Level: Which log events should be processed?

Now try considering these aspects together in three-dimensional space. Each point within this space represents a valid system configuration, as shown in Figure 1. (Note that I am offering a slightly simplified view of Log4j: Each point in Figure 1 is actually not a global system-wide configuration, but a configuration for one particular logger. The loggers themselves can be considered as a fourth dimension.)

  • Print
  • Feedback

Resources