logx

package module
v0.0.0-...-569dac3 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 14 Imported by: 11

README

simple and colorful golang logger

go get -u github.com/josexy/logx

usage

func main() {
	logCtx := logx.NewLogContext().
		WithColor(true).
		WithLevel(true, true).
		WithCaller(true, true, true, true).
		WithWriter(color.Output).
		WithEncoder(logx.Simple).
		WithTime(true, func(t time.Time) any { return t.Format(time.DateTime) })

	loggerSimple := logCtx.BuildConsoleLogger(logx.LevelTrace)
	loggerSimple.Trace("this is a trace message")
	loggerSimple.Debug("this is a debug message")
	loggerSimple.Info("this is an info message")
	loggerSimple.Warn("this is a warning message")
	loggerSimple.Error("this is an error message")

	logCtx = logCtx.Copy().
		WithEncoder(logx.Json).
		WithEscapeQuote(true).
		WithCaller(true, true, true, true).
		WithTime(true, func(t time.Time) any { return t.Unix() })

	// file, err := os.Create("test.log")
	// if err != nil {
	// 	panic(err)
	// }
	// defer file.Close()
	// loggerJson := logCtx.BuildFileLogger(logx.LevelInfo, file)
	loggerJson := logCtx.BuildConsoleLogger(logx.LevelInfo)
	loggerJson.Trace("this is a trace message")
	loggerJson.Debug("this is a debug message")
	loggerJson.Info("this is an info message")
	loggerJson.Warn("this is a warning message")
	loggerJson.Error("this is an error message")
	loggerJson.Info("this is a info message",
		logx.String("string", "string"),
		logx.Bool("bool", false),
		logx.Int8("int8", 10),
		logx.Int16("int16", -20),
		logx.Int32("int32", -30),
		logx.Int64("int64", 40),
		logx.Int("int", 50),
		logx.UInt8("uint8", 60),
		logx.UInt16("uint16", 70),
		logx.UInt32("uint32", 80),
		logx.UInt64("uint64", 90),
		logx.UInt("uint", 100),
		logx.Float32("float32", 1234.45),
		logx.Float64("float64", 1234.4567),
		logx.Time("ts", time.Now().Add(time.Duration(time.Hour))),
		logx.Duration("duration", time.Duration(time.Hour+30*time.Minute+40*time.Second)),
		logx.Error("err", errors.New("error message")),
		logx.Error("err2", nil),
		logx.SortedMap("sortedmap1"),
		logx.SortedMap("sortedmap2",
			logx.Pair{Key: "key1", Value: "value1"},
			logx.Pair{"key2", 100},
			logx.Pair{"key3", logx.M{"list": []any{10, "20", true, 30.10, nil}}}),
		logx.Map("map", logx.M{
			"name":     "tony",
			"age":      34,
			"time":     time.Now(),
			"float32":  123.123,
			"duration": time.Duration(time.Hour + 30*time.Minute + 40*time.Second),
			"err":      nil,
			"slice2":   []any{100, "hello", time.Now(), false, 10.2223, nil},
			"map2": logx.M{
				"name2": "mike",
				"age2":  20,
			},
		}),
		logx.Slice("slice", []any{100, "hello", time.Now(), false, 10.2223, nil}),
		logx.Map("map2", nil),
		logx.Slice("slice2", nil),
		logx.String("a", `"message"`),
		logx.String("b", `"message`),
		logx.String("c", `message"`),
		logx.String("d", `'message'`),
		logx.String("e", `'message`),
		logx.String("f", `message'`),
		logx.String("g", `message."test".message`),
		logx.String("h", `message.'test'.message`),
		logx.String("i", `"hello" '\n' "\n"`),
	)

	loggerSimple.Error("this is an error message")
}

output

Documentation

Index

Constants

View Source
const (
	GreenAttr = iota
	YellowAttr
	BlueAttr
	RedAttr
	CyanAttr
	MagentaAttr
	WhiteAttr
	HiGreenAttr
	HiYellowAttr
	HiBlueAttr
	HiRedAttr
	HiCyanAttr
	HiMagentaAttr
	HiWhiteAttr
)

Variables

This section is empty.

Functions

func Any

func Any(key string, value any) arg

func Blue

func Blue(msg string) string

func Bool

func Bool(key string, value bool) arg

func Cyan

func Cyan(msg string) string

func Duration

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

func Error

func Error(key string, value error) arg

func Float32

func Float32(key string, value float32) arg

func Float64

func Float64(key string, value float64) arg

func Green

func Green(msg string) string

func HiBlue

func HiBlue(msg string) string

func HiCyan

func HiCyan(msg string) string

func HiGreen

func HiGreen(msg string) string

func HiMagenta

func HiMagenta(msg string) string

func HiRed

func HiRed(msg string) string

func HiWhite

func HiWhite(msg string) string

func HiYellow

func HiYellow(msg string) string

func Int

func Int(key string, value int) arg

func Int16

func Int16(key string, value int16) arg

func Int32

func Int32(key string, value int32) arg

func Int64

func Int64(key string, value int64) arg

func Int8

func Int8(key string, value int8) arg

func Magenta

func Magenta(msg string) string

func Map

func Map(key string, value M) arg

func NewLogContext

func NewLogContext() *logContext

func Red

func Red(msg string) string

func Slice

func Slice(key string, value []any) arg

func Slice2

func Slice2(key string, value ...any) arg

func Slice3

func Slice3[T any](key string, value []T) arg

func SortedMap

func SortedMap(key string, value ...Pair) arg

func String

func String(key string, value string) arg

func Time

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

func UInt

func UInt(key string, value uint) arg

func UInt16

func UInt16(key string, value uint16) arg

func UInt32

func UInt32(key string, value uint32) arg

func UInt64

func UInt64(key string, value uint64) arg

func UInt8

func UInt8(key string, value uint8) arg

func White

func White(msg string) string

func Yellow

func Yellow(msg string) string

Types

type EncoderType

type EncoderType byte
const (
	// Simple Encode does't support key-value args, only message field can be set
	Simple EncoderType = 1 << iota
	Json
)

type JsonEncoder

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

func (JsonEncoder) BuildConsoleLogger

func (lc JsonEncoder) BuildConsoleLogger(level LevelType) Logger

func (JsonEncoder) BuildFileLogger

func (lc JsonEncoder) BuildFileLogger(level LevelType, fsWriter *os.File) Logger

func (JsonEncoder) Copy

func (lc JsonEncoder) Copy() *logContext

func (*JsonEncoder) Encode

func (enc *JsonEncoder) Encode(w *bytes.Buffer, msg string, args ...arg) error

func (*JsonEncoder) Init

func (enc *JsonEncoder) Init()

func (JsonEncoder) WithCaller

func (lc JsonEncoder) WithCaller(enable, fileName, funcName, lineNumber bool) *logContext

func (JsonEncoder) WithColor

func (lc JsonEncoder) WithColor(enable bool) *logContext

func (JsonEncoder) WithEncoder

func (lc JsonEncoder) WithEncoder(encoder EncoderType) *logContext

func (JsonEncoder) WithEscapeQuote

func (lc JsonEncoder) WithEscapeQuote(enable bool) *logContext

func (JsonEncoder) WithLevel

func (lc JsonEncoder) WithLevel(enable, lower bool) *logContext

func (JsonEncoder) WithPrefix

func (lc JsonEncoder) WithPrefix(keyAndValues ...Pair) *logContext

func (JsonEncoder) WithTime

func (lc JsonEncoder) WithTime(enable bool, format func(time.Time) any) *logContext

func (JsonEncoder) WithWriter

func (lc JsonEncoder) WithWriter(writer io.Writer) *logContext

type LevelType

type LevelType uint8
const (
	LevelTrace LevelType = 1 << iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
	LevelPanic
)

type Logger

type Logger interface {
	Trace(msg string, args ...arg)
	Debug(msg string, args ...arg)
	Info(msg string, args ...arg)
	Warn(msg string, args ...arg)
	Error(msg string, args ...arg)
	Fatal(msg string, args ...arg)
	Panic(msg string, args ...arg)
	Tracef(format string, args ...any)
	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
	Panicf(format string, args ...any)
	Fatalf(format string, args ...any)
	PanicBy(err error)
	ErrorBy(err error)
	FatalBy(err error)
	WithPrefix(keyValues map[string]any) Logger
	WithPrefix2(keyValues ...any) Logger
}

type LoggerX

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

func (*LoggerX) Debug

func (l *LoggerX) Debug(msg string, args ...arg)

func (*LoggerX) Debugf

func (l *LoggerX) Debugf(format string, args ...any)

func (*LoggerX) Error

func (l *LoggerX) Error(msg string, args ...arg)

func (*LoggerX) ErrorBy

func (l *LoggerX) ErrorBy(err error)

func (*LoggerX) Errorf

func (l *LoggerX) Errorf(format string, args ...any)

func (*LoggerX) Fatal

func (l *LoggerX) Fatal(msg string, args ...arg)

func (*LoggerX) FatalBy

func (l *LoggerX) FatalBy(err error)

func (*LoggerX) Fatalf

func (l *LoggerX) Fatalf(format string, args ...any)

func (*LoggerX) Info

func (l *LoggerX) Info(msg string, args ...arg)

func (*LoggerX) Infof

func (l *LoggerX) Infof(format string, args ...any)

func (*LoggerX) Panic

func (l *LoggerX) Panic(msg string, args ...arg)

func (*LoggerX) PanicBy

func (l *LoggerX) PanicBy(err error)

func (*LoggerX) Panicf

func (l *LoggerX) Panicf(format string, args ...any)

func (*LoggerX) Trace

func (l *LoggerX) Trace(msg string, args ...arg)

func (*LoggerX) Tracef

func (l *LoggerX) Tracef(format string, args ...any)

func (*LoggerX) Warn

func (l *LoggerX) Warn(msg string, args ...arg)

func (*LoggerX) Warnf

func (l *LoggerX) Warnf(format string, args ...any)

func (*LoggerX) WithPrefix

func (l *LoggerX) WithPrefix(keyValues map[string]any) Logger

func (*LoggerX) WithPrefix2

func (l *LoggerX) WithPrefix2(keyValues ...any) Logger

type M

type M map[string]any

type Pair

type Pair struct {
	Key   string
	Value any
}

type SimpleEncoder

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

func (SimpleEncoder) BuildConsoleLogger

func (lc SimpleEncoder) BuildConsoleLogger(level LevelType) Logger

func (SimpleEncoder) BuildFileLogger

func (lc SimpleEncoder) BuildFileLogger(level LevelType, fsWriter *os.File) Logger

func (SimpleEncoder) Copy

func (lc SimpleEncoder) Copy() *logContext

func (*SimpleEncoder) Encode

func (enc *SimpleEncoder) Encode(w *bytes.Buffer, msg string, _ ...arg) error

func (*SimpleEncoder) Init

func (enc *SimpleEncoder) Init()

func (SimpleEncoder) WithCaller

func (lc SimpleEncoder) WithCaller(enable, fileName, funcName, lineNumber bool) *logContext

func (SimpleEncoder) WithColor

func (lc SimpleEncoder) WithColor(enable bool) *logContext

func (SimpleEncoder) WithEncoder

func (lc SimpleEncoder) WithEncoder(encoder EncoderType) *logContext

func (SimpleEncoder) WithEscapeQuote

func (lc SimpleEncoder) WithEscapeQuote(enable bool) *logContext

func (SimpleEncoder) WithLevel

func (lc SimpleEncoder) WithLevel(enable, lower bool) *logContext

func (SimpleEncoder) WithPrefix

func (lc SimpleEncoder) WithPrefix(keyAndValues ...Pair) *logContext

func (SimpleEncoder) WithTime

func (lc SimpleEncoder) WithTime(enable bool, format func(time.Time) any) *logContext

func (SimpleEncoder) WithWriter

func (lc SimpleEncoder) WithWriter(writer io.Writer) *logContext

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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