欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 人文社科 > 生活经验 >内容正文

生活经验

Go的日志模块glog调研笔记

发布时间:2023/11/27 生活经验 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Go的日志模块glog调研笔记 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

glog简介
glog是著名的google开源C++日志库glog(https://github.com/google/glog)的golang版本,glog是一个轻量级的日志库,上手简单不需要配置文件并且稳定高效,可以自定义控制的内容比较少。 
glog主要有以下几个特点: 
1. glog有四种日志等级INFO < WARING < ERROR < FATAL,不同等级的日志是打印到不同文件的,低等级的日志文件中(INFO)会包含高等级的日志信息(ERROR) 
2. 通过命令行传递参数 –log_dir指定日志文件的存放目录,目录如果不存在,需要事先创建,默认为os.TempDir() ,也就是默认目录是/tmp
3. 可以根据文件大小切割日志文件,但是不能根据日期切割日志文件 
4. 日志输出格式是固定的(Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg…)不可以自定义 
5. 在程序开始时需要调用flag.Parse()解析命令行参数,在程序退出时需要调用glog.Flush() 确保将缓存区中的内容输出到文件中
6. glog.V(1).Infoln("level 1")这行代码表示设置的-v参数大于V()里面的参数才执行后面的Infoln。如果不加-v参数,默认等级为0

glob的参数

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

简单示例
下面是一个简单的例子, 假设该例子文件名为glob_demo.go

//description: 演示Go的日志库glob的用法
//note: 需要先安装该日志库,同时如果指定目录的话,需要先创建该目录,如果不指定目录,默认存放在/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() {//初始化命令行参数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.Info("info %d", 1)glog.Warning("warning %d", 2)glog.Error("error %d", 3)//需要开启-v=xx参数之后才会打印出来glog.V(1).Infoln("level 1")glog.V(2).Infoln("level 2")
}


首先安装glob日志库
go get -v "github.com/golang/glob"
然后编译运行
go build glob_demo.go
./glob_demo --log_dir="./"
./glob_demo -v=3
然后在当前目录或是tmp目录下面,可以看到各种日志文件,我们只需要查看符号链接文件即可,因为每次运行都会产生当前级别的日志文件,多次运行会有很多这种日志文件,而符号链接文件会始终指向最新的日志文件上

参考文献

[1].https://github.com/golang/glog

总结

以上是生活随笔为你收集整理的Go的日志模块glog调研笔记的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。