dlog

package
v0.0.0-...-6c58b8e Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: MIT Imports: 14 Imported by: 8

README

log for golang

常量

  • severity
    • DEBUG
    • INFO
    • WARNING
    • ERROR
    • FATAL

格式

2015-06-16 12:00:35 ERROR test.go:12 ...

backend

  • 实现Log(s Severity, msg []byte) 和 Close()
  • 初始时调用dlog.SetLogging(dlog.INFO, backend),也可传字符串dlog.SetLogging("INFO", backend);默认输出到stdout,级别为DEBUG;单独设置日志级别:dlog.SetSeverity("INFO")

输出到stderr而不是对应的后端(方便调试用)

if debug {
    dlog.LogToStderr()
}

log to local file

b, err := dlog.NewFileBackend("./log") //log文件目录
if err != nil {
    panic(err)
}
dlog.SetLogging("INFO", b)     //只输出大于等于INFO的log
b.Rotate(10, 1024*1024*500) //自动切分日志,保留10个文件(INFO.log.000-INFO.log.009,循环覆盖),每个文件大小为500M, 因为dlog支持多个文件后端, 所以需要为每个file backend指定具体切分数值
dlog.Info(1, 2, " test")
dlog.Close()
  • log将输出到指定目录下面INFO.logWARNING.logERROR.logFATAL.log
  • 为了配合op的日志切分工具,有个goroutine定期检查log文件是否消失并且创建新的log文件
  • 为了性能使用bufio,bufferSize为256kB。log库会自己定期Flush到文件。在主程序退出之前需要调用dlog.Close(),否则可能会丢失部分log。

syslog

b, err := dlog.NewSyslogBackend(syslog.LOG_LOCAL3, "passport")
//b, err := dlog.DialSyslogBackend("tcp", "127.0.0.1:123", LOG_USER, "passport")
if err != nil {
    //...
}
dlog.SetLogging(dlog.INFO, b)
dlog.Warningf("%d %s", 123, "test")
  • 会建立多个writer,priority分别是syslog对应的LOG_INFO, LOG_WARNING, LOG_ERR, LOG_EMERG,tag对应修改为passport.INFO, passport.WARNING等等

输出到多个后端

b, _ := dlog.NewMultiBackend(b1, b2)
dlog.SetLogging("INFO", b)
defer dlog.Close()
//...

logger

logger := NewLogger("DEBUG", backend)
logger.Info("asdfasd")
logger.Close()

指定depth

  • 这种需求通常用于满足外部包装一个dlog helper, 防止depth只能打到helper内部的行号
  • 使用接口:
func LogDepth(s Severity, depth int, format string, args ...interface{}) {
	logging.printfDepth(s, depth+1, format, args...)
}
  • 调用方需要指定Severity/depth(从0开始, 每增加1层函数调用frame就+1)/format/args
  • 考虑depth是较为高级的参数, 所以只提供一个low level接口, 不再单独封装INFO等public function

按小时rotate

  • 在配置文件中配置rotateByHour = true
  • 如果是使用b := dlog.NewFileBackend得到的后端,请调用b.SetRotateByHour(true)来开启按小时滚动
  • INFO.log.2016040113, 表示INFO log在2016/04/01, 下午13:00到14:00之间的log, 此log在14:00时生成
  • 如果需要定时删除N个小时之前的log,请在配置文件中配置keepHours = N,例如想保留24小时的log,则keepHours = 24
  • 如果是使用b := dlog.NewFileBackend得到的后端,请调用b.SetKeepHours(N)来指定保留多少小时的log

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SyslogPriorityMap = map[string]syslog.Priority{
	"local0": syslog.LOG_LOCAL0,
	"local1": syslog.LOG_LOCAL1,
	"local2": syslog.LOG_LOCAL2,
	"local3": syslog.LOG_LOCAL3,
	"local4": syslog.LOG_LOCAL4,
	"local5": syslog.LOG_LOCAL5,
	"local6": syslog.LOG_LOCAL6,
	"local7": syslog.LOG_LOCAL7,
}

Functions

func Close

func Close()

func Debug

func Debug(args ...interface{})

func Debugf

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

func DialSyslogBackend

func DialSyslogBackend(network, raddr string, priority syslog.Priority, tag string) (*syslogBackend, error)

func Error

func Error(args ...interface{})

func Errorf

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

func Fall

func Fall()

func Fatal

func Fatal(args ...interface{})

func Fatalf

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

func Info

func Info(args ...interface{})

func Infof

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

func Init

func Init(config LogConfig) error

func LogDepth

func LogDepth(s Severity, depth int, format string, args ...interface{})

func LogToStderr

func LogToStderr()

func NewMultiBackend

func NewMultiBackend(bes ...Backend) (*multiBackend, error)

func NewSyslogBackend

func NewSyslogBackend(priorityStr string, tag string) (*syslogBackend, error)

func Printf

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

func Rotate

func Rotate(rotateNum1 int, maxSize1 uint64)

func SetFlushDuration

func SetFlushDuration(t time.Duration)

func SetKeepHours

func SetKeepHours(hours uint)

func SetLogging

func SetLogging(level interface{}, backend Backend)

func SetRotateByHour

func SetRotateByHour(rotateByHour bool)

func SetSeverity

func SetSeverity(level interface{})

func Warning

func Warning(args ...interface{})

func Warningf

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

Types

type Backend

type Backend interface {
	Log(s Severity, msg []byte)
	// contains filtered or unexported methods
}

type FileBackend

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

func NewFileBackend

func NewFileBackend(dir string) (*FileBackend, error)

func (*FileBackend) Fall

func (self *FileBackend) Fall()

func (*FileBackend) Flush

func (self *FileBackend) Flush()

func (*FileBackend) Log

func (self *FileBackend) Log(s Severity, msg []byte)

func (*FileBackend) Rotate

func (self *FileBackend) Rotate(rotateNum1 int, maxSize1 uint64)

func (*FileBackend) SetFlushDuration

func (self *FileBackend) SetFlushDuration(t time.Duration)

func (*FileBackend) SetKeepHours

func (self *FileBackend) SetKeepHours(hours uint)

func (*FileBackend) SetRotateByHour

func (self *FileBackend) SetRotateByHour(rotateByHour bool)

type LogConfig

type LogConfig struct {
	Type              string // syslog/stderr/std/file
	Level             string // DEBUG/INFO/WARNING/ERROR/FATAL
	SyslogPriority    string // local0-7
	SyslogSeverity    string
	FileName          string
	FileRotateCount   int
	FileRotateSize    uint64
	FileFlushDuration time.Duration
	RotateByHour      bool
	KeepHours         uint // make sense when RotateByHour is T
}

type Logger

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

func GetLogger

func GetLogger() *Logger

func NewLogger

func NewLogger(level interface{}, backend Backend) *Logger

func NewLoggerFromConfig

func NewLoggerFromConfig(config LogConfig) (Logger, error)

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Debug

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

func (*Logger) Debugf

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

func (*Logger) Error

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

func (*Logger) Errorf

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

func (*Logger) Fatal

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

func (*Logger) Fatalf

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

func (*Logger) Info

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

func (*Logger) Infof

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

func (*Logger) LogDepth

func (l *Logger) LogDepth(s Severity, depth int, format string, args ...interface{})

/////////////////////////////////////////////////////////////// depth version, only a low level api

func (*Logger) LogToStderr

func (l *Logger) LogToStderr()

func (*Logger) PrintfSimple

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

func (*Logger) SetLogging

func (l *Logger) SetLogging(level interface{}, backend Backend)

func (*Logger) SetSeverity

func (l *Logger) SetSeverity(level interface{})

func (*Logger) Warning

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

func (*Logger) Warningf

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

type Severity

type Severity int
const (
	FATAL Severity = iota
	ERROR
	WARNING
	INFO
	DEBUG
)

Jump to

Keyboard shortcuts

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