Skip to main content

Command Palette

Search for a command to run...

Pretty Logger for Java - improve the aesthetics of java console apps

Published
3 min read
Pretty Logger for Java - improve the aesthetics of java console apps

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

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");

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

J

Being new to java, this is mind blowing to me! I can't wait to dig into this. Just so I am clear, I would need to be using slf4j to use pl4j?

M

pl4j is a wrapper over sl4j. So by using pl4j you will actually interact with the sl4j api. This is implicit and nothing else is needed. But what you need is an actual implementation of sl4j, presumably logback. There is an example in the project with a TestClass and a logback.xml configuration file.

1
J

madalin ilie Got it! I'll check this out. I've never been so excited to log things to the console!

1
N

IMHO, this is not the right abstraction level to handle it.

SLF4J is an API. The "prettiness" is an implementation detail and should be handled by the underlying logging engine. You could move to the Appender level - and yes, it would require one such appender for each implementation (Logback, Log4J, etc.).

M

It's true. It might not be the best abstraction, but I like the benefit of explicitivness i.e. the ability to call methods that will mark the logs accordingly. I prefer to say complete rather than info with additional configuration. Sl4j (and implementations) supports a limited number of log levels. Using this library, even if the log levels remain the same, you can explicitly call the intended marker method. And it's still decoupled and require no change in the logging patterns.