log

package module
v0.0.0-...-99c9ae7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 12 Imported by: 0

README

log

一个简单快速的日志库

使用方式

go get -u github.com/marcosxz/log

例子

设置参数

log.SetOptions(
    log.WithLevel(TraceLevel), // 设置日志级别
    log.WithStdLevel(TraceLevel), // 如果接管其他库log,设置输出级别
    log.WithOutput(file), // 输出目标
    log.WithFileLine(true), // 是否启用行列信息
    log.WithErrorHandler(nil), // 定义一个错误处理器
    log.WithNoLock(true), // 设置无锁模式,追加写模式下可以设置无锁
    log.WithFormatter(&TextFormatter{IgnoreBasicFields: false}), // 设置日志输出的格式,默认提供了text和json两种,可以自己实现Formatter接口来自定义格式,IgnoreBasicFields字段可以忽略输出基础字段
)

基本使用

log.Info(args ...)

接管标准库或者其他日志库(以标准库为例)

log:代表标准库
mylog:代表我们的库

log.SetOutput(mylog.Writer())

一些简单的测试

点击查看

Documentation

Index

Constants

View Source
const (
	FmtEmptySeparate = ""
	FmtLineSeparate  = "\n"
)

Variables

View Source
var LevelNameMapping = map[Level]string{
	PanicLevel: "PANIC",
	FatalLevel: "FATAL",
	ErrorLevel: "ERROR",
	WarnLevel:  "WARN",
	InfoLevel:  "INFO",
	DebugLevel: "DEBUG",
	TraceLevel: "TRACE",
}

log level string name mapping

Functions

func Debug

func Debug(args ...interface{})

func Debugf

func Debugf(format string, args ...interface{})

func Debugln

func Debugln(args ...interface{})

func Error

func Error(args ...interface{})

func Errorf

func Errorf(format string, args ...interface{})

func Errorln

func Errorln(args ...interface{})

func Fatal

func Fatal(args ...interface{})

func Fatalf

func Fatalf(format string, args ...interface{})

func Fatalln

func Fatalln(args ...interface{})

func Info

func Info(args ...interface{})

func Infof

func Infof(format string, args ...interface{})

func Infoln

func Infoln(args ...interface{})

func Panic

func Panic(args ...interface{})

func Panicf

func Panicf(format string, args ...interface{})

func Panicln

func Panicln(args ...interface{})

func SetOptions

func SetOptions(opts ...Option)

func Trace

func Trace(args ...interface{})

func Tracef

func Tracef(format string, args ...interface{})

func Traceln

func Traceln(args ...interface{})

func Warn

func Warn(args ...interface{})

func Warnf

func Warnf(format string, args ...interface{})

func Warnln

func Warnln(args ...interface{})

func Writer

func Writer() io.Writer

Types

type Entry

type Entry struct {
	Logger *Logger
	Buffer *bytes.Buffer
	Map    map[string]interface{}
	Level  Level
	Time   time.Time
	File   string
	Line   int
	Func   string
	Format string
	Args   []interface{}
}

type ErrorHandler

type ErrorHandler func(err error)

type log error handler

type Formatter

type Formatter interface {
	// Maybe in async goroutine
	// Please write the result to buffer
	Format(entry *Entry) error
}

type JsonFormatter

type JsonFormatter struct {
	// 忽略基础字段
	IgnoreBasicFields bool
}

func (*JsonFormatter) Format

func (f *JsonFormatter) Format(e *Entry) error

type Level

type Level uint8

log level

const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

const log level

type Logger

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

func New

func New(opts ...Option) *Logger

func StdLogger

func StdLogger() *Logger

func (*Logger) Debug

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Debugln

func (l *Logger) Debugln(args ...interface{})

func (*Logger) Error

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

func (*Logger) Errorln

func (l *Logger) Errorln(args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Fatalln

func (l *Logger) Fatalln(args ...interface{})

func (*Logger) Info

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

func (*Logger) Infoln

func (l *Logger) Infoln(args ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(args ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(format string, args ...interface{})

func (*Logger) Panicln

func (l *Logger) Panicln(args ...interface{})

func (*Logger) SetOptions

func (l *Logger) SetOptions(opts ...Option)

func (*Logger) Trace

func (l *Logger) Trace(args ...interface{})

func (*Logger) Tracef

func (l *Logger) Tracef(format string, args ...interface{})

func (*Logger) Traceln

func (l *Logger) Traceln(args ...interface{})

func (*Logger) Warn

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

func (*Logger) Warnln

func (l *Logger) Warnln(args ...interface{})

func (*Logger) Write

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

func (*Logger) Writer

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

type MutexWrap

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

func (*MutexWrap) Lock

func (mw *MutexWrap) Lock()

func (*MutexWrap) NoLock

func (mw *MutexWrap) NoLock(noLock bool)

func (*MutexWrap) RLock

func (mw *MutexWrap) RLock()

func (*MutexWrap) RUnlock

func (mw *MutexWrap) RUnlock()

func (*MutexWrap) Unlock

func (mw *MutexWrap) Unlock()

type Option

type Option func(*option)

func WithErrorHandler

func WithErrorHandler(handler ErrorHandler) Option

func WithFileLine

func WithFileLine(fileLine bool) Option

func WithFormatter

func WithFormatter(formatter Formatter) Option

func WithLevel

func WithLevel(level Level) Option

func WithNoLock

func WithNoLock(noLock bool) Option

func WithOutput

func WithOutput(output io.Writer) Option

SET

func WithStdLevel

func WithStdLevel(level Level) Option

type TextFormatter

type TextFormatter struct {
	IgnoreBasicFields bool
}

func (*TextFormatter) Format

func (f *TextFormatter) Format(e *Entry) error

Jump to

Keyboard shortcuts

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