Coder Social home page Coder Social logo

pl4j's Introduction

Pretty Logger for Java (PL4J)

Loggers

Description

Pretty Logger for Java is a slf4j decorator than 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.

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.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().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()
    }
}

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

  • bold
  • underline
  • showLabel
  • showSymbol

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

import io.github.ludovicianul.prettylogger.config.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 though 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

Default values

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

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

Credits

Inspired by signale.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.