Logging colored messages on a terminal using Java


JCDP is a Java library that offers you a convenient way to print debug messages or colored messages on a terminal.

It all started when I needed to create an application with multiple levels of debug. The solution was an object (a Printer) with a maximum level of debug. Every debug message delivered to it had a level. If the Printer had an equal or greater level, the debug message would be printed. This allows you to place debug messages with different levels along the code. Then, when initiating the app, you choose a maximum level of debug and only the debug messages with an equal or lower level will be displayed. Even better you can change that maximum level dynamically.

To enhance the usefulness of this library I decided to add colored messages, which turned out to be pretty tricky. By abstracting the Ansi Escape Codes, printing a colored message became as easy as print("message", Attribute.BOLD, FColor.BLUE, BColor.YELLOW);


Main features

  • Print colored messages on a UNIX terminal or a WINDOWS command console.
  • Place inline debug messages with different levels of debug and let the Printer do the work of checking whether that message should be printed or not.
  • Adaptable. Change the attributes of a Printer dynamically, anytime you want.
  • Scalable. Combine multiple printers to have normal, error and debug messages displaying in several colors.
  • Extensible. You can write your own implementations of Printers as long as they implement the same interfaces and use them dynamically.
  • Easy to use. This library was developed with the worry to minimize the number of classes (maximum two) with which the user has to interact. To achieve it, several abstractions were used to hide the concrete implementation.


JCDP comes in two flavors: a Unix-only version and a cross-platform (both Unix and Windows) version. This happens because Windows’ command console doesn’t interpret Ansi Escape Codes, which are the foundations to print colored messages, thus an external library is included to solve this limitation. Unix’s terminal interprets these codes before displaying them so there’s no need to use external libraries. Both versions were tested under Unix (@Ubuntu-x86) and Windows (x86).


JCDP Copyright (C) 2011-* Diogo Nunes This program is free software under the terms of the MIT LICENSE and it comes with ABSOLUTELY NO WARRANTY. If this software was useful to you, consider donating.

6 replies on “Logging colored messages on a terminal using Java”

Please fix this bug in ColoredPrinter class in Maven library(then I need println I get only print):

public void println(Object msg, Attribute attr, FColor fg, BColor bg) {
this.getImpl().print(msg, attr, fg, bg);}