log4go

package module
v0.0.0-...-82a1720 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: Apache-2.0 Imports: 7 Imported by: 4

README

log4go

Go Report Card GoDoc codecov Sourcegraph Open Source Helpers Release TODOs

A log library for golang based on hashicorp/logutils

Why log4go

Yes, it another logging library for Go program language. Why do I create a new logging library for Go when there are so many popular logging library. such as:

For my daily use, i found several points are important for logging:

  • level for logging shoud be easy to use
  • content for logging should be friendly for human reading
  • logs should be easy to check

so i create log4go

Quick start

  1. Step 01: get the library

    $ go get -u github.com/liuliqiang/log4go
    
  2. Step 02: try in code:

    func main() {
    	log4go.Debug("I am debug log")
    	log4go.Info("Web server is started at %s:%d", "127.0.0.1", 80)
    	log4go.Warn("Get an empty http request")
    	log4go.Error("Failed to query record from db: %v", errors.New("db error"))
    }
    
  3. Step 03: Run it!

    $ go run main.go
    2019/08/10 00:02:18 [INFO]Web server is started at 127.0.0.1:80
    2019/08/10 00:02:18 [WARN]Get an empty http request
    2019/08/10 00:02:18 [EROR]Failed to query record from db: db error
    

Dynamic change log level

Just add a endpoint(such as http/grpc/...) to change log level such as:

http.HandleFunc("/logs", func(resp http.ResponseWriter, req *http.Request) {
	switch req.URL.Query()["level"][0] {
	case "debug":
		log4go.SetLevel(log4go.LogLevelDebug)
	case "info":
		log4go.SetLevel(log4go.LogLevelInfo)
	case "warning":
		log4go.SetLevel(log4go.LogLevelWarn)
	case "error":
		log4go.SetLevel(log4go.LogLevelError)
	}
	return
})

Learn more...

  • To be continue...
  • More examples at Examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	LogLevelTrace = logutils.LogLevel("TRCE") // trace log level
	LogLevelDebug = logutils.LogLevel("DBUG") // debug log level
	LogLevelInfo  = logutils.LogLevel("INFO") // info log level
	LogLevelWarn  = logutils.LogLevel("WARN") // warning log level
	LogLevelError = logutils.LogLevel("EROR") // error log level

)

Functions

func Debug

func Debug(format string, v ...interface{})

Logging debug log with default logger

func Error

func Error(format string, v ...interface{})

Logging error log with default logger

func Info

func Info(format string, v ...interface{})

Logging info log with default logger

func SetFilter

func SetFilter(filter *logutils.LevelFilter)

Set logger filter for default logger

func SetFlags

func SetFlags(flag int)

Set logger flag for default logger

func SetLevel

func SetLevel(level logutils.LogLevel)

Set log level for default logger

func Trace

func Trace(format string, v ...interface{})

Logging strace log with default logger

func Warn

func Warn(format string, v ...interface{})

Logging warning log with default logger

Types

type Logger

type Logger interface {
	Trace(ctx context.Context, format string, v ...interface{})
	Debug(ctx context.Context, format string, v ...interface{})
	Info(ctx context.Context, format string, v ...interface{})
	Warn(ctx context.Context, format string, v ...interface{})
	Error(ctx context.Context, format string, v ...interface{})

	SetFlags(flags int)
	SetFilter(filter *logutils.LevelFilter)
	GetFilter() (filter *logutils.LevelFilter)

	WithField(field, val string) Logger
}

Interface for logger, you can implement your own logger with this.

func DefaultLogger

func DefaultLogger() (logger Logger)

Return a default logger

func NewLogger

func NewLogger(name string, opts *LoggerOpts) (logger Logger)

Create a named logger with specify options.

type LoggerOpts

type LoggerOpts struct {
	WithId bool
	IdName interface{}
}

Options for logger.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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