This is a creation in Article, where the information may have evolved or changed.
Go language Write Log
The go language has a standard library, log, provides the most basic logging functionality, but there is no advanced features, if you need advanced features, you can choose Glog or Log4go.
Glog is provided by Google, similar to Google's C + + log library, it is very simple to use, described below, log4go usage and log4j, the same configuration is relatively high.
Glog provides only a few options, via command line control, such as:
-log_dir: Log File Save directory
-alsologtostderr: The log is written to the file while output to stderr
-V: Configures the level of the V output.
Glog is very simple to use, only need to import. The init function of the Glog package initializes and initiates a glog Flushdaemon, and you only need to use Glog. Info, Glog. Warning, Glog. Error or Glog.fatal.
A simple example:
Package Mainimport ("Flag" "FMT" "Github.com/golang/glog" "OS")//Avoid a compilation error that does not refer to fmt var _ = fmt. Printlnfunc Main () {//Initialize command line parameter flag. Parse () Glog. Info ("Hello, Glog") Glog. Warning ("Warning glog") glog. Error ("Error Glog") glog.infof ("Info%d", 1) glog. WARNINGF ("Warning%d", 2) Glog. Errorf ("Error%d", 3) Glog. V (3). Infoln ("info with v 3") glog. V (2). Infoln ("info with v 2") glog. V (1). Infoln ("info with v 1") glog. V (0). Infoln ("info with v 0")//is called when exiting, ensuring that the log is written to the file in Glog. Flush ()}
There are a few notes:
1, high-level logs will be output to a file lower than its level, such as the error log will be output to the error file, warning file, info file, and so on;
2. When starting, call flag. Parse is used to initialize Glog parameters, such as xxx.exe-log_dir= "./"-v=3. If no parameters are supplied, the Log_dir directory is
Os. TempDir (), the value of V is 0
3, the program exits, you need to call Glog. Flush () to write the log to the file.
4, v function. The default v option is 0, and you can set the V value from the command line. The function of the V function is that when the parameter of the V function is higher than the V value of glog, subsequent info functions are not executed or executed.
Glog. V (2). Info
Equivalent
If Glog. V (2) { Info ()}
The implementation of V is very exciting and interested to see the source code.
5. Run the example, assuming the file name is F.go
Go build f.gof.exe-log_dir= "./"-v=3f.exe-log_dir= "./"-v=2
6. log file name
The log file name consists of the following parts:
The name of the program. The name of the user. Log.xxx.yyyymmdd-hhmmss.pid, where
XXX is a log level, for example info,error,warning
YYYYMMDD is the day of the month
Hhmmss for hours, minutes, seconds
PID is the process ID
7, Glog. The default behavior of fatal is to output the fatal level log and force the process to exit, but you can customize the behavior of the fatal
8, Glog also provides vmodule such a high-level usage, is interested in can study.
Glog Code Address:
Https://github.com/golang/glog