Logger

value class Logger(source)

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

Link copied to clipboard

Returns true if LogLevel.DEBUG is enabled for this logger.

Link copied to clipboard

Returns true if LogLevel.ERROR is enabled for this logger.

Link copied to clipboard

Returns true if LogLevel.INFO is enabled for this logger.

Link copied to clipboard

Returns true if LogLevel.TRACE is enabled for this logger.

Link copied to clipboard

Returns true if LogLevel.WARN is enabled for this logger.

Functions

Link copied to clipboard
inline fun at(level: LogLevel, cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at the given LogLevel, if enabled. This is useful when setting the log level dynamically, instead of calling info/warn/error/debug/trace conditionally.

Link copied to clipboard
inline fun debug(cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at LogLevel.DEBUG, if enabled.

Link copied to clipboard
inline fun error(cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at LogLevel.ERROR, if enabled.

Link copied to clipboard
inline fun info(cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at LogLevel.INFO, if enabled.

Link copied to clipboard

Returns true if the given log level is enabled for this logger.

Link copied to clipboard
inline fun trace(cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at the LogLevel.TRACE, if enabled.

Link copied to clipboard
inline fun warn(cause: Throwable? = null, crossinline buildLog: LogBuilder.() -> String)

Calls the given lambda to build a log message, and logs it at LogLevel.WARN, if enabled.