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-28

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值