logger

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 13 Imported by: 0

README

Logger

The logger package implements the core logging functionality.

You can set your logger which implements the Logger interface or use the standard logger.

By default, the standard logger is initialized:

  • with the time format 2006/01/02 15:04:05.000;
  • with logging format FormatText;
  • at the highest logging level LevelTrace;
  • printing the name of the calling function false.

You can set the required time format, level, log message format and additional output, as in the following examples:

  • at initialization
package main

import (
	"os"

	"github.com/gromey/proto-rest/logger"
)

func main() {
	file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err = file.Close(); err != nil {
			panic(err)
		}
	}()

	logger.Info("Hello World!")
	// 2006/01/02 15:04:05.000 INFO [1] /../../main.go:21 Func: main() Hello World!

	lCfg := new(logger.Config)
	lCfg.TimeFormat = "2006-01-02 15:04:05"
	lCfg.Format = logger.FormatJSON
	lCfg.AdditionalOut = file
	lCfg.Level = logger.LevelInfo
	lCfg.FuncName = true

	logger.SetLogger(logger.New(lCfg))

	logger.Info("Hello World!")
	// {"time":"2006-01-02 15:04:05","level":"INFO","thread":1,"func":"main()","message":"Hello World!"}
}
  • after initialization
package main

import (
	"os"

	"github.com/gromey/proto-rest/logger"
)

func main() {
	l := logger.New(nil)
	logger.SetLogger(l)

	file, err := os.OpenFile("logs.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}

	defer func() {
		if err = file.Close(); err != nil {
			panic(err)
		}
	}()

	l.SetTimeFormat("2006-01-02 15:04:05")
	l.SetFormatter(logger.FormatJSON)
	l.SetAdditionalOut(file)
	l.SetLevel(logger.LevelInfo)
	l.SetFuncNamePrinting(false)

	logger.Info("Hello World!")
	// {"time":"2006-01-02 15:04:05","level":"INFO","thread":1,"message":"Hello World!"}
}

To reduce unnecessary memory allocations, it is recommended to check the logging level before calling the log function.

Example:

	if logger.InLevel(logger.LevelInfo) {
		logger.Info("Hello World!")
	}

Documentation

Index

Constants

View Source
const (
	FormatText format = iota
	FormatJSON
)

Variables

This section is empty.

Functions

func Closer added in v0.2.1

func Closer(c io.Closer)

Closer calls the Close method, if the closure occurred with an error, it prints it to the log.

func Debug

func Debug(v ...any)

func Debugf

func Debugf(format string, v ...any)

func DumpHttpRequest added in v0.0.2

func DumpHttpRequest(r *http.Request, logFunc func(v ...any))

DumpHttpRequest dumps the HTTP request and prints out with logFunc.

func DumpHttpResponse added in v0.0.2

func DumpHttpResponse(r *http.Response, logFunc func(v ...any))

DumpHttpResponse dumps the HTTP response and prints out with logFunc.

func Error

func Error(v ...any)

func Errorf

func Errorf(format string, v ...any)

func Fatal

func Fatal(v ...any)

func Fatalf

func Fatalf(format string, v ...any)

func FunctionInfo

func FunctionInfo(skip int) (string, string, int)

FunctionInfo returns the name of the function and file, the line number on the calling goroutine's stack. The argument skip is the number of stack frames to ascend.

func InLevel

func InLevel(level Level) bool

InLevel returns true if the given level is less than or equal to the current logger level.

func Info

func Info(v ...any)

func Infof

func Infof(format string, v ...any)

func SetLogger

func SetLogger(logger Logger)

func Trace

func Trace(v ...any)

func Tracef

func Tracef(format string, v ...any)

func Warn

func Warn(v ...any)

func Warnf

func Warnf(format string, v ...any)

Types

type Config

type Config struct {
	TimeFormat    string
	AdditionalOut io.Writer
	Format        format
	Level         Level
	FuncName      bool
}

type GoroutineID added in v0.2.0

type GoroutineID []byte

GoroutineID is an array of bytes representing the number of the current goroutine on the stack.

func GetGoroutineID added in v0.2.0

func GetGoroutineID() GoroutineID

GetGoroutineID gets the number of the current goroutine on the stack.

func (GoroutineID) String added in v0.2.1

func (i GoroutineID) String() string

func (GoroutineID) Uint64 added in v0.2.0

func (i GoroutineID) Uint64() uint64

Uint64 converts the GoroutineID value to uint64.

type Level

type Level uint32
const (
	LevelUnknown Level = iota
	LevelFatal
	LevelError
	LevelWarn
	LevelInfo
	LevelDebug
	LevelTrace
)

func ParseLevel added in v0.0.4

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the logger Level constant.

func (Level) Print added in v0.0.5

func (lvl Level) Print() func(...any)

Print returns a logger print function for current logger level.

func (Level) Printf added in v0.0.5

func (lvl Level) Printf() func(string, ...any)

Printf returns a logger printf function for current logger level.

func (Level) String added in v0.0.4

func (lvl Level) String() string

type Logger

type Logger interface {
	InLevel(lvl Level) bool
	Fatalf(format string, v ...any)
	Fatal(v ...any)
	Errorf(format string, v ...any)
	Error(v ...any)
	Warnf(format string, v ...any)
	Warn(v ...any)
	Infof(format string, v ...any)
	Info(v ...any)
	Debugf(format string, v ...any)
	Debug(v ...any)
	Tracef(format string, v ...any)
	Trace(v ...any)
}

type StdLogger

type StdLogger struct {
	// contains filtered or unexported fields
}

func New

func New(c *Config) *StdLogger

func (*StdLogger) Debug

func (l *StdLogger) Debug(v ...any)

func (*StdLogger) Debugf

func (l *StdLogger) Debugf(format string, v ...any)

func (*StdLogger) Error

func (l *StdLogger) Error(v ...any)

func (*StdLogger) Errorf

func (l *StdLogger) Errorf(format string, v ...any)

func (*StdLogger) Fatal

func (l *StdLogger) Fatal(v ...any)

func (*StdLogger) Fatalf

func (l *StdLogger) Fatalf(format string, v ...any)

func (*StdLogger) InLevel

func (l *StdLogger) InLevel(lvl Level) bool

func (*StdLogger) Info

func (l *StdLogger) Info(v ...any)

func (*StdLogger) Infof

func (l *StdLogger) Infof(format string, v ...any)

func (*StdLogger) SetAdditionalOut

func (l *StdLogger) SetAdditionalOut(out io.Writer)

SetAdditionalOut sets an additional logger output.

func (*StdLogger) SetFormatter

func (l *StdLogger) SetFormatter(f format)

SetFormatter sets the logger formatter.

func (*StdLogger) SetFuncNamePrinting

func (l *StdLogger) SetFuncNamePrinting(on bool)

SetFuncNamePrinting sets whether the logger should print the caller function name.

func (*StdLogger) SetLevel

func (l *StdLogger) SetLevel(level Level)

SetLevel sets the logger level.

func (*StdLogger) SetTimeFormat

func (l *StdLogger) SetTimeFormat(format string)

SetTimeFormat sets the logger time format.

func (*StdLogger) Trace

func (l *StdLogger) Trace(v ...any)

func (*StdLogger) Tracef

func (l *StdLogger) Tracef(format string, v ...any)

func (*StdLogger) Warn

func (l *StdLogger) Warn(v ...any)

func (*StdLogger) Warnf

func (l *StdLogger) Warnf(format string, v ...any)

Jump to

Keyboard shortcuts

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