Logger
A logger provides methods for logging at various log levels (info, warn, error, debug and trace). It has a logger name, typically the same as the class that the logger is attached to (e.g. com.example.Example
). The name is included in the log output, and can be used to enable/disable log levels for loggers based on their package names, or query for logs from a specific class.
The easiest way to construct a logger is by calling getLogger with zero arguments. This automatically gives the logger the name of its containing class (or file, if defined at the top level). See the "Implementation" section on getLogger's docstring for how this works.
// In file Example.kt
package com.example
import dev.hermannm.devlog.getLogger
// Gets the name "com.example.Example"
private val log = getLogger()
fun example() {
log.info { "Example message" }
}
Alternatively, you can provide a custom name to getLogger
. The name should follow fully qualified class name format, like com.example.Example
, to allow you to enable/disable log levels based on the package.
private val log = getLogger(name = "com.example.Example")
You can also pass a class to getLogger
, to give the logger the name of that class:
package com.example
class Example {
companion object {
// Gets the name "com.example.Example"
private val log = getLogger(Example::class)
}
}
Properties
Returns true if LogLevel.DEBUG is enabled for this logger.
Returns true if LogLevel.ERROR is enabled for this logger.
Returns true if LogLevel.INFO is enabled for this logger.
Returns true if LogLevel.TRACE is enabled for this logger.
Returns true if LogLevel.WARN is enabled for this logger.
Functions
Calls the given lambda to build a log message, and logs it at LogLevel.DEBUG, if enabled.
Calls the given lambda to build a log message, and logs it at LogLevel.ERROR, if enabled.
Calls the given lambda to build a log message, and logs it at LogLevel.INFO, if enabled.
Returns true if the given log level is enabled for this logger.
Calls the given lambda to build a log message, and logs it at the LogLevel.TRACE, if enabled.
Calls the given lambda to build a log message, and logs it at LogLevel.WARN, if enabled.