glog (Google Logging Library)

Language: CPP

Logging

glog was developed at Google to provide developers with a powerful yet simple logging solution for C++ projects. Unlike syslog or printf-based approaches, glog adds structured logging with severity levels, stack traces on fatal errors, and flexible runtime configuration. It has since been open-sourced and is widely used across many C++ projects.

glog is Google’s C++ logging library that provides application-level logging with different severity levels (INFO, WARNING, ERROR, FATAL). It is designed for robustness, simplicity, and performance, making it suitable for large-scale systems.

Installation

cmake: find_package(glog CONFIG REQUIRED)
vcpkg: vcpkg install glog
conan: conan install glog
manual: git clone https://github.com/google/glog && cmake . && make && make install

Usage

glog provides macros for logging at different severity levels. It also supports logging to files, conditional logging, and custom log sinks.

Simple logging

#include <glog/logging.h>

int main(int argc, char* argv[]) {
    google::InitGoogleLogging(argv[0]);
    LOG(INFO) << "This is an info message.";
    LOG(WARNING) << "This is a warning message.";
    LOG(ERROR) << "This is an error message.";
    // LOG(FATAL) << "This is fatal and will terminate.";
    return 0;
}

Logs messages at different severity levels. `FATAL` logs terminate the program after printing a stack trace.

Conditional logging

int x = 5;
LOG_IF(INFO, x > 0) << "x is positive";

Logs a message only if the condition is true.

Verbose logging

FLAGS_v = 2;
VLOG(1) << "Verbose logging at level 1";
VLOG(2) << "Verbose logging at level 2";

Provides verbose logging with runtime-controlled levels.

Check macros

int* ptr = nullptr;
CHECK_NOTNULL(ptr);

`CHECK` macros validate conditions and abort with logs if they fail.

Custom log sink

class MyLogSink : public google::LogSink {
  void send(google::LogSeverity severity, const char* full_filename,
            const char* base_filename, int line, const struct ::tm* tm_time,
            const char* message, size_t message_len) override {
      std::cout << "Custom log: " << message << std::endl;
  }
};

MyLogSink sink;
google::AddLogSink(&sink);

Implements a custom sink to redirect logs to a different output.

Error Handling

Logs not appearing: Ensure `InitGoogleLogging` is called and check that log files are accessible.
Fatal errors terminate unexpectedly: Use `LOG(ERROR)` instead of `LOG(FATAL)` unless termination is required.
Performance overhead in production: Use lower verbosity levels and disable unnecessary logs with runtime flags.

Best Practices

Initialize glog early in the main function using `InitGoogleLogging`.

Use severity levels consistently to separate normal logs from warnings and errors.

Avoid `LOG(FATAL)` except for unrecoverable errors; it terminates the application.

Use `CHECK` macros to enforce invariants in critical code paths.

Combine glog with monitoring tools by redirecting log sinks if necessary.