GithubHelp home page GithubHelp logo

log4j2-slf4j-logging-mvp's Introduction

log4j2-slf4j-logging-mvp

A bare bone Log4j2 with SLF4j project.

I use it as a cheatsheet when starting a new project, because I always forget which dependencies should be configured to make it work.

Especially I am tired from the following error:

SLF4J(W): No SLF4J providers were found.
SLF4J(W): Defaulting to no-operation (NOP) logger implementation
SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.

Which can be easily fixed by importing of log4j-slf4j2-impl dependency:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j2-impl</artifactId>
    <version>2.22.1</version>
</dependency>

doc: https://logging.apache.org/log4j/2.x/log4j-slf4j-impl.html

Also Log4j2 requires com.lmax.disruptor to fix the StatusConsoleListener Null object returned for AsyncRoot in Loggers very annoying error:

<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.4</version>
</dependency>

If there is no need to have SLF4j we can use Log4j2 core implementation straightforwardly, it looks better on my taste:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class App {
    private static final Logger log = LogManager.getLogger();

    public static void main( String[] args ) {
        log.info( "Hello Log!" );
    }
}

I don't like that SLF4j implementation requires class specification in Logger initilization:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {
    private static final Logger log = LoggerFactory.getLogger(App.class);

    public static void main( String[] args )
    {log.info( "Hello Log!" );
    }
}

And it requires only 3 libraries:

<!-- LOG4J -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.22.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.22.1</version>
</dependency>
<!-- To fix error:
     ERROR StatusConsoleListener Could not create plugin of type class
     org.apache.logging.log4j.core.async.AsyncLoggerConfig$RootLogger
     for element AsyncRoot:
     java.lang.NoClassDefFoundError: com/lmax/disruptor/EventHandler
     ...
     ERROR StatusConsoleListener Null object returned for AsyncRoot in Loggers.
-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.4</version>
</dependency>

Instead of 5:

<!-- LOG4J -->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.22.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.22.1</version>
</dependency>
<!-- The Log4j 2 SLF4J Binding allows applications coded to the SLF4J API to use Log4j 2 as the implementation.
     https://logging.apache.org/log4j/2.x/log4j-slf4j-impl.html

     Also, this dependency resolves the following typical error:
     SLF4J(W): No SLF4J providers were found.
     SLF4J(W): Defaulting to no-operation (NOP) logger implementation
     SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.
-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j2-impl</artifactId>
    <version>2.22.1</version>
</dependency>
<!-- To fix error:
     ERROR StatusConsoleListener Could not create plugin of type class
     org.apache.logging.log4j.core.async.AsyncLoggerConfig$RootLogger
     for element AsyncRoot:
     java.lang.NoClassDefFoundError: com/lmax/disruptor/EventHandler
-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.4.4</version>
</dependency>

<!-- SLF4J -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>2.0.10</version>
</dependency>

Note: OnStartupTriggeringPolicy is configured for log rotation to keep data from each app run in a separate file.

log4j2-slf4j-logging-mvp's People

Contributors

sergpank avatar

Watchers

 avatar

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.