glog

package module
v0.0.0-...-c115ae9 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2016 License: Apache-2.0 Imports: 15 Imported by: 0

README

glog

golang/glog的基础上做了如下修改,适应我自己的需求。

修改的地方:

  1. -log_to=std,file

    日志输出目标,std表示输出到console,file表示输出到文件,两者都指定则同时输出到console和文件,未指定则默认是file

  2. -log_file=filename

    如果指定输出到文件,则指定log文件名,未指定则默认为<program>.log

  3. -log_dir=xxx

    log文件保存路径,未指定则默认是系统TEMP目录

  4. -v=<level>

    指定日志等级为DEBUG、INFO、WARN、ERROR、FATAL其中之一,日志级别由低到高,日志等级>=Level的日志才能输出,指定DEBUG表示所有调用打印日志的方法都会打出,指定FATAL则表示只有致命错误才打印日志并退出。未指定则默认是FATAL,调试时可指定DEBUG,release运行时可指定INFO

    设置日志级别的方法为:glog.SetLevel(glog.DEBUG)

  5. -vm=pattern:level,pattern:level

    为特定文件指定日志等级,帮助调试。其中pattern可以是具体文件名(不带路径,无.go后缀)或者glob模式,level为日志等级

    在某些情况下,比如调试某个特定模块,需要关闭该模块之外的所有log:

    -v=FATAL -vm=file1:DEBUG,file2:DEBUG

  6. -log_backtrace_at=file:line

    在log到指定文件指定行打印stack back trace,帮助调试。其中,file为不带路径不带.go后缀的文件名,line为行号

  7. -log_daily=true

    按日期每天切割日志文件,指定为false则按大小(-log_size指定)切割,未指定默认为true

  8. -log_size=<size>

    指定切割文件大小,size单位是MB,如果超过大小则创建新的log文件

  9. -log_flush=10

    设置刷新缓冲区时间,单位是秒,默认是30s

##使用示例

func main() {
    //初始化命令行参数
    flag.Parse()
    //退出时调用,确保日志写入文件中
    defer glog.Flush()

	// 如下是用户未做设定时的默认参数:
	// 输出到系统TEMP目录,文件名为“<program>.log.<host>.<user>.yyyymmdd-hhmmss.pid”,为防止文件过大,采取每天创建新的log文件,但始终创建符号链接文件“<program>.log”到最新的log文件
	// 默认输出级别是DEBUG,也就是所有级别信息都输出
	// 后台flush缓冲数据到文件的时间间隔是30s

    //一般在测试环境下设置输出等级为DEBUG,线上环境设置为INFO
    
    glog.Info("test")
    glog.Warn("test")
    glog.Error("test")
    
    glog.Infof("test %d", 1)
    glog.Warnf("test %d", 2)
    glog.Errorf("test %d", 3)
 }
 
//假设编译后的可执行程序名为demo,运行时指定log_dir参数将日志文件保存到特定的目录,同时输出到控制台
// ./demo -log_dir=./log -v=INFO -log_to=std,file 

Documentation

Overview

Package glog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. It provides functions Info, Warning, Error, Fatal, plus formatting variants such as Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags.

Basic examples:

glog.Info("Prepare to repel boarders")

glog.Fatalf("Initialization failed: %s", err)

See the documentation for the V function for an explanation of these examples:

if glog.V(2) {
	glog.Info("Starting transaction...")
}

glog.V(2).Infoln("Processed", nItems, "elements")

Log output is buffered and written periodically using Flush. Programs should call Flush before exiting to guarantee all log output is written.

By default, all log statements write to files in a temporary directory. This package provides several flags that modify this behavior. As a result, flag.Parse must be called before any logging is done.

-logtostderr=false
	Logs are written to standard error instead of to files.
-alsologtostderr=false
	Logs are written to standard error as well as to files.
-stderrthreshold=ERROR
	Log events at or above this severity are logged to standard
	error as well as to files.
-log_dir=""
	Log files will be written to this directory instead of the
	default temporary directory.

Other flags provide aids to debugging.

-log_backtrace_at=""
	When set to a file and line number holding a logging statement,
	such as
		-log_backtrace_at=gopherflakes.go:234
	a stack trace will be written to the Info log whenever execution
	hits that statement. (Unlike with -vmodule, the ".go" must be
	present.)
-v=0
	Enable V-leveled logging at the specified level.
-vmodule=""
	The syntax of the argument is a comma-separated list of pattern=N,
	where pattern is a literal file name (minus the ".go" suffix) or
	"glob" pattern and N is a V level. For instance,
		-vmodule=gopher*=3
	sets the V level to 3 in all Go files whose names begin "gopher".

Index

Constants

View Source
const (
	DEBUG severity = iota
	INFO
	WARN
	ERROR
	FATAL
)

These constants identify the log levels in order of increasing severity. A message written to a high-severity log file is also written to each lower-severity log file.

View Source
const (
	Log2Std outputFlag = 1 << (iota)
	Log2File
	LogDailyRolling
)
View Source
const (
	KB uint64 = 1 << (iota * 10)
	MB
	GB
	TB
)

Variables

View Source
var MaxSize uint64 = 100

MaxSize(MB) is the maximum size of a log file in Mbytes.

View Source
var Stats struct {
	Debug, Info, Warning, Error OutputStats
}

Stats tracks the number of lines of output and number of bytes per severity level. Values must be read with atomic.LoadInt64.

Functions

func Error

func Error(args ...interface{})

Error logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.

func ErrorDepth

func ErrorDepth(depth int, args ...interface{})

ErrorDepth acts as Error but uses depth to determine which call frame to log. ErrorDepth(0, "msg") is the same as Error("msg").

func Errorf

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

Errorf logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.

func Errorln

func Errorln(args ...interface{})

Errorln logs to the ERROR, WARNING, and INFO logs. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.

func Exit

func Exit(args ...interface{})

Exit logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.

func ExitDepth

func ExitDepth(depth int, args ...interface{})

ExitDepth acts as Exit but uses depth to determine which call frame to log. ExitDepth(0, "msg") is the same as Exit("msg").

func Exitf

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

Exitf logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.

func Exitln

func Exitln(args ...interface{})

Exitln logs to the FATAL, ERROR, WARNING, and INFO logs, then calls os.Exit(1).

func Fatal

func Fatal(args ...interface{})

Fatal logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Print; a newline is appended if missing.

func FatalDepth

func FatalDepth(depth int, args ...interface{})

FatalDepth acts as Fatal but uses depth to determine which call frame to log. FatalDepth(0, "msg") is the same as Fatal("msg").

func Fatalf

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

Fatalf logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs to the FATAL, ERROR, WARNING, and INFO logs, including a stack trace of all running goroutines, then calls os.Exit(255). Arguments are handled in the manner of fmt.Println; a newline is appended if missing.

func Flush

func Flush()

Flush flushes all pending log I/O.

func Info

func Info(args ...interface{})

Info logs to the INFO log. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.

func InfoDepth

func InfoDepth(depth int, args ...interface{})

InfoDepth acts as Info but uses depth to determine which call frame to log. InfoDepth(0, "msg") is the same as Info("msg").

func Infof

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

Infof logs to the INFO log. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.

func Infoln

func Infoln(args ...interface{})

Infoln logs to the INFO log. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.

func SetFlags

func SetFlags(f int) error

(Log2Std|Log2File|LogDailyRolling)

func SetMaxSize

func SetMaxSize(max int)

func V

func V(s severity) error

(DEBUG/INFO/WARN/ERROR/FATAL)

func Vbacktrace

func Vbacktrace(v string) error

xxx.go:120

func Vmodule

func Vmodule(v string) error

file1.go:debug,file2.go:info

func Warn

func Warn(args ...interface{})

Warning logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Print; a newline is appended if missing.

func WarnDepth

func WarnDepth(depth int, args ...interface{})

WarningDepth acts as Warning but uses depth to determine which call frame to log. WarningDepth(0, "msg") is the same as Warning("msg").

func Warnf

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

Warningf logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Printf; a newline is appended if missing.

func Warnln

func Warnln(args ...interface{})

Warningln logs to the WARNING and INFO logs. Arguments are handled in the manner of fmt.Println; a newline is appended if missing.

Types

type OutputStats

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

OutputStats tracks the number of output lines and bytes written.

func (*OutputStats) Bytes

func (s *OutputStats) Bytes() int64

Bytes returns the number of bytes written.

func (*OutputStats) Lines

func (s *OutputStats) Lines() int64

Lines returns the number of lines written.

Jump to

Keyboard shortcuts

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