golang之log

标准库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
type Logger

标准库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
)
Priority

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.

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

3. https://github.com/open-ness/common.git

原文地址:https://www.cnblogs.com/embedded-linux/p/12772352.html