Basic usage of glog

From: Author: hiloves
Blog: http://www.cnblogs.com/hiloves/

1, you must initialize the library before you use glog. To generate the log file, you only need to call it once before starting log:

google::InitGoogleLogging(argv[0]);  //Program name in parentheses

When you want to end glog, you must close the library, or the memory will overflow:

google::ShutdownGoogleLogging();

2. Set the directory to save log files. This directory must already exist, otherwise log files cannot be generated. It must be called before initializing the library.

FLAGS_log_dir = "c:\\Logs";

3. GLOG has four error levels. The enumeration is as follows:

enum SeverityLevel
{
  google::INFO = 0,
  google::WARNING = 1,
  google::ERROR = 2,
  google::FATAL = 3,
};

4. Output log:

LOG(INFO) << "info test";  //Output an Info log
LOG(WARNING) << "warning test";  //Output a Warning log
LOG(ERROR) << "error test";  //Output an Error log
LOG(FATAL) << "fatal test";  //Output a Fatal log, which is the most serious log and aborts the program after output

5. Conditional output:

LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";  //Output log when conditions are met

LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";  //google::COUNTER Record the number of times the statement was executed from1Start, after running the output log for the first time, every 10 Output log information again

LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER << "th big cookie";  //The combination of the two, however, should be noted that every 10 To determine whether the condition is satisfied or not, output the log if it is sluggish, instead of every 10 Output log information once

LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";  //Before the execution of this statement 20 Output log every time, and then no longer output

6. Several functions:

google::SetLogDestination(google::GLOG_INFO, "log/prefix_");  //Sets the output directory and prefix for logs of a specific severity level. The first parameter is the log level, and the second parameter indicates the output directory and log filename prefix

google::SetLogFilenameExtension("logExtension");  //Add an extension after the middle level of the log file name. For all severity levels

google::SetStderrLogging(google::GLOG_INFO);  //Logs larger than the specified level are output to standard output

7. Several parameter settings:

FLAGS_logtostderr = true;  //Set whether log messages go to standard output instead of log files

FLAGS_alsologtostderr = true;  //Set whether the log messages are output to standard except the log files

FLAGS_colorlogtostderr = true;  //Set the color message recorded to standard output (if supported by the terminal)

FLAGS_log_prefix = true;  //Set whether the log prefix should be added to each line of output

FLAGS_logbufsecs = 0;  //Set the maximum number of seconds that the log can be buffered. 0 refers to real-time output

FLAGS_max_log_size = 10;  //Set the maximum log file size in MB

FLAGS_stop_logging_if_full_disk = true;  //Set whether to avoid logging to the disk when the disk is full

8. Example:

// Start google log system:
FLAGS_log_dir = "c:\\Logs";
google::InitGoogleLogging(argv[0]);
google::SetLogDestination(google::GLOG_INFO, "c:\\Logs\\INFO_");
google::SetStderrLogging(google::GLOG_INFO);
google::SetLogFilenameExtension("log_");
FLAGS_colorlogtostderr = true;  // Set log color
FLAGS_logbufsecs = 0;  // Set log output speed(s)
FLAGS_max_log_size = 1024;  // Set max log file size
FLAGS_stop_logging_if_full_disk = true;  // If disk is full
char str[20] = "hello log!";
LOG(INFO) << str;
CStringA cStr = "hello google!";
LOG(INFO) << cStr;
LOG(INFO) << "info test" << "hello log!";  //Output an Info log
LOG(WARNING) << "warning test";  //Output a Warning log
LOG(ERROR) << "error test";  //Output an Error log
google::ShutdownGoogleLogging();

Keywords: Google

Added by scorpioy on Mon, 06 Apr 2020 00:31:36 +0300