/logrus-modular

Modular logging for logrus

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

speijnik/logrus-modular

GoDoc Build Status codecov

Package speijnik/logrus-modular implements modular logging for logrus.

This allows creation of a hierarchy of loggers, whereas log-levels may be inherited. The purpose of this library is simplifying the handling of loggers which can be logically grouped and allowing configuration of log-levels on such logger groups.

Install

With a correctly configured Go toolchain:

go get -u gopkg.in/speijnik/logrus-modular.v1

Examples

package main

import (
	"os"

	"github.com/sirupsen/logrus"
	"gopkg.in/speijnik/logrus-modular.v1"
)

func main() {
	log := logrus.New()
	log.Level = logrus.DebugLevel
	log.Out = os.Stdout
	rootLogger := modular.NewRootLogger(log)
	testModule := rootLogger.GetOrCreateChild("test", logrus.InfoLevel)
	testTestModule := rootLogger.GetOrCreateChild("test.test", logrus.DebugLevel)
	// No-op, log level for "test" module is Info
	testModule.Debug("No-op, log-level is info")
	testModule.Info("Info message")
	testTestModule.Debug("Debug message of child module")
	// Logs "test2", log level for "test.test" module is Debug
	// Set level of "test" module to Info, which will propagate to child
	// module "test.test"
	testModule.SetLevel(logrus.InfoLevel)
	// No-op, log level for "test.test" module is Info
	testTestModule.Debug("No-op, SetLevel propagated Info level to child")
	testTestModule.Info("Another info message")
}