Categorygithub.com/zenoss/logri
modulepackage
0.0.0-20250117173636-01ab2b88d272
Repository: https://github.com/zenoss/logri.git
Documentation: pkg.go.dev

# README

Logri

Logri is a wrapper for Logrus that provides hierarchical, configurable, structured logging.

Like Logrus, it's a drop-in replacement for Go's standard logging library, but it adds the ability to:

  • Define loggers that inherit their log levels and output streams from parent loggers
  • Configure loggers from a YAML file
  • Update configuration on the fly
  • Optionally watch a logging configuration file for changes

And, of course, it retains Logrus's excellent structured logging capabilities.

Usage

You can drop Logri in to replace logging or Logrus very simply:

package main


import (
        "github.com/sirupsen/logrus"
    log "github.com/zenoss/logri"
)

func main() {

    log.Infof("Logri can replace the %s package", "logging")

    log.WithFields(logrus.Fields{
        "package": "logrus",
    }).Infof("Or another popular logging package")
}

Named loggers

The power of Logri comes in with named hierarchical loggers. Use logri.GetLogger(name) with a dotted name to return an individual logger that inherits its log level and outputs from its parent, but can add or override its own.

package main

import (
    "github.com/sirupsen/logrus"
    "github.com/zenoss/logri"
)

var (
    pkglog = logri.GetLogger("package")
    cmplog = logri.GetLogger("package.component")
    subcmplog = logri.GetLogger("package.component.subcomponent")
)

func main() {

    pkglog.SetLevel(logrus.DebugLevel, true) // Second argument makes it inherited
    // package.component and package.component.subcomponent are also Debug level now

    // Quiet package.component down but leave subcomponent at debug
    cmplog.SetLevel(logrus.ErrorLevel, false) // Second argument false means
                                              // local to this logger only
}

Further calls to logri.GetLogger(name) will retrieve the same logger instance, so there's no need to jump through hoops exporting loggers to share them among packages.

Configuration via file

You can also configure Logri using a YAML file. Given a file /etc/logging.conf with these contents:

- logger: '*'
  level: debug
  out:
  - type: stderr
  - type: file
    options:
      file: /var/log/app.log
- logger: package
  level: warn
  local: true
  out:
  - type: file
    local: true
    options:
      file: /var/log/package.warn.log
- logger: package.component
  level: error
  out:
  - type: file
    options:
      file: /var/log/package.component.error.log

You can configure the loggers defined above very simply:

logri.ApplyConfigFromFile("/etc/logging.conf")

That can be called at any time, and it will reconfigure loggers to match the config at that time, with no need to restart or recreate loggers.

You can also watch that file for changes, rather than listening for a signal to reload logging config:

package main

import (
    "github.com/zenoss/logri"
)

func main() {

    // Start watching the logging config for changes
    go logri.WatchConfigFile("/etc/logging.conf")

    doStuff()
}

# Functions

Adds the given hook to every logger in the tree.
ApplyConfig applies configuration to the default tree.
ApplyConfigFromFile reads logging configuration from a file and applies it to the default tree.
No description provided by the author
No description provided by the author
Debug logs a message at level Debug on the standard logger.
Debugf logs a message at level Debug on the standard logger.
Debugln logs a message at level Debug on the standard logger.
Error logs a message at level Error on the standard logger.
Errorf logs a message at level Error on the standard logger.
Errorln logs a message at level Error on the standard logger.
Fatal logs a message at level Fatal on the standard logger.
Fatalf logs a message at level Fatal on the standard logger.
Fatalln logs a message at level Fatal on the standard logger.
GetLogger returns a logger from the default tree with the given name.
No description provided by the author
Info logs a message at level Info on the standard logger.
Infof logs a message at level Info on the standard logger.
Infoln logs a message at level Info on the standard logger.
NewLoggerFromLogrus creates a new Logri logger tree rooted at a given Logrus logger.
Panic logs a message at level Panic on the standard logger.
Panicf logs a message at level Panic on the standard logger.
Panicln logs a message at level Panic on the standard logger.
Print logs a message at level Info on the standard logger.
Printf logs a message at level Info on the standard logger.
Println logs a message at level Info on the standard logger.
SetLevel sets the level of the root logger and its descendants.
Warn logs a message at level Warn on the standard logger.
Warnf logs a message at level Warn on the standard logger.
Warning logs a message at level Warn on the standard logger.
Warningf logs a message at level Warn on the standard logger.
Warningln logs a message at level Warn on the standard logger.
Warnln logs a message at level Warn on the standard logger.
WatchConfigFile watches a given config file, applying the config on change.
WithError creates an entry from the root logger and adds an error to it, using the value defined in ErrorKey as key.
WithField creates an entry from the root logger and adds a field to it.
WithFields creates an entry from the root logger and adds multiple fields to it.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
Used for tests only.

# Variables

No description provided by the author
No description provided by the author
ErrInvalidRootLevel is returned when a nil level is set on the root logger.
RootLogger is the default logger tree.

# Structs

Logger is the wrapper of a Logrus logger.
LoggerConfig is the configuration for a single logger.
LoggerHook is a Logrus hook that adds the logger name as a field to the entry.
No description provided by the author

# Type aliases

LogriConfig is the configuration for a logri manager.
No description provided by the author