cdcf::Logger - thoughtworks-hpc/cdcf GitHub Wiki
Log Format
Log is in the following format:
[2020-07-10 07:50:36.860] [log_level] [source_file:line_number] Some message
Logger Initialization
You should init logger when program start and only once.
cdcf::Logger::Init(cfg);
cfg
is instance of CDCFConfig or it's child.
Logger Initialization will read following config:
- log_file: log file name
- log_file_size: maximum rotating log file size in bytes
- log_file_num: maximum rotating log file number, each file has size
log_file_size_in_bytes
- log_no_display_filename_and_line_num: whether display filename and line num in log
- log_level: log level, default: info
- no_log_to_console:whether print to console
- print-ping-log(node keeper only): whether print ping log, default: false
Logger print to console per default, print to file if you provide log_file
in config and enable rotating if you provide log_file
, log_file_size
and log_file_num
.
Basic file logger
Logger initialize with log_file_size_in_bytes_ = 0
have no limitation on its log file size.
Rotating logger
When the max file size is reached, close the file, rename it, and create a new file, the number of new file that can be created is limited by the log file number.
The following example creates a rotating logger that has maximum log file size of 1024 bytes and allows 1 extra log file, when 1024 bytes are reached, the logger will rename cdcf.log
to cdcf.1.log
and create a new cdcf.log
, when the new cdcf.log
is filled again, the same renaming happens and the old cdcf.1.log
will be deleted, hence the ROTATING LOGGER.
cfg.log_file_size_in_bytes_ = 1024;
cfg.log_file_number_ = 1;
cdcf::Logger::Init(cfg);
Logger Usage
Log Level
The logger has the following log levels:
"trace", "debug", "info", "warning", "error", "critical", "off"
Use certain function like 'Info' to log in that level:
CDCF_LOGGER_INFO("Hello World");
// [2020-07-10 07:50:36.860] [info] [/cdcf/main.cc:8] Hello World
Message Formatting
The logger use the fmt formatting library
CDCF_LOGGER_DEBUG("detect node {}:{} up.", "127.0.0.1", 55555);
// [2020-07-10 07:50:36.860] [info] [/cdcf/node_keeper/src/membership.cc:379] detect node 127.0.0.1:55555 up
Working with Elastic Stack
Use the following filter in LogStash to process logs generated by cdcf::Logger:
filter {
grok {
match => { "message" => "%{DATA}%{TIMESTAMP_ISO8601:timestamp}]%{DATA} %{DATA}%{WORD:level}%{DATA} %{DATA}%{PATH:source}:%{NUMBER:line_num}%{DATA} %{GREEDYDATA:line}" }
}
}