glog输出到一个文件_【go语言】一文轻松使用日志记录框架glog

本文介绍了如何在Go语言中使用glog日志框架进行日志管理。glog提供了Info、Warning、Error和Fatal等日志级别,以及格式化输出功能。通过设置-log_dir参数,可以将日志输出到指定文件,并且高等级日志会同时记录在低等级日志文件中。文章还提及glog支持按文件大小切割日志,但不按日期切割。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在研究一个开源项目的源码,发现底层使用了glog来实现日志管理,跟大家分享一下glog的使用。

glog是用来实现日志的分级,提供等级有Info、Warning、Error和Fatal,加上格式化变量的输出,例如Infof、Warningf、Errorf和Fatalf。通过使用glog可以很好的实现日志的管理,用法也比较简单。

glog.Info("Prepare to repel boarders") // 使用方式同fmt.Print
glog.Fatalf("Initialization failed: %s", err) // 使用方式同fmt.Printf

在编写示例代码前,先要安装依赖包:

# go get github.com/golang/glog

示例代码:

package main
​
import (
  "flag"
  "github.com/golang/glog"
)
​
func main() {
  // 解析传入的参数
  flag.Parse()
​
  // 退出前执行,清空缓存区,将日志写入文件
  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.Fatal("This is a fatal log")
}

先将以上代码编译:

# go build Glog.go

执行:

# ./Glog -log_dir="./"

执行完毕之后,会看到以下文件:

# ls -l Glog*
-rwxr-xr-x. 1 root root 2501808 Feb  9 13:07 Glog
lrwxrwxrwx. 1 root root      51 Feb  9 13:07 Glog.ERROR -> Glog.localhost.root.log.ERROR.20200209-130721.33076
lrwxrwxrwx. 1 root root      51 Feb  9 13:07 Glog.FATAL -> Glog.localhost.root.log.FATAL.20200209-130721.33076
-rw-r--r--. 1 root root     297 Feb  9 13:06 Glog.go
lrwxrwxrwx. 1 root root      50 Feb  9 13:07 Glog.INFO -> Glog.localhost.root.log.INFO.20200209-130721.33076
-rw-r--r--. 1 root root    1182 Feb  9 13:07 Glog.localhost.root.log.ERROR.20200209-130721.33076
-rw-r--r--. 1 root root    1120 Feb  9 13:07 Glog.localhost.root.log.FATAL.20200209-130721.33076
-rw-r--r--. 1 root root    1313 Feb  9 13:07 Glog.localhost.root.log.INFO.20200209-130721.33076
-rw-r--r--. 1 root root    1252 Feb  9 13:07 Glog.localhost.root.log.WARNING.20200209-130721.33076
lrwxrwxrwx. 1 root root      53 Feb  9 13:07 Glog.WARNING -> Glog.localhost.root.log.WARNING.20200209-130721.33076

观察发现,会生成4个等级的日志文件,并且创建了软连接,查看Info日志文件的响应信息:

# cat Glog.INFO 
Log file created at: 2020/02/09 13:07:21
Running on machine: localhost
Binary: Built with gc go1.13 for linux/amd64
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0209 13:07:21.341067   33076 Glog.go:13] This is a info log
W0209 13:07:21.342422   33076 proc.go:203] This is a warning log12345
E0209 13:07:21.342774   33076 Glog.go:15] This is a error log
F0209 13:07:21.343294   33076 Glog.go:16] This is 1 fatal log
goroutine 1 [running]:
github.com/golang/glog.stacks(0xc00009c001, 0xc0001cc000, 0x272, 0x2710)
  /opt/golang/src/github.com/golang/glog/glog.go:769 +0xb8
github.com/golang/glog.(*loggingT).output(0x5b3fa0, 0xc000000003, 0xc0000b8000, 0x59a656, 0x7, 0x10, 0x0)
  /opt/golang/src/github.com/golang/glog/glog.go:723 +0x1b7
github.com/golang/glog.(*loggingT).printf(0x5b3fa0, 0xc000000003, 0x4f6a9d, 0x14, 0xc0000bff00, 0x1, 0x1)
  /opt/golang/src/github.com/golang/glog/glog.go:655 +0x14b
github.com/golang/glog.Fatalf(...)
  /opt/golang/src/github.com/golang/glog/glog.go:1148
main.main()
  /opt/packages/go_practice/Glog.go:16 +0x272
​
goroutine 19 [chan receive]:
github.com/golang/glog.(*loggingT).flushDaemon(0x5b3fa0)
  /opt/golang/src/github.com/golang/glog/glog.go:882 +0x8b
created by github.com/golang/glog.init.0
  /opt/golang/src/github.com/golang/glog/glog.go:410 +0x26f

依次观察其他三个日志文件,可以发现高等级的日志同时也会被记录到低等级的日志文件中,日志等级大小为Fatal > Error > Warning > Info。

除了传参有"-log_dir",还有其他参数,说明如下:

-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".

glog并且提供切割日志的功能,但是是按照文件大小来切割,不是按照日期切割的。

项目地址:

https://github.com/golang/glog

d218d45cd67a2c043e12342885d34af7.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值