import org.apache.logging.log4j.kotlin.Logging
class MyClass: BaseClass, Logging {
fun doStuff() {
logger.info("Doing stuff")
}
fun doStuffWithUser(user: User) {
logger.info { "Doing stuff with ${user.name}." }
}
}
Apache Log4j Kotlin APITable of Contents
Log4j Kotlin API uses Log4j 2.x as its logging backend. Log4j Kotlin API uses Log4j 2.x as its logging backend by default, but this can also be replaced with compatible libraries (e.g., Logback). While this library is not required to use Log4j API in Kotlin, it does provide idiomatic Kotlin APIs which are friendlier to use in Kotlin programs than the Java APIs. UsageUsing the Kotlin API is as simple as mixing in the
The
Alternatively, a more traditional style can be used to instantiate a logger instance:
The function API DocumentationSee KDocs. ConfigurationLog4j Kotlin API uses Log4j configuration by default. This supports XML, properties files, and Java-based builders, as well as JSON and YAML with additional dependencies. Substituting ParametersUnlike Java, Kotlin provides native functionality for string templates. However, using a string template still incurs the message construction cost if the logger level is not enabled. To avoid this, prefer passing a lambda which won’t be evaluated until necessary. For example:
Logger NamesMost logging implementations use a hierarchical scheme for matching logger names with logging configuration. In this scheme the logger name hierarchy is represented by '.' characters in the logger name, in a fashion very similar to the hierarchy used for Java/Kotlin package names. The The value returned when calling the
Explicitly Named LoggersAn explicitly-named logger may be obtained via the
This is also needed in scopes that do not have a Unresolved directive in <stdin> - include::download.adoc[] Unresolved directive in <stdin> - include::changes.adoc[] Building From Source
Building
ContributingYou have found a bug or you have an idea for a cool new feature? Contributing code is a great way to give something back to the open source community. Before you dig right into the code there are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things. Getting Started
Making Changes
Making Trivial ChangesFor changes of a trivial nature to comments and documentation, it is not always necessary to create a new ticket in JIRA. In this case, it is appropriate to start the first line of a commit with '(doc)' instead of a ticket number. Submitting Changes
|