Skip to content

kxtra/kxtra-slf4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kxtra-slf4j Build Status

Kotlin extensions for SLF4J

Easy Logger creation

private val logger = getLogger()

Free lazy evaluation

logger.debug { "$message only evaluated if debug is enabled" }

Requires Java 7+

Contains only synthetic inline functions and thus only needs to be present at compile-time and not runtime

Getting a Logger

Static

class MyClass {
    companion object {
        private val logger = getLogger()
    }
}

Note: The following naive method (among others) does not properly use the name of the class, it instead uses the name of the companion object

class MyClass {
    companion object {
        private val logger = LoggerFactory.getLogger(javaClass)
    }
}

Top level

private val logger = getLogger()

Note: It is not easy to otherwise get the name of the enclosing file from the top level

Custom name

private val logger = getLogger("custom-name")

Instance

abstract class MyClass {
    protected val declarationLogger = getLogger()
    
    protected val classLogger = getLogger(javaClass)
}

class MySubClass : MyClass() {
    // assert(declarationLogger.name == MyClass::class.java.name)
    // assert(classLogger.name == MySubClass::class.java.name)
}

Lazy Evaluation

logger.debug { "$expensive $message" }

The lambda expression will be inlined and only evaluated if the logging level is enabled

Extensions are provided for every combination of logging level and arguments

logger.info(marker) { "$msg" }
logger.warn(exception) { "$msg" }
logger.error(marker, exception) { "$msg" }

Including

Note: SLF4J requires a binding!

Source - A single file which can be manually added to any project

Maven

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>org.kxtra</groupId>
        <artifactId>kxtra-slf4j</artifactId>
        <version>2.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.28</version>
    </dependency>
</dependencies>

Gradle

repositories {
    maven { url 'https://jitpack.io' }
}
dependencies {
    compileOnly 'org.kxtra:kxtra-slf4j:2.1.0'
    api 'org.slf4j:slf4j-api:1.7.28'
}