Skip to content

Configure multiple loggers with yaml #49

@Fuco1

Description

@Fuco1

I think that configuring only a single logger with yaml is not very usable. Ideally, I'd be able to configure the entire hierarchy with a single yaml file, including the logger names.

I came up with this simple function to do that, maybe we can find a way to integrate it into the package.

Basically, it uses the same format as the current yaml configuration except the additional children keyword which contains the nested loggers. The Logger keyword is omitted and instead the logger name is used directly.

configure_loggers <- function(config, parent_name = NULL) {
    for (i in seq_along(config)) {
        name <- paste(c(parent_name, names(config[i])), collapse = "/")
        value <- config[i][[1]]
        children <- value$children

        value$children <- NULL
        lg_config <- lgr::as_logger_config(list(Logger = value))
        lgr::get_logger(name)$config(lg_config)

        configure_loggers(children, name)
    }
}

Example config:

## loggers.yaml
mypackage:
  threshold: info
  children:
    db:
      threshold: warn
    model:
      threshold: info
      children:
        user:
          threshold: debug

Usage:

configure_loggers(yaml::read_yaml('loggers.yaml'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions