log

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

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

Go to latest
Published: Jan 6, 2022 License: MIT Imports: 19 Imported by: 0

README

go log system

log is a powerful logging framework that provides log custom log level.

log provides Fatal, Error, Warn, Info, Debug level log. and with the requestID can quickly go to the current line of code.

If you like,please give a star。

How to Use

it's very simple and easy to use.

func main(){
	l := log.New(log.WithTraceId(trace.NewTraceId()))
	ctx := log.NewContext(context.Background(), l)
	
	v, err := rand.Int(rand.Reader, big.NewInt(int64(16)))
    	if err != nil {
    		log.ErrorContext(ctx, " rand.In failed", "error", err.Error())
    		return 
    	}
	log.InfoContext(ctx, "the rand Int result", "v", v)
}

This use is made of the key and the Value output.

License

This is free software distributed under the terms of the MIT license

Documentation

Index

Constants

View Source
const (
	FatalLevelString = "fatal"
	ErrorLevelString = "error"
	WarnLevelString  = "warning"
	InfoLevelString  = "info"
	DebugLevelString = "debug"
)
View Source
const TimeFormatLayout = "2006-01-02 15:04:05.000"

Variables

View Source
var (
	ConcurrentStdout io.Writer = ConcurrentWriter(os.Stdout)
	ConcurrentStderr io.Writer = ConcurrentWriter(os.Stderr)
)

Functions

func ConcurrentWriter

func ConcurrentWriter(w io.Writer) io.Writer

ConcurrentWriter wraps an io.Writer and returns a concurrent io.Writer.

func Debug

func Debug(msg string, fields ...interface{})

Debug logs a message at DebugLevel on the standard logger. For more information see the Logger interface.

func DebugContext

func DebugContext(ctx context.Context, msg string, fields ...interface{})

DebugContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(1, DebugLevel, msg, fields...)
	return
}
Output(1, DebugLevel, msg, fields...)

func Error

func Error(msg string, fields ...interface{})

Error logs a message at ErrorLevel on the standard logger. For more information see the Logger interface.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, fields ...interface{})

ErrorContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(1, ErrorLevel, msg, fields...)
	return
}
Output(1, ErrorLevel, msg, fields...)

func Fatal

func Fatal(msg string, fields ...interface{})

Fatal logs a message at FatalLevel on the standard logger. For more information see the Logger interface.

func FatalContext

func FatalContext(ctx context.Context, msg string, fields ...interface{})

FatalContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(1, FatalLevel, msg, fields...)
	return
}
Output(1, FatalLevel, msg, fields...)

func FormatTime

func FormatTime(t time.Time) (result [23]byte)

2006-01-02 15:04:05.000

func FormatTimeString

func FormatTimeString(t time.Time) string

2006-01-02 15:04:05.000

func Info

func Info(msg string, fields ...interface{})

Info logs a message at InfoLevel on the standard logger. For more information see the Logger interface.

func InfoContext

func InfoContext(ctx context.Context, msg string, fields ...interface{})

InfoContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(1, InfoLevel, msg, fields...)
	return
}
Output(1, InfoLevel, msg, fields...)

func JSON

func JSON(v interface{}) string

JSON is a helper function, following is its function code.

data, _ := json.Marshal(v)
return string(data)

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

func NewRequest

func NewRequest(req *http.Request, logger Logger) *http.Request

func Output

func Output(calldepth int, level Level, msg string, fields ...interface{})

Output logs a message at specified level on the standard logger. For more information see the Logger interface.

func OutputContext

func OutputContext(ctx context.Context, calldepth int, level Level, msg string, fields ...interface{})

OutputContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(calldepth+1, level, msg, fields...)
	return
}
Output(calldepth+1, level, msg, fields...)

func SetBytesBufferPool

func SetBytesBufferPool(pool BytesBufferPool)

func SetDefaultOptions

func SetDefaultOptions(opts []Option)

func SetFormatter

func SetFormatter(formatter Formatter)

SetFormatter sets the standard logger formatter.

func SetLevel

func SetLevel(level Level) error

SetLevel sets the standard logger level.

func SetLevelString

func SetLevelString(str string) error

SetLevelString sets the standard logger level.

func SetOutput

func SetOutput(output io.Writer)

SetOutput sets the standard logger output.

NOTE: output must be thread-safe, see ConcurrentWriter.

func Warn

func Warn(msg string, fields ...interface{})

Warn logs a message at WarnLevel on the standard logger. For more information see the Logger interface.

func WarnContext

func WarnContext(ctx context.Context, msg string, fields ...interface{})

WarnContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	lg.Output(1, WarnLevel, msg, fields...)
	return
}
Output(1, WarnLevel, msg, fields...)

func XML

func XML(v interface{}) string

XML is a helper function, following is its function code.

data, _ := xml.Marshal(v)
return string(data)

Types

type BytesBufferPool

type BytesBufferPool interface {
	Get() *bytes.Buffer
	Put(*bytes.Buffer)
}

type Entry

type Entry struct {
	Location string // function(file:line)
	Time     time.Time
	Level    Level
	TraceId  string
	Message  string
	Fields   map[string]interface{}
	Buffer   *bytes.Buffer
}

type Formatter

type Formatter interface {
	Format(*Entry) ([]byte, error)
}
var JsonFormatter Formatter = jsonFormatter{}
var TextFormatter Formatter = textFormatter{}

type Level

type Level uint
const (
	FatalLevel Level
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
)

func (Level) String

func (level Level) String() string

type Logger

type Logger interface {
	// Fatal logs a message at FatalLevel.
	//
	// Unlike other golang log libraries (for example, the golang standard log library),
	// Fatal just logs a message and does not call os.Exit, so you need to explicitly call os.Exit if necessary.
	//
	// For fields, the following conditions must be satisfied
	//  1. the len(fields) must be an even number, that is to say len(fields)%2==0
	//  2. the even index element of fields must be non-empty string
	Fatal(msg string, fields ...interface{})

	// Error logs a message at ErrorLevel.
	// The requirements for fields can see the comments of Fatal.
	Error(msg string, fields ...interface{})

	// Warn logs a message at WarnLevel.
	// The requirements for fields can see the comments of Fatal.
	Warn(msg string, fields ...interface{})

	// Info logs a message at InfoLevel.
	// The requirements for fields can see the comments of Fatal.
	Info(msg string, fields ...interface{})

	// Debug logs a message at DebugLevel.
	// The requirements for fields can see the comments of Fatal.
	Debug(msg string, fields ...interface{})

	// Output logs a message at specified level.
	//
	// For level==FatalLevel, unlike other golang log libraries (for example, the golang standard log library),
	// Output just logs a message and does not call os.Exit, so you need to explicitly call os.Exit if necessary.
	//
	// The requirements for fields can see the comments of Fatal.
	Output(calldepth int, level Level, msg string, fields ...interface{})

	// WithField creates a new Logger from the current Logger and adds a field to it.
	WithField(key string, value interface{}) Logger

	// WithFields creates a new Logger from the current Logger and adds multiple fields to it.
	// The requirements for fields can see the comments of Fatal.
	WithFields(fields ...interface{}) Logger

	// SetFormatter sets the logger formatter.
	SetFormatter(Formatter)

	// SetOutput sets the logger output.
	SetOutput(io.Writer)

	// SetLevel sets the logger level.
	SetLevel(Level) error

	// SetLevelString sets the logger level.
	SetLevelString(string) error
}

func FromContext

func FromContext(ctx context.Context) (lg Logger, ok bool)

func FromContextOrNew

func FromContextOrNew(ctx context.Context, new func() Logger) (lg Logger, ctx2 context.Context, isNew bool)

func FromRequest

func FromRequest(req *http.Request) (lg Logger, ok bool)

func FromRequestOrNew

func FromRequestOrNew(req *http.Request, new func() Logger) (lg Logger, req2 *http.Request, isNew bool)

func MustFromContext

func MustFromContext(ctx context.Context) Logger

func MustFromRequest

func MustFromRequest(req *http.Request) Logger

func New

func New(opts ...Option) Logger

func WithField

func WithField(key string, value interface{}) Logger

WithField creates a new Logger from the standard Logger and adds a field to it. For more information see the Logger interface.

func WithFieldContext

func WithFieldContext(ctx context.Context, key string, value interface{}) Logger

WithFieldContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	return lg.WithField(key, value)
}
return WithField(key, value)

func WithFields

func WithFields(fields ...interface{}) Logger

WithFields creates a new Logger from the standard Logger and adds multiple fields to it. For more information see the Logger interface.

func WithFieldsContext

func WithFieldsContext(ctx context.Context, fields ...interface{}) Logger

WithFieldsContext is a shortcut to the following code:

lg, ok := FromContext(ctx)
if ok {
	return lg.WithFields(fields...)
}
return WithFields(fields...)

type NoopLogger

type NoopLogger struct{}

NoopLogger no operation Logger

func (NoopLogger) Debug

func (NoopLogger) Debug(msg string, fields ...interface{})

Debug impl Logger Debug

func (NoopLogger) Error

func (NoopLogger) Error(msg string, fields ...interface{})

Error impl Logger Error

func (NoopLogger) Fatal

func (NoopLogger) Fatal(msg string, fields ...interface{})

Fatal impl Logger Fatal

func (NoopLogger) Info

func (NoopLogger) Info(msg string, fields ...interface{})

Info impl Logger Info

func (NoopLogger) Output

func (NoopLogger) Output(calldepth int, level Level, msg string, fields ...interface{})

Output impl Logger Output

func (NoopLogger) SetFormatter

func (NoopLogger) SetFormatter(Formatter)

SetFormatter impl Logger SetFormatter

func (NoopLogger) SetLevel

func (NoopLogger) SetLevel(Level) error

SetLevel impl Logger SetLevel

func (NoopLogger) SetLevelString

func (NoopLogger) SetLevelString(string) error

SetLevelString impl Logger SetLevelString

func (NoopLogger) SetOutput

func (NoopLogger) SetOutput(io.Writer)

SetOutput impl Logger SetOutput

func (NoopLogger) Warn

func (NoopLogger) Warn(msg string, fields ...interface{})

Warn impl Logger Warn

func (NoopLogger) WithField

func (NoopLogger) WithField(key string, value interface{}) Logger

WithField impl Logger WithField

func (NoopLogger) WithFields

func (NoopLogger) WithFields(fields ...interface{}) Logger

WithFields impl Logger WithFields

type Option

type Option func(*options)

func WithFormatter

func WithFormatter(formatter Formatter) Option

func WithLevel

func WithLevel(level Level) Option

func WithLevelString

func WithLevelString(str string) Option

func WithOutput

func WithOutput(output io.Writer) Option

WithOutput sets the logger output.

NOTE: output must be thread-safe, see ConcurrentWriter.

func WithTraceId

func WithTraceId(traceId string) Option

func WithTraceIdFunc

func WithTraceIdFunc(fn func() string) Option

Directories

Path Synopsis
v1
v2

Jump to

Keyboard shortcuts

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