Skip to content

codesofun/pl4j

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pretty Logger for Java (PL4J)

(Now with themes support!)

Loggers Loggers Loggers

Description

Pretty Logger for Java is a slf4j decorator that enables pretty printing on the console using ANSI formatting through jansi. PL4J is built around the concept of Markers. This means that you can only use logback as an implementation, being the only SLF4J binding that supports Markers.

Table of Contents

Usage

Add it as a Maven dependency in your pom.xml file:

<dependency>
    <groupId>io.github.ludovicianul</groupId>
    <artifactId>pretty-logger</artifactId>
    <version>LATEST</version>
</dependency>
 public class TestClass {

    public static void main(String... args) {
        PrettyLogger prettyLogger = PrettyLoggerFactory.getLogger(TestClass.class); //same declaration as SLF4J

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

Configuration

Log Pattern

This is a sample logback configuration file that was used to display the above console output:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <withJansi>true</withJansi>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-27marker %msg%n</pattern>
        </encoder>
    </appender>
    <root level="trace">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

The pattern must contain the %masker keyword, otherwise no label or symbol will be displayed.

Log Level and Markers

PL4J uses the following mapping between the SLF4J log levels and the markers:

SLF4J Log Level PL4J Marker
debug debug
info awaiting, complete, info, note, pause, pending, santa, star, start, stop, success, skipping
warn warning
error error, fatal

Markers Configuration

Individual configuration

You can override the default Markers as follows:

import io.github.ludovicianul.prettylogger.config.level.PrettyMarker;
import io.github.ludovicianul.prettylogger.PrettyLogger;
import io.github.ludovicianul.prettylogger.config.level.ConfigFactory;

import java.util.HashMap;

public class TestClass {

    public static void main(String... args) {
        PrettyLogger prettyLogger = PrettyLoggerFactory.getLogger(TestClass.class);
        //option 1
        PrettyMarker config = ConfigFactory.error().label("err");// we change the label to `err` instead of `error`
        prettyLogger.log(config, "this is an error");//note that we use log() instead of error()
        //option 2
        Map<PrettyMarker.ConfigKey, Object> configMap = new HashMap<>();
        configMap.put(PrettyMarker.ConfigKey.UNDERLINE, true);
        prettyLogger.skip(configMap, "skip processing for id 1");
    }
}

You can override both the symbol and the label. The following flags can also be configured (all booleans):

  • bold
  • underline
  • showLabel
  • showSymbol

The flags can be configured both individually, for each marker:

import io.github.ludovicianul.prettylogger.config.level.PrettyMarker;
import io.github.ludovicianul.prettylogger.PrettyLogger;
import io.github.ludovicianul.prettylogger.config.level.ConfigFactory;

public class TestClass {

    public static void main(String... args) {
        PrettyLogger prettyLogger = PrettyLoggerFactory.getLogger(TestClass.class);
        PrettyMarker config = ConfigFactory.error().bold(false).underline(true).showLabel(true).showSymbol(true);
        prettyLogger.log(config, "this is an error");//note that we use log() instead of error()
    }
}

As well as globally as shown in Global Configuration

Global configuration

You can configure the above flags globally through a file called pl4j.properties which must be present in the classpath. The following properties can be used to change the flag values (all booleans:

  • pl4j.show-labels
  • pl4j.show-symbols
  • pl4j.bold
  • pl4j.underline
  • pl4j.theme

Default values

If no global or individual configuration is supplied the default values are as follows:

  • bold = true
  • underline = false
  • showLabel = true
  • showSymbol = true
  • theme = default

Themes

PL4J supports themes, meaning that you can create your own combination of symbol, label and color for each MarkerType. In order to do this you must create a file named pl4j-themename.theme following the example of the default theme, place it into the classpath and configure the theme name in pl4j.properties as follows:

pl4j.theme=themename

If PL4J doesn't find the pl4j-themename.theme inside the classpath it will default to the default theme. This is the list of out-of-the-box supported themes:

Timers

You can also automatically measure the duration of specific tasks using timers. This is an example on how to use timers:

import io.github.ludovicianul.prettylogger.config.level.PrettyMarker;
import io.github.ludovicianul.prettylogger.PrettyLogger;
import io.github.ludovicianul.prettylogger.config.level.ConfigFactory;

public class TestClass {

    public static void main(String... args) {
        PrettyLogger prettyLogger = PrettyLoggerFactory.getLogger(TestClass.class);
        prettyLogger.time("job1");
        // stuff happening
        prettyLogger.timeEnd("job1");
    }
}

Credits

Inspired by signale.

Packages

No packages published

Languages

  • Java 100.0%