This is a creation in Article, where the information may have evolved or changed.
A typical application scenario for log is to implement a log rating, such as an online environment that does not require logging of debug log information. Introduce Glog today. Let's look at a simple example of a glog.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
//file Name:glog.go Package main
Import ( "Flag" "Github.com/golang/glog" )
func Main () { flag. Parse () //1
Glog. Info ("This is a Info log") //2 Glog. Warning ("This is a Warning log") Glog. Error ("This is a Error log")
Glog. V (1). Infoln ("Level 1") //3 Glog. V (2). Infoln ("Level 2")
Glog. Flush () //4 }
|
If you have not used glog before and need to go get
install it, you need to perform the Go program as follows. Some of the above points can be noted first, and so on after reading the article understand.
1 2 3
|
$ go get $ go Build glog.go $./glog-log_dir= "./"
|
In this case, a few similar log files will be generated under the same sibling directory.
1 2 3
|
glog.kltao-mac.kltao.log.error.20160312-173205.22052 glog.kltao-mac.kltao.log.info.20160312-173205.22052 glog.kltao-mac.kltao.log.warning.20160312-173205.22052
|
This is the resulting log file, open the first error log file, the file content is as follows. The first 4 lines are file response information, and the last line is the log content of the 14th line of code recorded above.
1 2 3 4 5
|
Log file created AT:2016/03/12 17:32:05 Running on Machine:kltao-mac Binary:built with GC go1.4.2 for DARWIN/AMD64 Log line format: [IWEF]MMDD hh:mm:ss.uuuuuu threadid File:line] Msg E0312 17:32:05.568597 22052 glog.go:15] This is a Error log
|
This time if you open the other two info and warning log file, you will find that the warning log file in addition to the warning information also recorded the error message ( This is a Error log
), and the info log file is recorded all the log information ( This is a Info/Warning/Error log
). It is easy to understand that these logs are hierarchical (error>warning>info) and that high-level log content is logged to low-level log files at the same time. So Glog provides a rating? The answer is 4, in addition to the 3 mentioned above, there is a faltal.
Now there is another problem, why the log information in the 3rd place is not recorded? No hurry, this time the following re-execution. You can find the corresponding information in the new info log file.
1
|
./glog-log_dir= "./"-v=3
|
Yes, that's the -v
argument. Before I say V, let's talk about Glog's command-line parsing, and the corresponding code is the 1th place in the label. So how many kinds of parameters do glog have?
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - + A at - - - - - in - to +
|
//By default, all log statements write to files in a temporary directory. //This package provides several the flags that modify this behavior. //As a result, flag. Parse must be called before any logging are done. // //-logtostderr=false //Logs is written to the standard error instead of files. //-alsologtostderr=false //Logs is written to standard error as well as to files. //-stderrthreshold=error //Log events at or above this severity is logged to standard //Error as well as to files. //-log_dir= "" //Log files would 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 would 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.V(1).Infoln("level 1")
This line of code indicates that the parameter set by the-v parameter is greater than V () is executed after the Infoln. If you do not add the-v parameter, the default level is 0, so the third code is not executed. Specific implementation may wish to look at the source code realization, at a glance.
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - + A at - - - - - in - to + - the * $ Panax Notoginseng - the + A the + - $ $ - -
|
type Verbose bool
func V (level level) Verbose { //This function tries cheap unless there 's work to do. //The Fast path is a atomic loads and compares.
//Here is a cheap but safe test to see if V logging is enabled globally. if Logging.verbosity.get () >= level { return Verbose (True) }
//It ' s off globally but it vmodule could still be set. //Another cheap but safe test to see if Vmodule is enabled. if atomic. LoadInt32 (&logging.filterlength) > 0 { //Now we need a proper lock to use the logging structure. The PCs field //Is GKFX so we must lock before accessing it. This is fairly expensive, //But if V logging is enabled we ' re slow anyway. Logging.mu.Lock () defer Logging.mu.Unlock () if runtime. Callers (2, logging.pcs[:]) = = 0 { return Verbose (False) } V, OK: = Logging.vmap[logging.pcs[0]] if!ok { v = Logging.setv (logging.pcs[0]) } return Verbose (v >= level) } return Verbose (False) }
func (v Verbose) Info (args ... interface{}) { if v { logging.print (InfoLog, args ...) } }
func (v Verbose) infoln (args ... interface{}) { if v { logging.println (InfoLog, args ...) } }
func (v Verbose) infof (format string, args ... interface{}) { if v { logging.printf (infoLog, format, args ...) } }
|
4 places marked in the program, in addition to the 4th place, the other said, flush the role of emptying the buffer, is to write the log to the file. Golog initialization, a flush daemon, and then periodically to perform I/O operations, so the exit needs to show clear buffer. The initialization code for the Glog startup is as follows.
1 2 3 4
|
Func init () { ... Go Logging.flushdaemon () }
|
The above should be glog most of the use of the method, more detailed information can refer to Github.com/golang/glog.