When we develop a project, maybe sometimes we need to code a log package ourselves, and we can use the popular open source log project such as the standard golang log package, glog, zap which is open source project development by uber, logrus etc. So let’s dive into these packages now!
Table of contents
Open Table of contents
Youtube Video
Glog
Glog is developed by Google. It is another light log package just like the Standard log package, but it supplies more useful features:
- Support 4 log levels:
INFO
,WARNING
,ERROR
,FATAL
. - Support the options of command line: such as
-alsologtostderr
,-log_backtrace_at
,-log_dir
,-logtostderr
,-v
etc. Every parameter implements some functions. - Support split the log file based on size.
- Support output log file based on log level.
- Support
V
level, which can specify the custom log level by developer. - Support
vmodule
,which can specify different log levels in different files. - Support
traceLocation
, which can print program’s stack information, which location is specified by developer.
Kubernetes project used the log package based on Glog.
Glog Usage
Basic Usage
package main
import (
"flag"
"github.com/golang/glog"
)
func main() {
glog.MaxSize = 1024 * 1024 * 1024 // split log file based on 1Gi
flag.Parse()
defer glog.Flush()
glog.Info("This is info message")
glog.Infof("This is infof message: %v", 123)
glog.Warning("This is a warning message.")
glog.Warningf("This is a warningf message: %v", 123)
glog.Error("This is error message.")
glog.Errorf("This is a errorf message: %v", 124)
}
Glog supports 4 log levels, the order is : INFO
, WARNING
,ERROR
,FATAL
. And it also supports the command line. You only need to invoke the flag.Parse()
in the code before using the Glog package.
The following are the parameters supported by Glog on the command line:
-alsologtostderr
: print log to file and standard error-log_backtrace_at value
: print the program’s stack information when the program runs to specify code line.log_dir
: specify the log output dir-logtostderr
: the log output to standard error, not file.-stderrthreshold value
: specify equal or greater thanvalue
level log output to standard error, the value default isERROR
level-v value
: specify the log level-vmodule value
: specify different log levels in different files.
So when running the above code, use this command:
mkdir log
go run main.go -log_dir=log -alsologtostderr
The command line output:
I0702 14:43:49.476147 29140 main.go:14] This is info message
I0702 14:43:49.477794 29140 main.go:15] This is infof message: maloong
W0702 14:43:49.477794 29140 main.go:17] This is a warning message.
W0702 14:43:49.478905 29140 main.go:18] This is a warningf message: maloong
E0702 14:43:49.478905 29140 main.go:20] This is error message.
E0702 14:43:49.482661 29140 main.go:21] This is a errorf message: maloong
And because of the specified command line paramter: -alsologtostderr
, the log direction also output the log files.
main.INFO
file is a soft link, which links to the INFO
level log file. The lower log level file includes the higher log level logs. For example, the INFO
level log includes the WARNING
,ERROR
,FATAL
level log. Glog will create a new log file when the current log file is 1.8 Gi, you can specify the threshold value by glog.MaxSize
.
Glog output format is:<header>]<message>
, the header
format is :Lmmddhh:mm:ss.uuuuuu threadid file:line
:
Lmmd
:L
is glog log level:I->INFO
,W->WARNING
,E-> ERROR
,F->FATAL
.hh:mm:ss:uuuuuu
: It is the time information.threadid
: It’s the process PID,which can get the value usingos.Getpid()
file:line:specify
the log print location: file name and line number.
When we use glog.info
, glog.warning
etc to record the log, these logs will be stored in the buffer, not written to file directly. It’s good for performance. when invoke the glog.Flush()
function, the log data will write to the file. In the glog init
function, start a goroutine
which invokes the glog.Flush()
function cyclical duration is 30 seconds . When the program exits, the log will lose after the last time to invoke the glog.Flush()
function. So when your program needs to exit , you need to invoke the glog.Flush()
function as well. NOTE
: invoke the glog.Fatal
function, glog will print the log and exit the program, it will wirte all the log in the buffer to file, but glog.info
,glog.warning
, glog.error
do not do this.
Vmodule
V level
, It’s a very useful feature. The V value is less but the log level is higher.
package main
import (
"flag"
"github.com/golang/glog"
)
func main() {
flag.Parse()
defer glog.Flush()
glog.V(3).Info("Level 3 message")
glog.V(5).Info("Level 5 message")
glog.V(7).Info("Level 7 message")
glog.V(8).Info("Level 8 message")
}
Use the command to run the program.
go run main.go -log_dir=log -alsologtostderr
There is nothing to output to terminal. Because the log level is not enough , we can use -v
specify the log level.
go run main.go -log_dir=log -alsologtostderr -v=5
The output to terminal is :
I0702 15:38:10.244783 27092 main.go:13] Level 3 message
I0702 15:38:10.246910 27092 main.go:14] Level 5 message
Now, the log level is higher or equal to 5 (the V
value is lower or equal to 5) will output the log.
Glog also supports using the different levels in different files by using command parameter vmodule
, for example:
go run main.go foo.go -v=3 -log_dir=log -alsologtostderr -vmodule=foo=5
We specify the foo.go file with the log level 5 by using command parameter -vmodule=foo=5
, and other files use the 3 log level.
-vmodule’s input parameter ignores the file’s suffix. The grammar format is -vmodule=file1=2,file2=1,fs*=3
TraceLocation
This feature can specify printing the program’s stack information location by using command line parameter -log_backtrace_at:filename:line_number
package main
import (
"flag"
"github.com/golang/glog"
)
func main() {
glog.MaxSize = 1024 * 1024 * 1024 // specify 1Gi auto split
flag.Parse()
defer glog.Flush()
glog.Info("This is info message")
}
Run above code by the command:
go run main.go -log_dir=log -alsologtostderr -log_backtraace_at=main.go:14
The output is:
I0702 15:56:52.643124 30412 main.go:14] This is info message
goroutine 1 [running]:
github.com/golang/glog.Info(...)
C:/Users/andying/go/pkg/mod/github.com/golang/glog@v1.2.1/glog.go:482
main.main()
C:/Users/andying/goplace/glog/main.go:14 +0xae