Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to install and use GLog

📅 2017-Oct-26 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, glog, log ⬩ 📚 Archive

GLog is the logging library from Google for C++. It makes it real easy for you to add logging to any C++ application.

$ sudo apt install libgoogle-glog-dev
#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 a fatal message";

    return 0;
}
foobar.home-machine.ashwin.log.ERROR.20171026-220607.21911
foobar.home-machine.ashwin.log.INFO.20171026-220607.21911
foobar.home-machine.ashwin.log.WARNING.20171026-220607.21911

Format:
program_name.hostname.user_name.log.level.date.time.pid

The file with INFO in its name has log messages of levels INFO and above. The file with WARNING int its name has log messages of levels WARNING and above. Similarly, for the file with ERROR in its name.

In addition, 3 symbolic links are created in the same logging directory pointing to the latest log files. These 3 filenames are of the format:

foobar.ERROR
foobar.INFO
foobar.WARNING

Reference: GLog documentation


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧