Introduction
Google glog is a library that implements application-level logging. This library provides logging APIs based on C++-style streams and various helper macros. You can log a message by simply streaming things to LOG(<a particular severity level>), e.g.
#include <glog/logging.h> int main(int argc, char* argv[]) { // Initialize Google's logging library. google::InitGoogleLogging(argv[0]); // ... LOG(INFO) << "Found " << num_cookies << " cookies"; }
Google glog defines a series of macros that simplify many common logging tasks. You can log messages by severity level, control logging behavior from the command line, log based on conditionals, abort the program when expected conditions are not met, introduce your own verbose logging levels, and more. This document describes the functionality supported by glog. Please note that this document doesn't describe all features in this library, but the most useful ones. If you want to find less common features, please check header files under src/glog
directory.
Severity Level
You can specify one of the following severity levels (in increasing order of severity): INFO
, WARNING
, ERROR
, and FATAL
. Logging a FATAL
message terminates the program (after the message is logged). Note that messages of a given severity are logged not only in the logfile for that severity, but also in all logfiles of lower severity. E.g., a message of severity FATAL
will be logged to the logfiles of severity FATAL
, ERROR
, WARNING
, andINFO
.
The DFATAL
severity logs a FATAL
error in debug mode (i.e., there is no NDEBUG
macro defined), but avoids halting the program in production by automatically reducing the severity to ERROR
.
Unless otherwise specified, glog writes to the filename "/tmp/<program name>.<hostname>.<user name>.log.<severity level>.<date>.<time>.<pid>" (e.g., "/tmp/hello_world.example.com.hamaji.log.INFO.20080709-222411.10474"). By default, glog copies the log messages of severity level ERROR
orFATAL
to standard error (stderr) in addition to log files.
Setting Flags
Several flags influence glog's output behavior. If the Google gflags library is installed on your machine, the configure
script (see the INSTALL file in the package for detail of this script) will automatically detect and use it, allowing you to pass flags on the command line. For example, if you want to turn the flag --logtostderr
on, you can start your application with the following command line:
./your_application --logtostderr=1If the Google gflags library isn't installed, you set flags via environment variables, prefixing the flag name with "GLOG_", e.g.
GLOG_logtostderr=1 ./your_application
The following flags are most commonly used:
-
Log messages to stderr instead of logfiles.
Note: you can set binary flags totrue
by specifying1
,true
, oryes
(case insensitive). Also, you can set binary flags tofalse
by specifying0
,false
, orno
(again, case insensitive). -
Copy log messages at or above this level to stderr in addition to logfiles. The numbers of severity levels
INFO
,WARNING
,ERROR
, andFATAL
are 0, 1, 2, and 3,
logtostderr
(
bool
, default=
false
)
stderrthreshold
(
int
, default=2, which is
ERROR
)