Skip to main content

C++ Logging with glog: A Practical Introduction

·396 words·2 mins
Glog Google C++ Logging
Table of Contents

Google’s glog is a mature and high-performance logging library for C++. It is widely used in production systems and provides rich diagnostics while keeping runtime overhead low. With strong community adoption and long-term stability, glog remains a solid choice for modern C++ projects.


Google Logging Library

🚀 Why Choose glog?
#

glog is designed to deliver powerful logging capabilities without compromising application performance. Its core strengths include:

  • Severity Levels
    Built-in log levels (INFO, WARNING, ERROR, FATAL) allow clear categorization of runtime events.

  • Thread Safety
    Logging is fully thread-safe, making glog suitable for highly concurrent applications.

  • Log Rotation
    Supports automatic log file rotation based on size or time, preventing unbounded file growth.

  • Rich Context Information
    Each log entry includes timestamps, severity, source file names, and line numbers.

  • Stack Tracing
    Automatically records stack traces for fatal errors, greatly simplifying root-cause analysis.

  • Cross-Platform Support
    Works reliably on Linux, Windows, macOS, and other platforms.


🔧 Core Logging Workflow
#

Using glog typically follows a simple pattern:

  1. Initialize the logging library
  2. Configure log destinations and behavior
  3. Write logs using macros
  4. Shut down the library on exit

The API is intentionally stream-based, making log statements intuitive and expressive.


🧪 Simple Code Example
#

#include <glog/logging.h>

int main(int argc, char* argv[]) {
    // Initialize glog
    google::InitGoogleLogging(argv[0]);

    // Configure log destinations
    google::SetLogDestination(google::GLOG_INFO, "logs/info_");
    google::SetLogDestination(google::GLOG_WARNING, "logs/warning_");
    google::SetLogDestination(google::GLOG_ERROR, "logs/error_");
    google::SetLogDestination(google::GLOG_FATAL, "logs/fatal_");

    // Standard logging
    LOG(INFO) << "This is an informational log.";
    LOG(WARNING) << "This is a warning log.";
    LOG(ERROR) << "This is an error log.";

    // Periodic logging
    LOG_EVERY_N(INFO, 10) << "Processed item " << google::COUNTER;

    // Fatal logging terminates the program
    LOG(FATAL) << "Fatal error encountered.";

    google::ShutdownGoogleLogging();
    return 0;
}

🧠 Advanced Logging Features
#

glog provides several advanced macros that go beyond basic logging and are especially useful in large-scale systems.

Feature Description
LOG_IF Logs only when a specified condition evaluates to true.
CHECK / CHECK_EQ Runtime assertions that remain active in optimized builds and emit stack traces on failure.
VLOG Verbose logging controlled at runtime for fine-grained debugging.

Unlike traditional assert, CHECK macros are never compiled out and provide detailed diagnostic output, making them ideal for production environments.


✅ Summary
#

glog offers a powerful, efficient, and battle-tested logging solution for C++ applications. With strong support for concurrency, rich diagnostics, and minimal runtime overhead, it is particularly well-suited for performance-sensitive and large-scale systems. For teams looking for a robust logging foundation, glog remains a dependable choice.

Related

Google Unveils SynthID Text: Open-Source AI Watermarking Tool
·406 words·2 mins
Google SynthID AI Watermarking Open Source
The Lifespan of a Data Center GPU is Only 3 Years
·443 words·3 mins
Google GPU H100
Advanced GDB Debugging for Multi-Threaded Programs
·519 words·3 mins
GDB C++ Multi-Threading Linux