Google Logging (glog) is a C++98 library that implements application-level logging. The 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
8000
span>* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
For a detailed overview of glog features and their usage, please refer to the user guide.
Table of Contents
- Getting Started
- Building from Source
- User Guide
- Severity Levels
- Setting Flags
- Conditional / Occasional Logging
- Debug Mode Support
CHECK
Macros- Verbose Logging
- Custom Log Prefix Format
- Failure Signal Handler
- Performance of Messages
- User-defined Failure Function
- Raw Logging
- Google Style
perror()
- Syslog
- Strip Logging Messages
- Automatically Remove Old Logs
- Notes for Windows Users
- Installation Notes for 64-bit Linux Systems
- How to Contribute
glog supports multiple build systems for compiling the project from source: Bazel, CMake, and vcpkg.
To use glog within a project which uses the
Bazel build tool, add the following lines to
your WORKSPACE
file:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_github_gflags_gflags",
sha256 = "34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf",
strip_prefix = "gflags-2.2.2",
urls = ["https://github.com/gflags/gflags/archive/v2.2.2.tar.gz"],
)
http_archive(
name = "com_github_google_glog",
sha256 = "21bc744fb7f2fa701ee8db339ded7dce4f975d0d55837a97be7d46e8382dea5a",
strip_prefix = "glog-0.5.0",
urls = ["https://github.com/google/glog/archive/v0.5.0.zip"],
)
You can then add @com_github_google_glog//:glog
to the deps section
of a cc_binary
or cc_library
rule, and #include
<glog/logging.h>
to include it in your source code. Here’s a simple example:
cc_binary(
name = "main",
srcs = ["main.cc"],
deps = ["@com_github_google_glog//:glog"],
)
glog also supports CMake that can be used to build the project on a wide range of platforms. If you don’t have CMake installed already, you can download it for from CMake’s official website.
CMake works by generating native makefiles or build projects that can be used in the compiler environment of your choice. You can either build glog with CMake as a standalone project or it can be incorporated into an existing CMake build for another project.
When building glog as a standalone project, on Unix-like systems with GNU Make as build tool, the typical workflow is:
- Get the source code and change to it. e.g., cloning with git:
git clone https://github.com/google/glog.git cd glog
- Run CMake to configure the build tree.
cmake -S . -B build -G "Unix Makefiles"CMake provides different generators, and by default will pick the most relevant one to your environment. If you need a specific version of Visual Studio, use
cmake . -G <generator-name>
, and seecmake --help
for the available generators. Also see-T <toolset-name>
, which can be used to request the native x64 toolchain with-T host=x64
.
- Afterwards, generated files can be used to compile the project.
cmake --build build
- Test the build software (optional).
cmake --build build --target test
- Install the built files (optional).
cmake --build build --target install
If you have glog installed in your system, you can use the CMake command
find_package
to build against glog in your CMake Project as follows:
cmake_minimum_required (VERSION 3.0.2)
project (myproj VERSION 1.0)
find_package (glog 0.6.0 REQUIRED)
add_executable (myapp main.cpp)
target_link_libraries (myapp glog::glog)
Compile definitions and options will be added automatically to your target as needed.
You can also use the CMake command add_subdirectory
to include glog
directly from a subdirectory of your project by replacing the
find_package
call from the previous example by
add_subdirectory
. The glog::glog
target is in this case an
ALIAS
library target for the glog
library target.
Again, compile definitions and options will be added automatically to your target as needed.
You can download and install glog using the vcpkg dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install glog
The glog port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
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, customize the prefix attached to log messages, and more.
Following sections describe the functionality supported by glog. Please note this description may not be complete but limited to the most useful ones. If you want to find less common features, please check header files under src/glog directory.
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
, and INFO
.
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
or
FATAL
to standard error (stderr
) in addition to log files.
Several flags influence glog’s output behavior. If the Google gflags library is installed on your machine, the build
system 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=1
If 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:
logtostderr
(bool
, default=false
)- 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). stderrthreshold
(int
, default=2, which isERROR
)- 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, respectively. minloglevel
(int
, default=0, which isINFO
)- Log messages at or above this level. Again, the numbers of severity
levels
INFO
,WARNING
,ERROR
, andFATAL
are 0, 1, 2, and 3, respectively. log_dir
(string
, default="")- If specified, logfiles are written into this directory instead of the default logging directory.
v
(int
, default=0)- Show all
VLOG(m)
messages form
less or equal the value of this flag. Overridable by--vmodule
. See the section about verbose logging for more detail. vmodule
(string
, default="")- Per-module verbose level. The argument has to contain a
comma-separated list of <module name>=<log level>. <module name> is a
glob pattern (e.g.,
gfs*
for all modules whose name starts with "gfs"), matched against the filename base (that is, name ignoring .cc/.h./-inl.h). <log level> overrides any value given by--v
. See also the section about verbose logging.
There are some other flags defined in logging.cc. Please grep the source
code for DEFINE_
to see a complete list of all flags.
You can also modify flag values in your program by modifying global
variables FLAGS_*
. Most settings start working immediately after
you update FLAGS_*
. The exceptions are the flags related to
destination files. For example, you might want to set FLAGS_log_dir
before calling google::InitGoogleLogging
. Here is an example:
LOG(INFO) << "file";
// Most flags work immediately after updating values.
FLAGS_logtostderr = 1;
LOG(INFO) << "stderr";
FLAGS_logtostderr = 0;
// This won’t change the log destination. If you want to set this
// value, you should do this before google::InitGoogleLogging .
FLAGS_log_dir = "/some/log/directory";
LOG(INFO) << "the same file";
Sometimes, you may only want to log a message under certain conditions. You can use the following macros to perform conditional logging:
LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
The "Got lots of cookies" message is logged only when the variable
num_cookies
exceeds 10. If a line of code is executed many times, it
may be useful to only log a message at certain intervals. This kind of
logging is most useful for informational messages.
LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
The above line outputs a log messages on the 1st, 11th, 21st, ... times
it is executed. Note that the special google::COUNTER
value is used
to identify which repetition is happening.
You can combine conditional and occasional logging with the following macro.
LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
<< "th big cookie";
Instead of outputting a message every nth time, you can also limit the output to the first n occurrences:
LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
Outputs log messages for the first 20 times it is executed. Again, the
google::COUNTER
identifier indicates which repetition is happening.
Other times, it is desired to only log a message periodically based on a time. So for example, to log a message every 10ms:
LOG_EVERY_T(INFO, 0.01) << "Got a cookie";