Go--Logger学习

Go语言的log

Logger struct

type Logger struct {
    mu      sync.Mutex
    prefix  string
    flag    int
    out     io.Writer
    buf     []byte
}

用于记录日志,sync.Mutex 锁mu, prefix 每条日志的前缀,flag 有Ldata Ltime Lmicroseconds Llongfile Lshortfile LUTC LstdfFlags(Ldate | Ltime), out 输出端, buf 日志缓存端

New

func New(out io.Writer, prefix string, flag int) *Logger 

生成Logger对象

Logger::SetOutput

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

设置输出端

itoa

func itoa(buf *[]byte, i int, wid int)

将i转换为ASCII 宽度限制为wid

Logger::formatHeader

func (l *Logger)formatHeader(buf *[]byte, t time.Time, file string, line int) 

格式化输出 Ldate+’ ‘+Ltime+’ ‘+Llongfile/Lshortfile+’:’+line

Logger::Output

func (l *Logger) Output(calldepth int, s string) error

输出:格式化+’\n’+out.Write(l.buf), calldepth: file路径深度

Logger::Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf 调用Logger::Output并且path=2

Logger::Print

func (l *Logger) Print(v interface{})

Print无格式直接输出

Logger::Println

func (l *Logger) Println(v ...interface{})

Println->fmt.Sprintln()->fmt.Println()->l.Output

Logger::Fatal

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

l.Fatal->fmt.Sprint->l.Output->os.Exit(1)

Logger::Fatalf

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

l.Fatalf->fmt.Sprintf->l.Output->os.Exit(1)

Logger::Fatalln

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

l.Fatalln->fmt.Sprintln->l.Output->os.Exit(1)

Logger::Panic

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

l.Panic->fmt.Sprint->l.Output->panic(s)

Logger::Panicf

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

l.Panicf->fmt.Sprintf->l.Output->panic(s)

Logger::Panicln

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

l.Panicln->fmt.Sprintln->l.Output(2, s)->panic(s)

Flags

func (l *Logger) Flags() int

获取flag

SetFlags

func (l *logger) SetFlags(flag int)

设置Flag

Prefix

func (l *Logger) Prefix() string

获取prefix string

SetPrefix

func (l *Logger) SetPrefix(prefix string)

设置prefix string

std

New std

var std = New(os.Stderr, "", LstdFlags)

标准错误输出

std functions

func Flag()int
func SetFlag(flag int)

调用std.Flag std.SetFlag

func Prefix()string
func SetPrefix(prefix string)

调用std.Prefix std.SetPrefix

func Print(v ...interface{})
func Printf(format string, v ...interface{})
func Println(v ...interface{})

调用std.Print std.Printf…

func Fatal(v ...interface{})
func Fatalf(format string, v ...interface{})
func Fatalln(v ...interface{})

调用std.Fatal std.Fatalf…

func Panic(v ...interface{})
func Panicf(format string, v ...interface{})
func Panicln(v ...interface{})

调用std.Panic std.Panicf std.Panicln..

func Output(calldepth int, s string) error

调用std.Output
// Calldepth is the count of the number of
// frames to skip when computing the file name and line number
// if Llongfile or Lshortfile is set;

你可能感兴趣的:(GO)