標准庫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
標准庫log/syslog
syslog實現了基本的linux級別級日志輸出,可作為實現更多功能的syslog庫的參考。
Package syslog provides a simple interface to the system log service. It can send messages to the syslog daemon using UNIX domain sockets, UDP or TCP.
Only one call to Dial is necessary. On write failures, the syslog client will attempt to reconnect to the server and write again.
func NewLogger(p Priority, logFlag int) (*log.Logger, error)
func Dial(network, raddr string, priority Priority, tag string) (*Writer, error)
The Priority is a combination of the syslog facility and severity. For example, LOG_ALERT | LOG_FTP sends an alert severity message from the FTP facility.
The default severity is LOG_EMERG; the default facility is LOG_KERN.
type Priority int const ( // From /usr/include/sys/syslog.h. // These are the same on Linux, BSD, and OS X. LOG_EMERG Priority = iota LOG_ALERT LOG_CRIT LOG_ERR LOG_WARNING LOG_NOTICE LOG_INFO LOG_DEBUG ) const ( // From /usr/include/sys/syslog.h. // These are the same up to LOG_FTP on Linux, BSD, and OS X. LOG_KERN Priority = iota << 3 LOG_USER LOG_MAIL LOG_DAEMON LOG_AUTH LOG_SYSLOG LOG_LPR LOG_NEWS LOG_UUCP LOG_CRON LOG_AUTHPRIV LOG_FTP LOG_LOCAL0 LOG_LOCAL1 LOG_LOCAL2 LOG_LOCAL3 LOG_LOCAL4 LOG_LOCAL5 LOG_LOCAL6 LOG_LOCAL7 )
openness log
https://github.com/open-ness/common.git
接口說明:https://godoc.org/github.com/open-ness/common/log
import “github.com/open-ness/common/log”
不僅提供基礎的level[f|ln]函數,還提供GrpcLogger、Logger、Printer接口。
const ( // DefaultLevel is the initial logging verbosity DefaultLevel = syslog.LOG_INFO // DefaultFacility is the default facility portion of the syslog priority. DefaultFacility = syslog.LOG_LOCAL0 ) var ( // DefaultLogger is the package-level logger that is used for all package // funcs. DefaultLogger = &Logger{} )
import "github.com/open-ness/common/log/syslog"
func NewLogger(p syslog.Priority, logFlag int) (*log.Logger, error)
func ParseLevel(prio string) (syslog.Priority, error)
ParseLevel parses provided syslog severity name into its integer(syslog.Priority) representation. Supported input values: emerg, emergency, alert, crit, critical, err, error, warn, warning, notice, info, information, debug. Input is parsed without case sensitivity.
- type Logger
- func (l *Logger) ConnectSyslog(addr string) error
- func (l *Logger) ConnectSyslogTLS(addr string, conf *tls.Config) error
- func (l *Logger) DisconnectSyslog() error
- func (l *Logger) GetFacility() syslog.Priority
- func (l *Logger) GetLevel() syslog.Priority
- func (l *Logger) SetFacility(p syslog.Priority)
- func (l *Logger) SetLevel(p syslog.Priority)
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) WithField(key string, value interface{}) Printer
- func (l *Logger) WithFields(kvs map[string]interface{}) Printer
package api import "github.com/open-ness/common/log"
// 設置log tag或前綴,一般以模塊為單位設置 var log = log.DefaultLogger.WithField("component", "api") func Hello(name string) { log.Infof("Hello %s!", name) // Output: "[component=api] Hello <name>!" }
// ------------------------------------------------------------
import "github.com/open-ness/common/log"
import "api"
func main(){
// 設置總日志級別, 一般僅在main中設置
lvl, err := log.ParseLevel("info")
if err != nil {
log.Errf("Failed to parse log level: %s", err.Error())
os.Exit(1)
}
log.SetLevel(lvl)
Hello("Golang")
}
Minimal support for structured tagging exists via (*Logger).WithField(s). Key-value pairs can be set before printing in order to automatically prepend data to each log. As an edge case, if the value is nil, then the prepended data will look like [key] rather than [key=<nil>].
結構化的log可通過在每個模塊中設置(*Logger).WithField(s)來定位各個模塊的輸出,s一般為key-value,若value為nil,則輸出數據格式為[key]。
參考:
1. https://godoc.org/log/syslog
2. Go Web編程 12.1 應用日志 beego
