logger

package module
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: MIT Imports: 19 Imported by: 1

README

logger

base on slog 该库并非同步打印,不会阻塞进程

usage

install

go get -u github.com/ndsky1003/logger
func main(){
        defer logger.Close() //chan 当的锁,为了保证其全部写入
    }


func main() {
        logger.SetLevel(slog.LevelInfo) // 设置打印等级
	logger.Info("nihao")
	logger.Infof("nihao:%s", "ppxia")
	test()
	logger.Fields().Add("age", slog.Int64Value(18)).Info("show age")
	select {}
}

func test() {
	ff := logger.Fields()
	defer ff.Info("ppxia")
	ff.Add("nihao", slog.StringValue("123"))
	ff.Add("nihao1", slog.StringValue("123"))
	ff.Add("nihao2", slog.StringValue("123"))
	ff.Add("nihao3", slog.StringValue("123"))
}
切换handler

默认的是自定义的CustomHandler,handler在使用handle这个函数,只需要实现该函数即可

	logger.SetCreateHandler(func(w io.Writer, opt *slog.HandlerOptions) slog.Handler {
		// return slog.NewJSONHandler(w, opt)
		return slog.NewTextHandler(w, opt)
	})

Documentation

Overview

copy from offical

copy from offical

Index

Constants

View Source
const (
	LevelTrace     = slog.Level(-8)
	LevelDebug     = slog.LevelDebug
	LevelInfo      = slog.LevelInfo
	LevelNotice    = slog.Level(2)
	LevelWarn      = slog.LevelWarn
	LevelErr       = slog.LevelError
	LevelEmergency = slog.Level(12)
	LevelFatal     = slog.Level(16)
)

这里没有位运算

Variables

View Source
var DailyHandlerCreateFunc create_handler = func(w io.Writer, opt *slog.HandlerOptions) slog.Handler {
	return NewCustomHandler(w, opt)
}
View Source
var StdJsonHandlerCreateFunc create_handler = func(_ io.Writer, opt *slog.HandlerOptions) slog.Handler {
	return slog.NewJSONHandler(os.Stdout, opt)
}
View Source
var StdTextHandlerCreateFunc create_handler = func(_ io.Writer, opt *slog.HandlerOptions) slog.Handler {
	return slog.NewTextHandler(os.Stdout, opt)
}

Functions

func AppendSlogValue added in v1.0.2

func AppendSlogValue(v slog.Value, dst []byte) []byte

func Close added in v1.0.2

func Close()

func Debug

func Debug(msg ...any)

func Debugf

func Debugf(msg string, args ...any)

func Default

func Default() *logger

func Emergency

func Emergency(msg ...any)

func Emergencyf

func Emergencyf(msg string, args ...any)

func Err

func Err(msg ...any)

func Errf

func Errf(msg string, args ...any)

func Fatalf added in v1.0.8

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

func Fatalln added in v1.0.11

func Fatalln(v ...any)

func Flush added in v1.0.7

func Flush()

func GetLevel added in v1.0.11

func GetLevel() slog.Level

func Info

func Info(msg ...any)

func Infof

func Infof(msg string, args ...any)

func IsEmpty added in v1.0.2

func IsEmpty(a slog.Attr) bool

func NewCustomHandler added in v1.0.2

func NewCustomHandler(w io.Writer, opts *slog.HandlerOptions) *custom_handler

func Notice

func Notice(msg ...any)

func Noticef

func Noticef(msg string, args ...any)

func SetCreateHandler added in v1.0.2

func SetCreateHandler(fn create_handler)

func SetDaily added in v1.0.10

func SetDaily()

func SetDeltaSkip added in v1.0.11

func SetDeltaSkip(v int)

func SetFolder added in v1.0.4

func SetFolder(f string)

func SetLevel

func SetLevel(v slog.Level)

func SetWrapFunc added in v1.0.13

func SetWrapFunc(f warp_func)

api eg:美化每一个输出对象,json序列化等

func Trace

func Trace(msg ...any)

func Tracef

func Tracef(msg string, args ...any)

func Warn

func Warn(msg ...any)

func Warnf

func Warnf(msg string, args ...any)

Types

type Buffer added in v1.0.2

type Buffer []byte

buffer adapted from go/src/fmt/print.go

func NewBuffer added in v1.0.2

func NewBuffer() *Buffer

func (*Buffer) Free added in v1.0.2

func (b *Buffer) Free()

func (*Buffer) Reset added in v1.0.2

func (b *Buffer) Reset()

func (*Buffer) String added in v1.0.2

func (b *Buffer) String() string

func (*Buffer) Write added in v1.0.2

func (b *Buffer) Write(p []byte) (int, error)

func (*Buffer) WriteByte added in v1.0.2

func (b *Buffer) WriteByte(c byte) error

func (*Buffer) WritePosInt added in v1.0.2

func (b *Buffer) WritePosInt(i int)

func (*Buffer) WritePosIntWidth added in v1.0.2

func (b *Buffer) WritePosIntWidth(i, width int)

WritePosIntWidth writes non-negative integer i to the buffer, padded on the left by zeroes to the given width. Use a width of 0 to omit padding.

func (*Buffer) WriteString added in v1.0.2

func (b *Buffer) WriteString(s string) (int, error)

type Field added in v1.0.13

type Field map[string]slog.Value

func Fields

func Fields() Field

func (Field) Add added in v1.0.13

func (this Field) Add(key string, value any) Field

func (Field) Debug added in v1.0.13

func (this Field) Debug(msg ...any)

func (Field) Debugf added in v1.0.13

func (this Field) Debugf(msg string, args ...any)

func (Field) Emergency added in v1.0.13

func (this Field) Emergency(msg ...any)

func (Field) Emergencyf added in v1.0.13

func (this Field) Emergencyf(msg string, args ...any)

func (Field) Err added in v1.0.13

func (this Field) Err(msg ...any)

func (Field) Errf added in v1.0.13

func (this Field) Errf(msg string, args ...any)

func (Field) Fatal added in v1.0.13

func (this Field) Fatal(msg ...any)

func (Field) Fatalf added in v1.0.13

func (this Field) Fatalf(msg string, args ...any)

func (Field) Info added in v1.0.13

func (this Field) Info(msg ...any)

func (Field) Infof added in v1.0.13

func (this Field) Infof(msg string, args ...any)

func (Field) Notice added in v1.0.13

func (this Field) Notice(msg ...any)

func (Field) Noticef added in v1.0.13

func (this Field) Noticef(msg string, args ...any)

func (Field) Trace added in v1.0.13

func (this Field) Trace(msg ...any)

3 写死是没问题,因为外围是不会包装这个函数的,直接暴露Fields字段

func (Field) Tracef added in v1.0.13

func (this Field) Tracef(msg string, args ...any)

func (Field) Warn added in v1.0.13

func (this Field) Warn(msg ...any)

func (Field) Warnf added in v1.0.13

func (this Field) Warnf(msg string, args ...any)

Jump to

Keyboard shortcuts

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