Go的日志模块glog调研笔记
glog簡介
glog是著名的google開源C++日志庫glog(https://github.com/google/glog)的golang版本,glog是一個輕量級的日志庫,上手簡單不需要配置文件并且穩(wěn)定高效,可以自定義控制的內(nèi)容比較少。?
glog主要有以下幾個特點(diǎn):?
1. glog有四種日志等級INFO < WARING < ERROR < FATAL,不同等級的日志是打印到不同文件的,低等級的日志文件中(INFO)會包含高等級的日志信息(ERROR)?
2. 通過命令行傳遞參數(shù) –log_dir指定日志文件的存放目錄,目錄如果不存在,需要事先創(chuàng)建,默認(rèn)為os.TempDir() ,也就是默認(rèn)目錄是/tmp
3. 可以根據(jù)文件大小切割日志文件,但是不能根據(jù)日期切割日志文件?
4. 日志輸出格式是固定的(Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg…)不可以自定義?
5. 在程序開始時需要調(diào)用flag.Parse()解析命令行參數(shù),在程序退出時需要調(diào)用glog.Flush() 確保將緩存區(qū)中的內(nèi)容輸出到文件中
6. glog.V(1).Infoln("level 1")這行代碼表示設(shè)置的-v參數(shù)大于V()里面的參數(shù)才執(zhí)行后面的Infoln。如果不加-v參數(shù),默認(rèn)等級為0
glob的參數(shù)
// 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".
簡單示例
下面是一個簡單的例子, 假設(shè)該例子文件名為glob_demo.go
//description: 演示Go的日志庫glob的用法
//note: 需要先安裝該日志庫,同時如果指定目錄的話,需要先創(chuàng)建該目錄,如果不指定目錄,默認(rèn)存放在/tmp下面
//run: go get; go build glob_demo.go; ./glob_demo --log_dir="./"
//date: 2019-05-28package mainimport ("flag""github.com/golang/glog"
)func main() {//初始化命令行參數(shù)flag.Parse()//退出時調(diào)用,確保日志寫入磁盤文件中defer glog.Flush()glog.Info("This is a Info log")glog.Warning("This is a Warning log")glog.Error("This is a Error log")glog.Info("info %d", 1)glog.Warning("warning %d", 2)glog.Error("error %d", 3)//需要開啟-v=xx參數(shù)之后才會打印出來glog.V(1).Infoln("level 1")glog.V(2).Infoln("level 2")
}
首先安裝glob日志庫
go get -v "github.com/golang/glob"
然后編譯運(yùn)行
go build glob_demo.go
./glob_demo --log_dir="./"
./glob_demo -v=3
然后在當(dāng)前目錄或是tmp目錄下面,可以看到各種日志文件,我們只需要查看符號鏈接文件即可,因?yàn)槊看芜\(yùn)行都會產(chǎn)生當(dāng)前級別的日志文件,多次運(yùn)行會有很多這種日志文件,而符號鏈接文件會始終指向最新的日志文件上
參考文獻(xiàn)
[1].https://github.com/golang/glog
總結(jié)
以上是生活随笔為你收集整理的Go的日志模块glog调研笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Ubuntu 16.04.6 LTS上
- 下一篇: 在Ubuntu 16.04.6 LTS上