DEV Community

Madalin Ilie
Madalin Ilie

Posted on

Pretty Logger for Java - build nice looking command line apps in Java

Default ThemeGestures Theme

Overview

Java might not be the first choice when wanting to build command line applications, but as a Java developer myself, I don't see any reason for not doing so. With the rise of frameworks like Spring Boot or Quarkus and GraalVM you can create fast, performant and powerful command line apps with ease.

What you display in the console when your application is doing its magic is one of the key aspects that you need to get right. There are plenty of good practices out there on how to output quality content that will help users understand what is going on, but another important thing is aesthetics.

Improving aesthetics

There are several ways of making your output more eye-catching using libraries like jansi but it usually requires either to build some common routines for formatting or treat each output individually.

This is why I've created the PL4J library (Pretty Logger for Java). It's a decorator for SLF4J (so that you don't need to change your logging patterns) that enriches the number of logging levels you have available and adds colourful symbols and labels for better aesthetics.

Getting starting it's easy:

PrettyLogger prettyLogger = PrettyLoggerFactory.getLogger(TestClass.class); //same declaration as SLF4J
Enter fullscreen mode Exit fullscreen mode

and you have multiple levels available:

prettyLogger.success("received response from: {}", "http://google.com");
prettyLogger.awaiting("parsing input data");
prettyLogger.complete("finish processing");
prettyLogger.debug("value is: {}", "190");
prettyLogger.error("not able to connect to: {}", "http://google.com");
prettyLogger.fatal("something went terribly wrong");
prettyLogger.info("url to connect to: {}", "http://google.com");
prettyLogger.note("remember to run CATS");
prettyLogger.pause("process was paused");
prettyLogger.santa("ho! ho! ho!");
prettyLogger.star("run CATS next time");
prettyLogger.start("process started");
prettyLogger.stop("process paused");
prettyLogger.warning("unable to normalize string");
Enter fullscreen mode Exit fullscreen mode

You can also control labels's and symbols's colour, bold, underline as well as setting global themes to get different symbols sets entirely.

You can check the entire set of features on GitHub: https://github.com/ludovicianul/pl4j

Top comments (0)