logx

package module
v0.0.0-...-3953824 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 14 Imported by: 11

README

simple and colorful golang logger

go get github.com/josexy/logx

usage

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

	loggerSimple := logCtx.BuildConsoleLogger(logx.LevelTrace)
	loggerSimple.Trace("this is a trace message", logx.String("key", "value"), logx.Int("key", 2222))
	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")
}

Documentation

Overview

reference: https://github.com/uber-go/zap/blob/master/buffer/buffer.go

Index

Constants

This section is empty.

Variables

View Source
var ConsoleEncoderSplitCharacter = byte('\t')

Functions

func Blue

func Blue(msg string) string

func Cyan

func Cyan(msg string) string

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 Magenta

func Magenta(msg string) string

func Red

func Red(msg string) string

func White

func White(msg string) string

func Yellow

func Yellow(msg string) string

Types

type Buffer

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

Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so the only way to construct one is via a Pool.

func NewBuffer

func NewBuffer(buf []byte) *Buffer

func (*Buffer) AppendBool

func (b *Buffer) AppendBool(v bool)

AppendBool appends a bool to the underlying buffer.

func (*Buffer) AppendByte

func (b *Buffer) AppendByte(v byte)

AppendByte writes a single byte to the Buffer.

func (*Buffer) AppendBytes

func (b *Buffer) AppendBytes(v []byte)

AppendBytes writes the given slice of bytes to the Buffer.

func (*Buffer) AppendFloat

func (b *Buffer) AppendFloat(f float64, bitSize int)

AppendFloat appends a float to the underlying buffer. It doesn't quote NaN or +/- Inf.

func (*Buffer) AppendInt

func (b *Buffer) AppendInt(i int64)

AppendInt appends an integer to the underlying buffer (assuming base 10).

func (*Buffer) AppendQuote

func (b *Buffer) AppendQuote(s string)

func (*Buffer) AppendString

func (b *Buffer) AppendString(s string)

AppendString writes a string to the Buffer.

func (*Buffer) AppendTime

func (b *Buffer) AppendTime(t time.Time, layout string)

AppendTime appends the time formatted using the specified layout.

func (*Buffer) AppendUint

func (b *Buffer) AppendUint(i uint64)

AppendUint appends an unsigned integer to the underlying buffer (assuming base 10).

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a mutable reference to the underlying byte slice.

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the underlying byte slice.

func (*Buffer) Clone

func (b *Buffer) Clone() *Buffer

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the length of the underlying byte slice.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the underlying byte slice. Subsequent writes re-use the slice's backing array.

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) TrimNewline

func (b *Buffer) TrimNewline()

TrimNewline trims any final "\n" byte from the end of the buffer.

func (*Buffer) TryGrow

func (b *Buffer) TryGrow(size int)

func (*Buffer) Write

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

Write implements io.Writer.

func (*Buffer) WriteByte

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

WriteByte writes a single byte to the Buffer.

Error returned is always nil, function signature is compatible with bytes.Buffer and bufio.Writer

func (*Buffer) WriteString

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

WriteString writes a string to the Buffer.

Error returned is always nil, function signature is compatible with bytes.Buffer and bufio.Writer

type CallerFormatter

type CallerFormatter uint8
const (
	// package/file:line
	ShortFile CallerFormatter = iota
	// /full/path/to/package/file:line
	FullFile
	// package/file:line package.func
	ShortFileFunc
	// /full/path/to/package/file:line package.func
	FullFileFunc
)

type CallerOption

type CallerOption struct {
	// caller key, default: "caller"
	CallerKey string
	// file key of caller, default: "file"
	FileKey string
	// function key of caller, default: "func"
	FuncKey string
	// caller formatter, default: ShortFileCaller
	Formatter CallerFormatter
	// caller skips increases the number of callers skipped by caller annotation.
	// when building wrappers around the Logger, supplying this Option prevents logx from always
	// reporting the wrapper code as the caller. default: 0
	CallerSkip int
}

type ColorAttr

type ColorAttr = uint8
const (
	GreenAttr ColorAttr = iota
	YellowAttr
	BlueAttr
	RedAttr
	CyanAttr
	MagentaAttr
	WhiteAttr
	HiGreenAttr
	HiYellowAttr
	HiBlueAttr
	HiRedAttr
	HiCyanAttr
	HiMagentaAttr
	HiWhiteAttr
)

type ConsoleEncoder

type ConsoleEncoder struct {
	*LogContext
	// contains filtered or unexported fields
}

func (*ConsoleEncoder) Encode

func (enc *ConsoleEncoder) Encode(buf *Buffer, msg string, fields []Field) error

func (*ConsoleEncoder) Init

func (enc *ConsoleEncoder) Init()

type EncoderType

type EncoderType byte
const (
	Console EncoderType = 1 << iota
	Json
)

type Field

type Field struct {
	Type        FieldType
	Key         string
	IntValue    int64
	StringValue string
	AnyValue    any
}

func Any

func Any(key string, value any) Field

func Array

func Array(key string, value ...any) Field

func ArrayT

func ArrayT[T any](key string, value ...T) Field

func Bool

func Bool(key string, value bool) Field

func Duration

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

func Error

func Error(key string, value error) Field

func Float32

func Float32(key string, value float32) Field

func Float64

func Float64(key string, value float64) Field

func Int

func Int(key string, value int) Field

func Int16

func Int16(key string, value int16) Field

func Int32

func Int32(key string, value int32) Field

func Int64

func Int64(key string, value int64) Field

func Int8

func Int8(key string, value int8) Field

func Object

func Object(key string, value ...Field) Field

func String

func String(key string, value string) Field

func Time

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

func UInt

func UInt(key string, value uint) Field

func UInt16

func UInt16(key string, value uint16) Field

func UInt32

func UInt32(key string, value uint32) Field

func UInt64

func UInt64(key string, value uint64) Field

func UInt8

func UInt8(key string, value uint8) Field

type FieldType

type FieldType byte
const (
	NoneType FieldType = iota
	StringType
	BoolType
	Int8Type
	Int16Type
	Int32Type
	Int64Type
	IntType
	Uint8Type
	Uint16Type
	Uint32Type
	Uint64Type
	UintType
	Float32Type
	Float64Type
	TimeFullType
	TimeType
	DurationType
	ErrorType
	ObjectType
	ArrayType
	NilType
	AnyType
)

type JsonEncoder

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

func (*JsonEncoder) Encode

func (enc *JsonEncoder) Encode(buf *Buffer, msg string, fields []Field) error

func (*JsonEncoder) Init

func (enc *JsonEncoder) Init()

type LevelOption

type LevelOption struct {
	// level key, default: "level"
	LevelKey string
	// lower level key
	LowerKey bool
}

type LevelType

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

type LogContext

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

func NewLogContext

func NewLogContext() *LogContext

func (*LogContext) BuildConsoleLogger

func (lc *LogContext) BuildConsoleLogger(level LevelType) Logger

func (*LogContext) BuildFileLogger

func (lc *LogContext) BuildFileLogger(level LevelType, writer io.Writer) Logger

func (*LogContext) Copy

func (lc *LogContext) Copy() *LogContext

func (*LogContext) WithCaller

func (lc *LogContext) WithCaller(enable bool, option CallerOption) *LogContext

func (*LogContext) WithColorfulset

func (lc *LogContext) WithColorfulset(enable bool, attr TextColorAttri) *LogContext

func (*LogContext) WithEncoder

func (lc *LogContext) WithEncoder(encoder EncoderType) *LogContext

func (*LogContext) WithEscapeQuote

func (lc *LogContext) WithEscapeQuote(enable bool) *LogContext

func (*LogContext) WithFields

func (lc *LogContext) WithFields(fields ...Field) *LogContext

func (*LogContext) WithLevel

func (lc *LogContext) WithLevel(enable bool, option LevelOption) *LogContext

func (*LogContext) WithMsgKey

func (lc *LogContext) WithMsgKey(key string) *LogContext

func (*LogContext) WithNewFields

func (lc *LogContext) WithNewFields(fields ...Field) *LogContext

func (*LogContext) WithReflectValue

func (lc *LogContext) WithReflectValue(enable bool) *LogContext

func (*LogContext) WithTime

func (lc *LogContext) WithTime(enable bool, option TimeOption) *LogContext

func (*LogContext) WithWriter

func (lc *LogContext) WithWriter(writer WriteSyncer) *LogContext

type Logger

type Logger interface {
	Trace(msg string, fields ...Field)
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)
	Fatal(msg string, fields ...Field)
	Panic(msg string, fields ...Field)
	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)
	PanicWith(err error)
	ErrorWith(err error)
	FatalWith(err error)
	With(fields ...Field) Logger
}

type LoggerX

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

func (*LoggerX) Debug

func (l *LoggerX) Debug(msg string, fields ...Field)

func (*LoggerX) Debugf

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

func (*LoggerX) Error

func (l *LoggerX) Error(msg string, fields ...Field)

func (*LoggerX) ErrorWith

func (l *LoggerX) ErrorWith(err error)

func (*LoggerX) Errorf

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

func (*LoggerX) Fatal

func (l *LoggerX) Fatal(msg string, fields ...Field)

func (*LoggerX) FatalWith

func (l *LoggerX) FatalWith(err error)

func (*LoggerX) Fatalf

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

func (*LoggerX) Info

func (l *LoggerX) Info(msg string, fields ...Field)

func (*LoggerX) Infof

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

func (*LoggerX) Panic

func (l *LoggerX) Panic(msg string, fields ...Field)

func (*LoggerX) PanicWith

func (l *LoggerX) PanicWith(err error)

func (*LoggerX) Panicf

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

func (*LoggerX) Trace

func (l *LoggerX) Trace(msg string, fields ...Field)

func (*LoggerX) Tracef

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

func (*LoggerX) Warn

func (l *LoggerX) Warn(msg string, fields ...Field)

func (*LoggerX) Warnf

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

func (*LoggerX) With

func (l *LoggerX) With(fields ...Field) Logger

type TextColorAttri

type TextColorAttri struct {
	KeyColor     ColorAttr
	StringColor  ColorAttr
	BooleanColor ColorAttr
	FloatColor   ColorAttr
	NumberColor  ColorAttr
}

type TimeOption

type TimeOption struct {
	// time key, default: time
	TimeKey string
	// time formatter, return value: string, int64, time.Time, any
	Formatter func(t time.Time) any
}

type WriteSyncer

type WriteSyncer interface {
	io.Writer
	Sync() error
}

func AddSync

func AddSync(w io.Writer) WriteSyncer

Directories

Path Synopsis
sub

Jump to

Keyboard shortcuts

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