首页 > Go的日志模块glog调研笔记

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

更多相关:

  • ELK:(ELK 由 ElasticSearch 、 Logstash 和 Kiabana 三个开源工具组成),Elasticsearch用于存储日志信息,Logstash用于收集日志,Kibana用于图形化展示。上一节我们介绍了如何使用docker-compose搭建ELK日志分析系统。传送门:传说中的ELK日志分析系统,手把手教你...

  • 欢迎关注头条号:老顾聊技术精品原创技术分享,知识的组装工目录前言常用日志组件什么是日志门面和日志实现常见的日志框架日志使用@slf4j注解日志的配置logback-spring配置总结前言日志是我们系统必备的功能之一,可以帮助我们开发人员定位系统的异常、错误以及运行流程的重要的工具。今天老顾就来介绍一下Spring boot的默认的l...

  • 应用场景 该配置文件用于ATS用作反向代理模式,访问日志被server域名分隔。比如,我想将sohu和ifeng的域名的日志分别记在两个不同的日志文件中,其它的日志统一记在默认的日志文件中。 配置 为了将不同源站的HTTP transaction记录在不同的日志文件中,你必须在log_hosts.config中列举出每...

  • 缘起 近来因为公司项目需要,阅读了一部分ATS logging system的源码实现,越发觉得logs_xml.config文件的配置非常重要,而我目前只是实践了一点它的皮毛。为此,根据自己的理解,翻译了官网的这篇文档,以作备忘。 logs_xml.config文件定义了自定义日志文件格式,过滤器和处理选项。文件格式采用x...

  • 本文来自 运维人生 ,作者:fly是个稻草人链接:http://www.ywadmin.com/?id=76误删除linux系统文件了?不用急,本文将给你一个恢复linux文件的方法,让你轻松应对运维中的各风险问题。方法总比问题多~说在前面的话针对日常维护操作,难免会出现文件误删除的操作。大家熟知linux文件系统不同win有回收...

  • 原文来自SecIN社区—作者:WiHat0x00 什么是WebShell渗透测试工作的一个阶段性目标就是获取目标服务器的操作控制权限,于是WebShell便应运而生。Webshell中的WEB就是web服务,shell就是管理攻击者与操作系统之间的交互。Webshell被称为攻击者通过Web服务器端口对Web服务器有一定的操作权限,而...

  • 断电时文件系统发生了什么?硬盘又发生了什么?下一次开机时写到一半的文件在系统层面还在吗?在底层还在吗?更进一步的, 文件系统如何保证事务性, 会不会存在某种极端情况导致例如最后几个bit还没写完, 文件系统却认为它成功了的情况?回答不限任何文件系统,谢谢!下面是「北极」的回复分享断电的一瞬间,很多事情是无法确定的:1. 你无法确定...

  • 接到项目需求。需要搭建一个页面进行交互,慢慢来b (2).jpg使用python django框架进行页面的搭建在项目文件下打开窗口,输入命令;django-admin startproject helloword#在文件helloword/helloword/创建view.py在view.py文件中输入以代码from django....

  • 常见的错误集合解决方案(一)No.1提示错误'Microsoft.VC90.CRT,version="9.0.21022.8"把Microsoft.NET Framework 3.5.1下面的全部勾选上。No.2解决Qt Designer设计的图标但是VS生成不显示问题描述:在Qt designer中为菜单栏和工具栏设计的图标,但是...

  • We have ZZIPlib installed.My command configure line looks like :./configure ?with-apxs ?with-curl ?with-curl-dir=/usr/local/lib ?with-gd ?with-gd-dir=/usr/local ?with-g...

  • asar Whether to package the application’s source code into an archive, using Electron’s archive format. Defaults to true. Node modules, that must be unpacked, will be d...

  • 1.      今天遇到一问题,在sles11/vxworks下编译通过,但是在hpux下失败 2.      编译错误: /usr/ccs/bin/ld:DP relative code in file /projects/xxx/DERIVED/tfa_pa32-hpux.a(tfa02_pa32-hpux.o) -share...

  •         最近买个了小本lenovo x100e,结果发现这小本没有大小写指示灯,在windows用也无妨,不过我常常用这本在ubuntu中调试linux代码,vi 常用的编辑器,熟悉的都知道,大小写很关键的,用google搜了一下,发现可以用如下方法解决:        1.  “sudo apt-get install l...

  •   修改Ubuntu的启动logo 原文链接: https://my.oschina.net/jmjoy/blog/380262     内容:   Plymouth splash screen is the initial splash screen at boot-up.Ubuntu 10.04 uses Plym...