modular

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2017 License: BSD-3-Clause Imports: 4 Imported by: 1

README

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")
}

Documentation

Overview

Package modular provides modular logging for logrus

Index

Constants

View Source
const DefaultModuleField = "module"

DefaultModuleField defines the default field to use for the module name

Variables

View Source
var (
	// ErrChildExists denotes that a child logger already exists
	ErrChildExists = errors.New("Child logger exists")
	// ErrChildNotFound denotes that a child logger was not found
	ErrChildNotFound = errors.New("Child logger not found")
)

Functions

This section is empty.

Types

type Logger

type Logger interface {
	// WithField extends the current logger's fields with the given field and value and returns a new logger
	WithField(key string, value interface{}) Logger
	// WithFields extends the current logger's fields with the given fields and returns a new logger
	WithFields(fields logrus.Fields) Logger
	// WithError extends the current logger's fields with an error field and returns a new logger
	WithError(err error) Logger

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})

	// GetModuleLogger returns the associated ModuleLogger
	GetModuleLogger() ModuleLogger
}

Logger defines the baseline logger interface

type ModuleLogger

type ModuleLogger interface {
	Logger

	// GetModuleName returns the (full) module name
	GetModuleName() string

	// SetLevel sets the module's log level to the given level and recursively propagates this change
	// to all children.
	SetLevel(level logrus.Level)
	// GetLevel returns the module's log level
	GetLevel() logrus.Level

	// GetRoot returns the associated RootLogger
	GetRoot() RootLogger

	// GetChild returns the child with the given name
	GetChild(moduleName string) (ModuleLogger, error)
	// CreateChild creates a child with the given name
	CreateChild(moduleName string, defaultLevel logrus.Level) (ModuleLogger, error)
	// GetOrCreateChild tries returns an existing child or creates it, if it is missing
	GetOrCreateChild(moduleName string, defaultLevel logrus.Level) ModuleLogger
}

ModuleLogger defines the interface implemented by a module logger

type RootLogger

type RootLogger interface {
	ModuleLogger

	// GetLogger returns the underlying logrus.Logger
	GetLogger() *logrus.Logger

	// GetModuleField returns the name of the module field
	GetModuleField() string
	// SetModuleField sets the module field.
	// Sets field to DefaultModuleField if empty string is passed in.
	SetModuleField(field string)
}

RootLogger defines the interface implemented by a root logger

func NewRootLogger

func NewRootLogger(logger *logrus.Logger) RootLogger

NewRootLogger creates a new root logger, that wraps the passed logrus.Logger

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL