glog - yuhannah/skills_map GitHub Wiki

GLOG使用说明

以下内容来自logging.h文件:

功能简介

// The global value of GOOGLE_STRIP_LOG. All the messages logged to
// LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
// If it can be determined at compile time that the message will not be
// printed, the statement will be compiled out.
//
// Example: to strip out all INFO and WARNING messages, use the value
// of 2 below. To make an exception for WARNING messages from a single
// file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including
// base/logging.h
#ifndef GOOGLE_STRIP_LOG
#define GOOGLE_STRIP_LOG 0
#endif

严重程度小于GOOGLE_STRIP_LOG的所有LOG(XXX)的消息将不会显示。如果在编译时可以确定消息不会被打印,则语句将被编译出来。

示例:

  • 要删除所有==INFO==和==WARNING==消息,请使用#define GOOGLE_STRIP_LOG 2
  • 要对来自单个文件的==WARNING==消息进行异常处理,请在包含base/logging.h之前向该文件添加#define GOOGLE_STRIP_LOG 1

LOG部分

// Make a bunch of macros for logging.  The way to log things is to stream
// things to LOG(<a particular severity level>).  E.g.,
//
//   LOG(INFO) << "Found " << num_cookies << " cookies";

创建一堆用于日志记录的宏。记录事件的方法是将事件流记录到日志中(<特定的严重级别>)。

// You can capture log messages in a string, rather than reporting them
// immediately:
//
//   vector<string> errors;
//   LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
//
// This pushes back the new error onto 'errors'; if given a NULL pointer,
// it reports the error via LOG(ERROR).

您可以捕获字符串中的日志消息,而不是立即报告它们。这将新的错误推到errors上;如果给定一个空指针,它将通过LOG(ERROR)报告错误。

// You can also do conditional logging:
//
//   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";

您也可以进行条件日志记录。

// You can also do occasional logging (log every n'th occurrence of an
// event):
//
//   LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
//
// The above will cause log messages to be output on the 1st, 11th, 21st, ...
// times it is executed.  Note that the special google::COUNTER value is used
// to identify which repetition is happening.

您还可以偶尔进行日志记录(记录一个事件的每n次发生)。以上将导致日志消息在它执行的1、11、21、…次进行输出。注意,特殊的google::COUNTER值用于识别正在发生的重复。

// You can also do occasional conditional logging (log every n'th
// occurrence of an event, when condition is satisfied):
//
//   LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
//                                           << "th big cookie";

您还可以偶尔进行条件日志记录(当条件满足时,记录一个事件的每n次发生)。

// You can log messages the first N times your code executes a line. E.g.
//
//   LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
//
// Outputs log messages for the first 20 times it is executed.

您可以在代码执行一行时的前N次记录消息。如,在前20次执行日志消息时输出日志消息。

SYSLOG部分

// These log to syslog as well as to the normal logs.  If you use these at
// all, you need to be aware that syslog can drastically reduce performance,
// especially if it is configured for remote logging!  Don't use these
// unless you fully understand this and have a concrete need to use them.
// Even then, try to minimize your use of them.

可以使用类似的SYSLOGSYSLOG_IFSYSLOG_EVERY_N宏。这些向syslog以及普通日志中记录日志。如果您使用这些工具,那么您需要知道==syslog会极大地降低性能,特别是在为远程日志配置syslog时==!不要使用它们,除非你完全理解这一点,并有一个具体的需要使用它们。即使这样,也要尽量减少使用它们。

DLOG部分

// There are also "debug mode" logging macros like the ones above:
//
//   DLOG(INFO) << "Found cookies";
//
//   DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
//
//   DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
//
// All "debug mode" logging is compiled away to nothing for non-debug mode
// compiles.

还有像上面那样的“调试模式”日志宏。对于非调试模式编译,所有“调试模式”日志记录都被编译为空。

// We also have
//
//   LOG_ASSERT(assertion);
//   DLOG_ASSERT(assertion);
//
// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;

还有简化的LOG_ASSERT(assertion);对应LOG_IF(FATAL, assert fails) << assertion;以及DLOG_ASSERT(assertion);对应 DLOG_IF(FATAL, assert fails) << assertion;

VLOG部分

// There are "verbose level" logging macros.  They look like
//
//   VLOG(1) << "I'm printed when you run the program with --v=1 or more";
//   VLOG(2) << "I'm printed when you run the program with --v=2 or more";
//
// These always log at the INFO log level (when they log at all).
// The verbose logging can also be turned on module-by-module.  For instance,
//    --vmodule=mapreduce=2,file=1,gfs*=3 --v=0
// will cause:
//   a. VLOG(2) and lower messages to be printed from mapreduce.{h,cc}
//   b. VLOG(1) and lower messages to be printed from file.{h,cc}
//   c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
//   d. VLOG(0) and lower messages to be printed from elsewhere
//
// The wildcarding functionality shown by (c) supports both '*' (match
// 0 or more characters) and '?' (match any single character) wildcards.

有“详细级别”的日志宏。它们总是在==INFO==日志级别进行日志记录(当它们进行日志记录时)。详细的日志记录也可以逐个模块打开。

例如,配置--vmodule=mapreduce=2,file=1,gfs*=3 --v=0将产生以下结果:

a. 从mapreduce.{h,cc}中打印VLOG(2)及以下消息。

b. 从file.{h,cc}中打印VLOG(1)及以下消息。

c. 从以“gfs”为前缀的文件中打印VLOG(3)及以下消息。

d. 从其他地方打印VLOG(0)及以下消息。

(c)中显示的通配符功能支持'*'(匹配0个或多个字符)和'?'(匹配任何单个字符)通配符。

// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
//
//   if (VLOG_IS_ON(2)) {
//     // do some logging preparation and logging
//     // that can't be accomplished with just VLOG(2) << ...;
//   }

还有VLOG_IS_ON(n)“详细级别”条件宏。做一些仅用VLOG(2) <<…无法完成的日志准备和日志记录。

// There are also VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N "verbose level"
// condition macros for sample cases, when some extra computation and
// preparation for logs is not needed.
//   VLOG_IF(1, (size > 1024))
//      << "I'm printed when size is more than 1024 and when you run the "
//         "program with --v=1 or more";
//   VLOG_EVERY_N(1, 10)
//      << "I'm printed every 10th occurrence, and when you run the program "
//         "with --v=1 or more. Present occurence is " << google::COUNTER;
//   VLOG_IF_EVERY_N(1, (size > 1024), 10)
//      << "I'm printed on every 10th occurence of case when size is more "
//         " than 1024, when you run the program with --v=1 or more. ";
//         "Present occurence is " << google::COUNTER;

当不需要对日志进行额外的计算和准备时,还可以使用VLOG_IFVLOG_EVERY_NVLOG_IF_EVERY_N“详细级别”条件宏作为示例用例。

日志文件属性

// The supported severity levels for macros that allow you to specify one
// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
// Note that messages of a given severity are logged not only in the
// logfile for that severity, but also in all logfiles of lower severity.
// E.g., a message of severity FATAL will be logged to the logfiles of
// severity FATAL, ERROR, WARNING, and INFO.

允许您指定一个宏的受支持的严重级别是(按严重程度的递增顺序)INFO, WARNING,ERROR,和FATAL。请注意,给定严重程度的消息不仅记录在该严重程度的日志文件中,还记录在所有较低严重程度的日志文件中。例如,严重致命的消息将被记录到严重致命、错误、警告和信息的日志文件中。

// There is also the special severity of DFATAL, which logs FATAL in
// debug mode, ERROR in normal mode.
//
// Very important: logging a message at the FATAL severity level causes
// the program to terminate (after the message is logged).
//
// Unless otherwise specified, logs will be written to the filename
// "<program name>.<hostname>.<user name>.log.<severity level>.", followed
// by the date, time, and pid (you can't prevent the date, time, and pid
// from being in the filename).
//
// The logging code takes two flags:
//     --v=#           set the verbose level
//     --logtostderr   log all the messages to stderr instead of to logfiles

还有DFATAL的特殊严重性,它在调试模式下记录FATAL,在正常模式下记录ERROR。非常重要:以致命的严重级别记录消息会导致程序终止(在记录消息之后)。除非另外指定,否则日志将被写入文件名“<program name>.<hostname>.<user name>.log.<severity level>.,后面跟着日期、时间和pid(您不能阻止日期、时间和pid出现在文件名中)。

// LOG LINE PREFIX FORMAT
//
// Log lines have this form:
//
//     Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
//
// where the fields are defined as follows:
//
//   L                A single character, representing the log level
//                    (eg 'I' for INFO)
//   mm               The month (zero padded; ie May is '05')
//   dd               The day (zero padded)
//   hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
//   threadid         The space-padded thread ID as returned by GetTID()
//                    (this matches the PID on Linux)
//   file             The file name
//   line             The line number
//   msg              The user-supplied message
//
// Example:
//
//   I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
//   I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
//
// NOTE: although the microseconds are useful for comparing events on
// a single machine, clocks on different machines may not be well
// synchronized.  Hence, use caution when comparing the low bits of
// timestamps from different machines.

日志文件格式。注意:虽然微秒对于比较单个机器上的事件很有用,但是不同机器上的时钟可能不能很好地同步。因此,在比较来自不同机器的低位时间戳时要小心。

总结

常用:

  • LOG(INFO) << "Found " << num_cookies << " cookies";
  • LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
  • LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
  • LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER << "th big cookie";
  • LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";

参考资料:https://www.cnblogs.com/tianyajuanke/archive/2013/02/22/2921850.html

⚠️ **GitHub.com Fallback** ⚠️