logger

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT Imports: 27 Imported by: 0

README

Logger pakcage

Help manage logger. This package does not implement a logger but user logrus as logger behind. This package will simplify call of logger and allow more features like *log.Logger wrapper.

Exmaple of implement

In your file, first add the import of golib/logger :

	import liblog "github.com/nabbar/golib/logger"

Initialize the logger like this

	log := liblog.New()
	log.SetLevel(liblog.InfoLevel)

	if err := l.SetOptions(context.TODO(), &liblog.Options{
		DisableStandard:  false,
		DisableStack:     false,
		DisableTimestamp: false,
		EnableTrace:      false,
		TraceFilter:      "",
		DisableColor:     false,
		LogFile: []liblog.OptionsFile{
			{
				LogLevel: []string{
					"panic",
					"fatal",
					"error",
					"warning",
					"info",
					"debug",
				},
				Filepath:         "/path/to/my/logfile-with-trace",
				Create:           true,
				CreatePath:       true,
				FileMode:         0644,
				PathMode:         0755,
				DisableStack:     false,
				DisableTimestamp: false,
				EnableTrace:      true,
			},
		},
	}); err != nil {
		panic(err)
	}

Calling log like this :

	log.Info("Example log", nil, nil)
    
	// example with a struct name o that you want to expose in log
	// and an list of error : err1, err2 and err3
	log.LogDetails(liblog.InfoLevel, "example of detail log message with simple call", o, []error{err1, err2, err3}, nil, nil)
    

Having new log based on last logger but with some pre-defined information

    l := log.Clone(context.TODO())    
    l.SetFields(l.GetFields().Add("one-key", "one-value").Add("lib", "myLib").Add("pkg", "some-package"))
    l.Info("Example log with pre-define information", nil, nil)
    // will print line like : level=info fields.level=Info fields.time="2021-05-25T13:10:02.8033944+02:00" lib=myLib message="Example log with pre-define information" pkg=some-package stack=924 one-key=one-value
    
    // Override the field value on one log like this 
    l.LogDetails(liblog.InfoLevel, "example of detail log message with simple call", o, []error{err1, err2, err3}, liblog.NewFields().Add("lib", "another lib"), nil)
    // will print line like : level=info fields.level=Info fields.time="2021-05-25T13:10:02.8033944+02:00" lib="another lib" message="Example log with pre-define information" pkg=some-package stack=924 one-key=one-value

Implement other logger to this logger

Plug the SPF13 (Cobra / Viper) logger to this logger like this

   log.SetSPF13Level(liblog.InfoLevel, logSpf13)

Plug the Hashicorp logger hclog with the logger like this

   log.SetHashicorpHCLog()

Or get a hclog logger from the current logger like this

   hlog := log.NewHashicorpHCLog()

This call, return a go *log.Logger interface

   l := log.Clone(context.TODO())
   l.SetFields(l.GetFields().Add("one-key", "one-value").Add("lib", "myLib").Add("pkg", "some-package"))
   glog := l.GetStdLogger(liblog.ErrorLevel, log.LstdFlags|log.Lmicroseconds)

This call, will connect the default go *log.Logger

   log.SetStdLogger(liblog.ErrorLevel, log.LstdFlags|log.Lmicroseconds)

Documentation

Index

Constants

View Source
const (
	KeyCancel
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncLog added in v1.7.0

type FuncLog func() Logger

type Logger added in v1.7.0

type Logger interface {
	io.WriteCloser

	//SetLevel allow to change the minimal level of log message
	SetLevel(lvl loglvl.Level)

	//GetLevel return the minimal level of log message
	GetLevel() loglvl.Level

	//SetIOWriterLevel allow to change the minimal level of log message for io.WriterCloser interface
	SetIOWriterLevel(lvl loglvl.Level)

	//GetIOWriterLevel return the minimal level of log message for io.WriterCloser interface
	GetIOWriterLevel() loglvl.Level

	// SetIOWriterFilter allow to filter message that contained the given pattern. If the pattern is found, the log is drop.
	SetIOWriterFilter(pattern string)

	//SetOptions allow to set or update the options for the logger
	SetOptions(opt *logcfg.Options) error

	//GetOptions return the options for the logger
	GetOptions() *logcfg.Options

	//SetFields allow to set or update the default fields for all logger entry
	// Fields are custom information added into log message
	SetFields(field logfld.Fields)

	//GetFields return the default fields for all logger entry
	// Fields are custom information added into log message
	GetFields() logfld.Fields

	//Clone allow to duplicate the logger with a copy of the logger
	Clone() Logger

	//SetSPF13Level allow to plus spf13 logger (jww) to this logger
	SetSPF13Level(lvl loglvl.Level, log *jww.Notepad)

	//GetStdLogger return a golang log.logger instance linked with this main logger.
	GetStdLogger(lvl loglvl.Level, logFlags int) *log.Logger

	//SetStdLogger force the default golang log.logger instance linked with this main logger.
	SetStdLogger(lvl loglvl.Level, logFlags int)

	//Debug add an entry with DebugLevel to the logger
	Debug(message string, data interface{}, args ...interface{})

	//Info add an entry with InfoLevel to the logger
	Info(message string, data interface{}, args ...interface{})

	//Warning add an entry with WarnLevel to the logger
	Warning(message string, data interface{}, args ...interface{})

	//Error add an entry with ErrorLevel level to the logger
	Error(message string, data interface{}, args ...interface{})

	//Fatal add an entry with FatalLevel to the logger
	//The function will break the process (os.exit) after log entry.
	Fatal(message string, data interface{}, args ...interface{})

	//Panic add an entry with PanicLevel level to the logger
	//The function will break the process (os.exit) after log entry.
	Panic(message string, data interface{}, args ...interface{})

	//LogDetails add an entry to the logger
	LogDetails(lvl loglvl.Level, message string, data interface{}, err []error, fields logfld.Fields, args ...interface{})

	//CheckError will check if a not nil error is given and if yes, will add an entry to the logger.
	// Othwise if the lvlOK is given (and not NilLevel) the function will add entry and said ok
	CheckError(lvlKO, lvlOK loglvl.Level, message string, err ...error) bool

	//Entry will return an entry struct to manage it (set gin context, add fields, log the entry...)
	Entry(lvl loglvl.Level, message string, args ...interface{}) logent.Entry

	//Access will return an entry struct to store info level access log message
	Access(remoteAddr, remoteUser string, localtime time.Time, latency time.Duration, method, request, proto string, status int, size int64) logent.Entry
}

func New added in v1.7.0

func New(ctx libctx.FuncContext) Logger

New return a new logger interface pointer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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