SLF4J bridge to Bukkit's logger for plugin.
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.
The library is available in the Maven Central Repository.
<dependency>
<groupId>com.djaytan.bukkit</groupId>
<artifactId>bukkit-slf4j</artifactId>
<version>2.0.0</version>
</dependency>
implementation group: 'com.djaytan.bukkit', name: 'bukkit-slf4j', version: '2.0.0'
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.
Please read CONTRIBUTING.md for details on ways to help us.
Take care to always follow our CODE_OF_CONDUCT.md.
We use SemVer for versioning. For the versions available, see the tags on this repository.
In case you think having found a security vulnerability, please consult our Security Policy.
This project is under the MIT license.