在上一篇文章中,主要记录了如何使用 Easylogging++ 的配置功能,虽然已经用了很大的篇幅尽可能详细地加以记录,不过相信有些细心的小伙伴可能已经发现遗漏了些什么,请看下面两句摘自 my_log.conf 配置文件的语句:
- FORMAT = "[%level | %datetime] | %msg"
- FILENAME = "log\\log_%datetime{%Y%M%d}.log"
FORMAT = "[%level | %datetime] | %msg"
FILENAME = "log\\log_%datetime{%Y%M%d}.log"
对于等号右边的值,比如 %level、%datetime{%Y%M%d} 这些特殊符号的意义和用法在上一文中并没有提及,所以就在这里对此进行一个补充说明。而这些特殊的符号,就是 Easylogging++ 用于控制日志输出的格式说明符。
输出格式说明符
和C语言的格式化输出一样,Easylogging++ 也可以格式化输出日志,下面的表格列举了GitHub上给出的 Easylogging++ 支持的输出格式说明符:
Specifier | Replaced By |
---|---|
%logger | Logger ID |
%thread | Thread ID - Uses std::thread if available, otherwise GetCurrentThreadId() on windows |
%level | Severity level (Info, Debug, Error, Warning, Fatal, Verbose, Trace) |
%levshort | Severity level (Short version i.e, I for Info and respectively D, E, W, F, V, T) |
%vlevel | Verbosity level (Applicable to verbose logging) |
%datetime | Date and/or time - Pattern is customizable - see Date/Time Format Specifiers below |
%user | User currently running application |
%host | Computer name application is running on |
%file | File name of source file (Full path) |
%fbase | File name of source file (Only base name) |
%line | Source line number |
%func | Logging function |
%loc | Source filename and line number of logging (separated by colon) |
%msg | Actual log message |
% | Escape character (e.g, %%level will write %level) |
另外,Easylogging++ 还提供了让我们自定义格式说明符的能力。下面的代码演示了如何自定义 %ip_addr 这个格式说明符:
- #include "easylogging++.h"
- INITIALIZE_EASYLOGGINGPP
- const char* getIp(void)
- {
- return "192.168.1.1";
- }
- int main(void)
- {
- /// 自定义格式说明符 %ip_addr
- el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier("%ip_addr", getIp));
- /// 利用自定义格式说明符 %ip_addr 进行日志输出
- el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Format, "%datetime %level %ip_addr : %msg");
- LOG(INFO) << "This is request from client";
- system("pause");
- return 0;
- }
#include "easylogging++.h"
INITIALIZE_EASYLOGGINGPP
const char* getIp(void)
{
return "192.168.1.1";
}
int main(void)
{
/// 自定义格式说明符 %ip_addr
el::Helpers::installCustomFormatSpecifier(el::CustomFormatSpecifier("%ip_addr", getIp));
/// 利用自定义格式说明符 %ip_addr 进行日志输出
el::Loggers::reconfigureAllLoggers(el::ConfigurationType::Format, "%datetime %level %ip_addr : %msg");
LOG(INFO) << "This is request from client";
system("pause");
return 0;
}
时间格式说明符
在输出格式说明符中有一个专门用于格式化输出时间的说明符:%datetime ,而 Easylogging++ 已经默认为 %datetime 设置了一个时间输出格式。然而,我们可以利用时间格式说明符重新自定义时间的输出格式,下面的表格列举了GitHub上给出的 Easylogging++ 支持的时间格式说明符:
Specifier | Replaced By |
---|---|
%d | Day of month (zero-padded) |
%a | Day of the week - short (Mon, Tue, Wed, Thu, Fri, Sat, Sun) |
%A | Day of the week - long (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) |
%M | Month (zero-padded) |
%b | Month - short (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec) |
%B | Month - Long (January, February, March, April, May, June, July, August, September, October, November, December) |
%y | Year - Two digit (13, 14 etc) |
%Y | Year - Four digit (2013, 2014 etc) |
%h | Hour (12-hour format) |
%H | Hour (24-hour format) |
%m | Minute (zero-padded) |
%s | Second (zero-padded) |
%g | Milliseconds (width is configured by ConfigurationType::MillisecondsWidth) |
%F | AM/PM designation |
% | Escape character |
注意事项
在刚开始使用 Easylogging++ 作为我的日志库的时候,我曾经写过下面这样一个配置文件:
- * GLOBAL:
- FORMAT = "[%level | %datetime] | %msg"
- ENABLED = true
- TO_FILE = true
- TO_STANDARD_OUTPUT = true
- MILLISECONDS_WIDTH = 3
- PERFORMANCE_TRACKING = false
- MAX_LOG_FILE_SIZE = 2097152 ##
- LOG_FLUSH_THRESHOLD = 0
- * INFO:
- FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
- * DEBUG:
- ENABLED = false
- * WARNING:
- FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
- * TRACE:
- FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
- * VERBOSE:
- ENABLED = false
- * ERROR:
- FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
- * FATAL:
- ENABLED = false
* GLOBAL:
FORMAT = "[%level | %datetime] | %msg"
ENABLED = true
TO_FILE = true
TO_STANDARD_OUTPUT = true
MILLISECONDS_WIDTH = 3
PERFORMANCE_TRACKING = false
MAX_LOG_FILE_SIZE = 2097152 ##
LOG_FLUSH_THRESHOLD = 0
* INFO:
FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
* DEBUG:
ENABLED = false
* WARNING:
FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
* TRACE:
FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
* VERBOSE:
ENABLED = false
* ERROR:
FILENAME = "log\\server_%level_%datetime{%Y%M%d%H}.log"
* FATAL:
ENABLED = false
配置文件的意思很简单,就是我想让不同级别的日志保存在不同的文件中而已,但是事实上,如果使用上面的这个配置文件,那么在 log 日志文件夹下就只会生成一个名称为 server_%level_2015102715.log 的日志文件,并且所有级别的日志都只会保存在这个日志文件中。
从上面给出的反例中可以看出,对于输出格式说明符,只适用于配置项 FORMAT;而时间格式说明符,不仅适用于配置项 FORMAT,同样还适用于配置项 FILENAME。这一点,通过查看 Easylogging++ 的源码也可以得到证实。当然,如果我们想要让配置项 FILENAME 也能够使用输出格式说明符,可以通过修改源码去实现。建议有兴趣的小伙伴参考一下函数 updateFormatSpec() ,然后在函数 resolveFilename() 中修改即可。