标准库log
golang实现了简单易用的log,可以满足基本需求。虽然标准库实现了syslog,但已冻结不增加新功能。
Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined 'standard' Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. Every log message is output on a separate line: if the message being printed does not end in a newline, the logger will add one.
The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.
log常用的函数是Print[f|ln],Fatal[f|ln]和Panic[f|ln]
func Flags() int获取flag,func Prefix() string获取前缀,SefFlags(flag int)和SetPrefix(prefix string)用于设置。
These flags define which text to prefix to each log entry generated by the Logger. flags常量如下:
const ( Ldate = 1 << iota // the date in the local time zone: 2009/01/23 Ltime // the time in the local time zone: 01:23:23 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. Llongfile // full file name and line number: /a/b/c/d.go:23 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone Lmsgprefix // move the "prefix" from the beginning of the line to before the message LstdFlags = Ldate | Ltime // initial values for the standard logger )
标准logger接口
type Logger func New(out io.Writer, prefix string, flag int) *Logger
type Logger func New(out io.Writer, prefix string, flag int) *Logger func (l *Logger) Fatal(v ...interface{}) func (l *Logger) Fatalf(format string, v ...interface{}) func (l *Logger) Fatalln(v ...interface{}) func (l *Logger) Flags() int func (l *Logger) Output(calldepth int, s string) error func (l *Logger) Panic(v ...interface{}) func (l *Logger) Panicf(format string, v ...interface{}) func (l *Logger) Panicln(v ...interface{}) func (l *Logger) Prefix() string func (l *Logger) Print(v ...interface{}) func (l *Logger) Printf(format string, v ...interface{}) func (l *Logger) Println(v ...interface{}) func (l *Logger) SetFlags(flag int) func (l *Logger) SetOutput(w io.Writer) func (l *Logger) SetPrefix(prefix string) func (l *Logger) Writer() io.Writer