基础抽象
其中sink中的log,flush,set_pattern,set_formatter为纯虚函数
formatter为虚类
base_sink
base_sink模板类继承了sink,模板参数提供了同步机制。其实现了基类中的log,flush,set_pattern和set_formatter抽象方法,log方法中会调用抽象方法sink_it_,flush方法调用抽象方法flush_
template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::log(const details::log_msg &msg) {
std::lock_guard<Mutex> lock(mutex_);
sink_it_(msg);
}
template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::flush() {
std::lock_guard<Mutex> lock(mutex_);
flush_();
}
template <typename Mutex>
void SPDLOG_INLINE spdlog::sinks::base_sink<Mutex>::set_pattern(const std::string &pattern) {
std::lock_guard<Mutex> lock(mutex_);
set_pattern_(pattern);
}
template <typename Mutex>
void SPDLOG_INLINE
spdlog::sinks::base_sink<Mutex>::set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter) {
std::lock_guard<Mutex> lock(mutex_);
set_formatter_(std::move(sink_formatter));
}
日志
支持两种日志类型,同步和异步,分别是logger和async_logger,日志级别使用枚举level_enum,定义为
enum level_enum : int {
trace = SPDLOG_LEVEL_TRACE,
debug = SPDLOG_LEVEL_DEBUG,
info = SPDLOG_LEVEL_INFO,
warn = SPDLOG_LEVEL_WARN,
err = SPDLOG_LEVEL_ERROR,
critical = SPDLOG_LEVEL_CRITICAL,
off = SPDLOG_LEVEL_OFF,
n_levels
};
logger和async_logger的关系为
日志创建
对于同步和异步日志,有两种不同的工厂,分别是synchronous_factory和async_factory_impl。
synchronous_factory类通过成员函数模板静态方法create来创建logger
async_factory_impl模板类通过成员函数模板静态方法create来创建async_logger
日志管理
通过registry来统一管理日志,内部使用unordered_map来管理logger
register_logger:将logger注册到unordered_map中
get:从unordered_map中获取name对应的logger
formatter子类
full_formatter:[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] [%s:%#] %v,标识为+
name_formatter:日志名,标识为n
level_formatter:日志级别,标识为l
short_level_formatter:日志级别的缩写,标识为L
t_formatter:显示线程id,标识为t
v_formatter:显示日志内容,标识为v
a_formatter:显示周几的缩写,标识为a
A_formatter:显示周几,标识为A
b_formatter:显示月份的缩写,标识为b或者h
B_formatter:显示月份,标识为B
c_formatter:显示年月日时分秒,标识为c
C_formatter:显示年,其中年只显示2位,标识为C
Y_formatter:显示年,年显示4位,标识为Y
D_formatter:显示年月日,标识为D或者x
m_formatter:显示1-12月,标识为m
d_formatter:显示1-31日,标识为d
H_formatter:显示24小时,标识为H
I_formatter:显示12小时,标识 为 I
M_formatter:显示分钟,标识为 M
S_formatter:显示徐浩然,标识为S
e_formatter:显示毫秒,标识为e
f_formatter:显示微秒,标识为f
F_formatter:显示纳秒,标识为F
E_formatter:显示从epoch的秒数,标识为E
p_formatter:显示am/pm
r_formatter:12小时制显示,包含am/pm,标识为r
R_formatter:24小时制显示,标识为R
T_formatter:ISO 8601时间格式,标识为T或者X
z_formatter:显示时区,标识为z
pid_formatter:显示进程id,标识 为P
color_start_formatter:颜色开始,标识为^
color_stop_formatter:颜色结束,标识为$
source_location_formatter:显示文件名以及行号,标识为@
short_filename_formatter:显示文件名,不包含目录,标识为s
source_filename_formatter:显示全文件名,标识为g
source_linenum_formatter:显示行号,标识 为#
source_funcname_formatter:显示函数名,标识为!
ch_formatter:显示字符,标识为%
elapsed_formatter:离上一次输出日志的经过的时间
- ‘u’:表示纳秒
- ‘i’:表示微秒
- ’o’:表示毫秒
- ‘O’:表示秒