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}." }
}
}
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 |