Logging in C++

Here’s the C++ translation of the logging example, formatted in Markdown suitable for Hugo:

Our example demonstrates various logging techniques in C++. We’ll use the iostream library for basic output and create a simple custom logging class for more advanced features.

#include <iostream>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <fstream>

class Logger {
public:
    Logger(std::ostream& os, const std::string& prefix = "") 
        : os_(os), prefix_(prefix) {}

    void log(const std::string& message) {
        auto now = std::chrono::system_clock::now();
        auto in_time_t = std::chrono::system_clock::to_time_t(now);

        std::stringstream ss;
        ss << std::put_time(std::localtime(&in_time_t), "%Y/%m/%d %X");
        os_ << prefix_ << ss.str() << " " << message << std::endl;
    }

    void setPrefix(const std::string& prefix) {
        prefix_ = prefix;
    }

private:
    std::ostream& os_;
    std::string prefix_;
};

int main() {
    // Using standard output for logging
    std::cout << "standard logger" << std::endl;

    // Creating a custom logger
    Logger mylog(std::cout, "my:");
    mylog.log("from mylog");

    // Changing the prefix
    mylog.setPrefix("ohmy:");
    mylog.log("from mylog");

    // Logging to a file
    std::ofstream file("log.txt");
    Logger filelog(file, "file:");
    filelog.log("hello");

    // Reading from the file and printing to console
    file.close();
    std::ifstream infile("log.txt");
    std::string line;
    std::getline(infile, line);
    std::cout << "from filelog:" << line << std::endl;

    return 0;
}

This C++ example demonstrates basic logging techniques. Here’s a breakdown of what’s happening:

  1. We start by including necessary headers for input/output operations, time handling, and string manipulation.

  2. We define a simple Logger class that can output timestamped messages to any ostream. This class allows setting a prefix for the log messages.

  3. In the main function, we first use standard output directly for logging.

  4. We then create a custom logger mylog that outputs to std::cout with a prefix.

  5. We demonstrate changing the prefix of the logger.

  6. We show how to log to a file by creating a Logger that writes to an ofstream.

  7. Finally, we read from the log file and print its contents to the console.

This example doesn’t include structured logging or JSON output, as these are not standard features in C++. For such functionality, you would typically use a third-party logging library.

To compile and run this program:

$ g++ -std=c++11 logging.cpp -o logging
$ ./logging
standard logger
my:2023/05/23 10:45:16 from mylog
ohmy:2023/05/23 10:45:16 from mylog
from filelog:file:2023/05/23 10:45:16 hello

Note that the actual date and time in the output will depend on when you run the program.

C++ doesn’t have built-in structured logging like the slog package in Go. For advanced logging features, consider using a library like spdlog or glog.