logger

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 16 Imported by: 12

README

logger

CircleCI godoc license

Efficient and simple logger for golang with more features supported.

  • custom log levels
  • custom log tags
  • custom log colors (only worked for *NIX)
  • custom log output and output format
  • structured log

Install

go get -u github.com/dolab/logger

Usage

package main

import "github.com/dolab/logger"

func main() {
    log, _ := logger.New("stdout")
    log.SetLevel(logger.Ldebug)

    // normal
    log.Debug("Hello, logger!")
    log.Infof("Hello, %s!", "logger")

    // create new logger with tags based on log
    taggedLog := log.New("X-REQUEST-ID")
    taggedLog.Debug("Receive HTTP request")
    taggedLog.Warnf("Send response with %d.", 200)
    
    // or use struct log
    textLog := log.NewTextLogger()
    textLog.Str("key", "value").Err(err, true).Error("it's for demo")
}

Output

  • stdout = os.Stdout
  • stderr = os.Stderr
  • null | nil = os.DevNull
  • path/to/file = os.OpenFile("path/to/file", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)

Level

  • Ldebug = DEBUG
  • Linfo = INFO
  • Lwarn = WARN
  • Lerror = ERROR
  • Lfatal = FATAL
  • Lpanic = PANIC
  • Ltrace = Stack

License

MIT

Author

Spring MC

Documentation

Index

Constants

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

Variables

View Source
var (
	ErrLevel = errors.New("Invalid level")
)

Functions

This section is empty.

Types

type Attr

type Attr func(as *attrs)

Attr for fields option

func Any

func Any(key string, value any) Attr

Any for any type field option.

func Bool

func Bool(key string, value bool) Attr

Bool is shortcut for bool field option.

func Duration added in v1.3.0

func Duration(key string, value time.Duration) Attr

Duration is shortcut for time.Duration field option.

func Err added in v1.1.0

func Err(err error) Attr

Err is shortcut for error field option. NOTE: It uses error for the key forced!

func String

func String(key, value string) Attr

String is shortcut for string field option.

func Time added in v1.3.0

func Time(key string, value time.Time) Attr

Time is shortcut for time.Time field option.

type Formatter

type Formatter int
const (
	TextFormat Formatter = iota
	JSONFormat
)

type Level

type Level int
const (
	Ldebug Level
	Linfo
	Lwarn
	Lerror
	Lfatal
	Lpanic
	Ltrace
	Llog
)

func ResolveLevelByName

func ResolveLevelByName(name string) Level

Resolves level by name, returns lmin without definition by default

func (Level) IsValid

func (l Level) IsValid() bool

func (Level) String

func (l Level) String() string

type Logger

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

func New

func New(output string) (*Logger, error)

New creates a logger with the requested output. (default to stderr) NOTE: available outputs are [stdout|stderr|null|nil|path/to/file]

func (*Logger) AddTags

func (l *Logger) AddTags(tags ...string)

AddTags adds new tags to all logs, duplicated tags will be ignored.

func (*Logger) Debug

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

Debug calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

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

Debugf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Error

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

Error calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

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

Errorf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Fatal

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

Fatal calls l.Output to print to the logger and exit process with sign 1. Arguments are handled in the manner of fmt.Print.

func (*Logger) Fatalf

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

Fatalf calls l.Output to print to the logger and exit process with sign 1. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Flag

func (l *Logger) Flag() int

func (*Logger) Info

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

Info calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

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

Infof calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Level

func (l *Logger) Level() Level

func (*Logger) Log added in v1.5.0

func (l *Logger) Log(ctx context.Context, slogLevel slog.Level, msg string, args ...any)

Log drops in of slog.Log

func (*Logger) LogAttrs added in v1.5.0

func (l *Logger) LogAttrs(ctx context.Context, slogLevel slog.Level, msg string, slogAttrs ...slog.Attr)

LogAttrs drops in of slog.LogAttrs

func (*Logger) New

func (l *Logger) New(tags ...string) *Logger

New allocates a new Logger for given tags shared.

func (*Logger) NewJsonLogger

func (l *Logger) NewJsonLogger(attrs ...Attr) StructLogger

NewJsonLogger returns a new StructLogger with json formatter.

func (*Logger) NewTextLogger

func (l *Logger) NewTextLogger(attrs ...Attr) StructLogger

NewTextLogger returns a new StructLogger with text formatter.

func (*Logger) Output

func (l *Logger) Output(level Level, msg string) error

Output writes the output for a logging event. The string s contains the text to print after the tags specified by the flags of the Logger. A newline is appended if the last character of s is not already a newline.

func (*Logger) Panic

func (l *Logger) Panic(v ...any)

Panic calls l.Output to print to the logger and panic process. Arguments are handled in the manner of fmt.Print.

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...any)

Panicf calls l.Output to print to the logger and panic process. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Print

func (l *Logger) Print(v ...any)

Print calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...any)

Printf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) SetColor

func (l *Logger) SetColor(colorful bool)

SetColor sets whether output logs with colorful

func (*Logger) SetFlag

func (l *Logger) SetFlag(flag int)

SetFlag changes flag of source file path format

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level) error

SetLevel sets min level of output

func (*Logger) SetLevelByName

func (l *Logger) SetLevelByName(name string) error

SetLevelByName sets min level of output by name, available values are [debug|info|warn|error|fatal|panic|stack]. It returns ErrLevel for invalid name.

func (*Logger) SetOutput

func (l *Logger) SetOutput(w io.Writer)

SetOutput sets output of Logger

func (*Logger) SetSkip

func (l *Logger) SetSkip(depth int)

SetSkip changes the PC

func (*Logger) SetTags

func (l *Logger) SetTags(tags ...string)

SetTags sets tags of all logs, it'll replace previous definition.

func (*Logger) Skip

func (l *Logger) Skip() int

func (*Logger) Tags

func (l *Logger) Tags() []string

func (*Logger) Trace

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

Trace calls l.Output to print to the logger and output process stacks, and exit process with sign 1 at last. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warn

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

Warn calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warnf

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

Warnf calls l.Output to print to the logger. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Write

func (l *Logger) Write(b []byte) (int, error)

Write implements io.Writer interface

type StructLogger

type StructLogger interface {
	Str(key, value string) StructLogger
	Bool(key string, value bool) StructLogger
	Duration(key string, value time.Duration) StructLogger
	Time(key string, value time.Time) StructLogger
	Err(err error, stack bool) StructLogger
	Any(key string, value any) StructLogger
	Fields(fields map[string]any) StructLogger

	Debug(msg string)
	Debugf(format string, args ...any)
	Info(msg string)
	Infof(format string, args ...any)
	Warn(msg string)
	Warnf(format string, args ...any)
	Error(msg string)
	Errorf(format string, args ...any)
	Fatal(msg string)
	Fatalf(format string, args ...any)
	Panic(msg string)
	Panicf(format string, args ...any)
}

StructLogger for well formatted

Jump to

Keyboard shortcuts

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