env_logger

package module
v0.1.28 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 15 Imported by: 17

README

env_logger

This is a super simple project which aims to help out with setting up logging correctly in your project. It is a true drop in replacement for logrus logger atm.

Usage

The entire logging framework is configured via a single environment variable LOG. The variable is a comma delimited list of packages and their respective log-levels. (falling back to InfoLevel if not configured).

Windows support

This logger should be fully able to work colored on windows! TTY detection may fail though, so to ensure that it does not set the environment variable CLICOLOR_FORCE=1 in your shell.

Bonus tricks

Some bonus modifiers exist for the log config:

  • ln enables printing of line numbers
  • gr adds number of goroutines to each log statement
  • grl adds number of goroutines to each log statement and starts a loop printing the number of routines every second
  • pp enables pprof and dynamic log config via http requests on 11111, port can be changed with ppport= (all of this requires the package to be built with -tags logpprof). The endpoint for the logconfig is POST /logstring. Send the new logstring as body
  • mut=10 allows to set runtime.SetMutexProfileFraction(val)
  • blk=10 allows to set runtime.SetBlockProfileFraction(val)

Bonus functions

  • log.Must, log.MustFatal if the passed error is not nil log it and throw a panic or end the program
  • log.Should, log.ShouldWarn if the passed error is not nul just log it, returns true if error has been printed
  • log.Wrap can be used with Should and must functions to provide additional error information (eg: log.Should(log.Wrap(err, "on testing %s", somedata)))
  • log.ShouldWrap convenience for the above
  • log.Indent can be used to prety print the public fields of a structure (eg: log.Info(log.Indent(myStructure)))
  • log.Timer and log.TimerEnd can be used to quickly measure the time between 2 places with a key, similar to js. this does not log on its own, use with one of the standard log functions (just like .Indent above)

Dynamic log config

If pp is active and tags logpprof have been set use this command to change the logconfig dynamically

curl -X POST -d 'grl' http://localhost:11111/logstring

Examples

LOG=foo=debug,bar=warn go run

This configures the foo package at loglevel Debug, the bar package at loglevel Warn and the default/fallback logger at Info.

LOG=foo=info,debug,bar=warn go run

This is the same as the previous example, except foo is now at loglevel Info, and the default loglevel is Debug.

LOG=debug go run

This example sets everything to Debug.

Documentation

Index

Constants

View Source
const (
	TraceV = iota
	DebugV = iota
	InfoV  = iota
	WarnV  = iota
	ErrV   = iota
	FatalV = iota
	PanicV = iota
)

Variables

This section is empty.

Functions

func AutoStartProfileServer added in v0.1.26

func AutoStartProfileServer(port uint16)

func ConfigureAllLoggers

func ConfigureAllLoggers(newdefaultLogger *logrus.Logger, debugConfig string)

ConfigureLogger takes in a logger object and configures the logger depending on environment variables. Configured based on the GOLANG_DEBUG environment variable

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(format string, args ...interface{})

func Debugln

func Debugln(args ...interface{})

func EnableLineNumbers

func EnableLineNumbers()

EnableLineNumbers log output of linenumbers as logerus fields

func Error

func Error(args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Errorln

func Errorln(args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(format string, args ...interface{})

func Fatalln

func Fatalln(args ...interface{})

func Indent added in v0.1.17

func Indent(arg interface{}) string

Indent transforms the structure into json by using MarshalIndent

func Info

func Info(args ...interface{})

func Infof

func Infof(format string, args ...interface{})

func Infoln

func Infoln(args ...interface{})

func ListModules added in v0.1.28

func ListModules()

FUnction to list all modules that have logged ? kinda hard to do...

func Log

func Log(level logrus.Level, args ...interface{})

func Logf

func Logf(level logrus.Level, format string, args ...interface{})

func Logln

func Logln(level logrus.Level, args ...interface{})

func Must

func Must(err error)

Must Checks if an error occured, otherwise panic

func MustFatal added in v0.1.12

func MustFatal(err error)

MustFatal Checks if an error occured, otherwise stop the program

func Panic

func Panic(args ...interface{})

func PanicHandler added in v0.1.22

func PanicHandler()

Wrap an error, this is useful in combination with Should and Must

func Panicf

func Panicf(format string, args ...interface{})

func Panicln

func Panicln(args ...interface{})

func Print

func Print(args ...interface{})

func Printf

func Printf(format string, args ...interface{})

func Println

func Println(args ...interface{})

func SetGlobalDebugConfig added in v0.1.28

func SetGlobalDebugConfig(debugConfig string)

SetGlobalDebugConfig overrides the debug config, but with default logger at runtime

func SetLevel

func SetLevel(level logrus.Level)

SetLevel sets the default loggers level

func Should

func Should(err error) bool

Should Checks if an error occured, otherwise prints it as error, returns true if error is not nil

func ShouldWarn

func ShouldWarn(err error) bool

ShouldWarn Checks if an error occured, otherwise prints it as warning, returns true if error is not nil

func ShouldWrap added in v0.1.26

func ShouldWrap(err error, msg string, args ...interface{}) bool

Should Checks if an error occured, otherwise prints it as error, returns true if error is not nil

func Timer added in v0.1.26

func Timer(idkey string) string

func TimerEnd added in v0.1.26

func TimerEnd(idkey string) string

Print time since the last call to the Time function with the same name

func Trace

func Trace(args ...interface{})

func Tracef

func Tracef(format string, args ...interface{})

func Traceln

func Traceln(args ...interface{})

func Warn

func Warn(args ...interface{})

Warn prints a warning...

func Warnf

func Warnf(format string, args ...interface{})

func Warnln

func Warnln(args ...interface{})

func Wrap added in v0.1.17

func Wrap(err error, msg string, args ...interface{}) error

Wrap an error, this is useful in combination with Should and Must

func WrapFinal added in v0.1.20

func WrapFinal(err *error, msg string, args ...interface{})

Wrap an error, this is useful in combination with Should and Must

Types

type Entry added in v0.1.22

type Entry logrus.Entry

func GetLoggerForPrefix

func GetLoggerForPrefix(prefix string) *Entry

GetLoggerForPrefix gets the logger for a certain prefix if it has been configured

func WithError

func WithError(err error) *Entry

func WithField

func WithField(key string, value interface{}) *Entry

func WithFields

func WithFields(fields logrus.Fields) *Entry

func (*Entry) Debug added in v0.1.22

func (e *Entry) Debug(args ...interface{})

func (*Entry) Debugf added in v0.1.22

func (e *Entry) Debugf(format string, args ...interface{})

func (*Entry) Debugln added in v0.1.22

func (e *Entry) Debugln(args ...interface{})

func (*Entry) Error added in v0.1.22

func (e *Entry) Error(args ...interface{})

func (*Entry) Errorf added in v0.1.22

func (e *Entry) Errorf(format string, args ...interface{})

func (*Entry) Errorln added in v0.1.22

func (e *Entry) Errorln(args ...interface{})

func (*Entry) Fatal added in v0.1.22

func (e *Entry) Fatal(args ...interface{})

func (*Entry) Fatalf added in v0.1.22

func (e *Entry) Fatalf(format string, args ...interface{})

func (*Entry) Fatalln added in v0.1.22

func (e *Entry) Fatalln(args ...interface{})

func (*Entry) Indent added in v0.1.23

func (e *Entry) Indent(arg interface{}) string

func (*Entry) Info added in v0.1.22

func (e *Entry) Info(args ...interface{})

func (*Entry) Infof added in v0.1.22

func (e *Entry) Infof(format string, args ...interface{})

func (*Entry) Infoln added in v0.1.22

func (e *Entry) Infoln(args ...interface{})

func (*Entry) Log added in v0.1.22

func (e *Entry) Log(level logrus.Level, args ...interface{})

func (*Entry) Logf added in v0.1.22

func (e *Entry) Logf(level logrus.Level, format string, args ...interface{})

func (*Entry) Logln added in v0.1.22

func (e *Entry) Logln(level logrus.Level, args ...interface{})

func (*Entry) Must added in v0.1.22

func (e *Entry) Must(err error)

Must Checks if an error occured, otherwise panic

func (*Entry) MustFatal added in v0.1.22

func (e *Entry) MustFatal(err error)

MustFatal Checks if an error occured, otherwise stop the program

func (*Entry) Panic added in v0.1.22

func (e *Entry) Panic(args ...interface{})

func (*Entry) PanicHandler added in v0.1.23

func (e *Entry) PanicHandler()

func (*Entry) Panicf added in v0.1.22

func (e *Entry) Panicf(format string, args ...interface{})

func (*Entry) Panicln added in v0.1.22

func (e *Entry) Panicln(args ...interface{})

func (*Entry) Print added in v0.1.22

func (e *Entry) Print(args ...interface{})

func (*Entry) Printf added in v0.1.22

func (e *Entry) Printf(format string, args ...interface{})

func (*Entry) Println added in v0.1.22

func (e *Entry) Println(args ...interface{})

func (*Entry) Should added in v0.1.22

func (e *Entry) Should(err error) bool

Should Checks if an error occured, otherwise prints it as error, returns true if error is not nil

func (*Entry) ShouldWarn added in v0.1.22

func (e *Entry) ShouldWarn(err error) bool

ShouldWarn Checks if an error occured, otherwise prints it as warning, returns true if error is not nil

func (*Entry) ShouldWrap added in v0.1.26

func (e *Entry) ShouldWrap(err error, msg string, args ...interface{}) bool

ShouldWrap Checks if an error occured, otherwise prints it as error, returns true if error is not nil

func (*Entry) Timer added in v0.1.26

func (e *Entry) Timer(idkey string) string

func (*Entry) TimerEnd added in v0.1.26

func (e *Entry) TimerEnd(idkey string) string

func (*Entry) Trace added in v0.1.22

func (e *Entry) Trace(args ...interface{})

func (*Entry) Tracef added in v0.1.22

func (e *Entry) Tracef(format string, args ...interface{})

func (*Entry) Traceln added in v0.1.22

func (e *Entry) Traceln(args ...interface{})

func (*Entry) Warn added in v0.1.22

func (e *Entry) Warn(args ...interface{})

Warn prints a warning...

func (*Entry) Warnf added in v0.1.22

func (e *Entry) Warnf(format string, args ...interface{})

func (*Entry) Warnln added in v0.1.22

func (e *Entry) Warnln(args ...interface{})

func (*Entry) WithError added in v0.1.22

func (e *Entry) WithError(err error) *Entry

func (*Entry) WithField added in v0.1.22

func (e *Entry) WithField(key string, value interface{}) *Entry

func (*Entry) WithFields added in v0.1.22

func (e *Entry) WithFields(fields logrus.Fields) *Entry

func (*Entry) Wrap added in v0.1.23

func (e *Entry) Wrap(err error, msg string, args ...interface{}) error

func (*Entry) WrapFinal added in v0.1.23

func (e *Entry) WrapFinal(err *error, msg string, args ...interface{})

type Fields added in v0.1.20

type Fields logrus.Fields

Pass through type to not have another import in packages using this lib

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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