Skip to content

Djaytan/bukkit-slf4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bukkit-slf4j

Maven metadata URL CI Quality Gate Status Coverage
semantic-release: conventional-commits OpenSSF Best Practices OpenSSF Scorecard

SLF4J bridge to Bukkit's logger for plugin.

Why Using It

Even if PaperMC provides a getSLF4JLogger() method, you need to inject the retrieved instance in any class that need to log something. When it's time to deal with libraries like HikariCP, things become even harder when wanted to have clean console output when plugin is running. Finally, the previously mentioned method is not available with Spigot.

This solution goes beyond by overcoming all these limitations with a simple approach highly inspired from the slf4j-jdk14 one.

Setup

The library is available in the Maven Central Repository.

Maven

<dependency>
  <groupId>com.djaytan.bukkit</groupId>
  <artifactId>bukkit-slf4j</artifactId>
  <version>2.0.0</version>
</dependency>

Gradle

implementation group: 'com.djaytan.bukkit', name: 'bukkit-slf4j', version: '2.0.0'

How To Use

Once added, you simply need to pass the Bukkit logger when enabling plugin like as follows:

public class YourPlugin extends JavaPlugin {

  @Override
  public void onEnable() {
    // It's important to call this method as soon as possible, especially before loading any class
    BukkitLoggerFactory.provideBukkitLogger(this.getLogger());

    // Then execute your plugin's specific logic
  }
}

Then you can simply declare a new logger as follows:

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

public class MyClass {

  // The Bukkit logger will be injected
  private static final Logger log = LoggerFactory.getLogger(LogExample.class);

  public void dummyMethod() {
    log.atInfo().log("Bukkit x SLF4J");
  }
}

Or if using Lombok's @Slf4j annotation:

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyClass {

  public void dummyMethod() {
    log.atInfo().log("Bukkit x SLF4J");
  }
}

You can find a concrete example of Bukkit plugin using this extension here.

Contributing

Please read CONTRIBUTING.md for details on ways to help us.

Take care to always follow our CODE_OF_CONDUCT.md.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Security Policy

In case you think having found a security vulnerability, please consult our Security Policy.

Licence

This project is under the MIT license.