easylogging++学习记录(一):接入

easylogging++是一个很是轻量级而且很是高效的一个日志库,支持文件配置,支持线程安全,而且其自定义格式很是的方便,最关键的是,其全部代码都集中在一个.h头文件之中,彻底不须要引用第三方库,接入时很是的方便。在其github首页上给出了最简单的接入例子:c++

#include "easylogging++.h" INITIALIZE_EASYLOGGINGPP int main(int argc, char* argv[]) { LOG(INFO) << "My first info log using default logger"; return 0; }

只须要引用一下头文件,而且调用下INITIALIZE_EASYLOGGINGPP宏进行初始化,INITIALIZE_EASYLOGGINGPP紧随着引用头文件以后的位置便可(不能放在头文件中哦),若是不调用这个宏,那么会报出如下错误:git

$ g++ test.cpp -o main -std=c++11
/tmp/ccPBiVzm.o: In function `el::base::debug::crashReason(int)':
test.cpp:(.text+0x1ed): undefined reference to `el::base::elStorage' /tmp/ccPBiVzm.o: In function `el::base::LogFormat::updateFormatSpec()':
test.cpp:(.text._ZN2el4base9LogFormat16updateFormatSpecEv[_ZN2el4base9LogFormat16updateFormatSpecEv]+0xb43): undefined reference to `_ZN2el4base5utils13s_currentUserB5cxx11E' test.cpp:(.text._ZN2el4base9LogFormat16updateFormatSpecEv[_ZN2el4base9LogFormat16updateFormatSpecEv]+0xb85): undefined reference to `_ZN2el4base5utils13s_currentUserB5cxx11E' test.cpp:(.text._ZN2el4base9LogFormat16updateFormatSpecEv[_ZN2el4base9LogFormat16updateFormatSpecEv]+0xc0d): undefined reference to `_ZN2el4base5utils13s_currentHostB5cxx11E' /tmp/ccPBiVzm.o: In function `el::base::LogDispatcher::dispatch()':
test.cpp:(.text._ZN2el4base13LogDispatcher8dispatchEv[_ZN2el4base13LogDispatcher8dispatchEv]+0x55): undefined reference to `el::base::elStorage' test.cpp:(.text._ZN2el4base13LogDispatcher8dispatchEv[_ZN2el4base13LogDispatcher8dispatchEv]+0x94): undefined reference to `el::base::elStorage' test.cpp:(.text._ZN2el4base13LogDispatcher8dispatchEv[_ZN2el4base13LogDispatcher8dispatchEv]+0xaf): undefined reference to `el::base::elStorage' test.cpp:(.text._ZN2el4base13LogDispatcher8dispatchEv[_ZN2el4base13LogDispatcher8dispatchEv]+0xfe): undefined reference to `el::base::elStorage' /tmp/ccPBiVzm.o: In function `el::base::MessageBuilder::initialize(el::Logger*)':
test.cpp:(.text._ZN2el4base14MessageBuilder10initializeEPNS_6LoggerE[_ZN2el4base14MessageBuilder10initializeEPNS_6LoggerE]+0x1c): undefined reference to `el::base::elStorage' /tmp/ccPBiVzm.o:test.cpp:(.text._ZN2el4base14MessageBuilderlsEm[_ZN2el4base14MessageBuilderlsEm]+0x33): more undefined references to `el::base::elStorage' follow
collect2: error: ld returned 1 exit status

我当前使用的easylogger++是v9.80版本,这个版本是须要c++11的支持的。不然没法编译经过。github

编译经过以后,运行程序:安全

$ ./main 2018-06-07 23:34:24,947 INFO  [default] My first info log using default logger $ ls easylogging++.h logs main test.cpp $ cd logs/ $ ls myeasylog.log

运行程序以后,发现当前文件夹下多了一个文件夹 logs,进入文件夹logs以后,发现日志内容输入到了myeasylog.log内。多线程

若是想自定义日志文件名字和日志格式,就须要配置本身的配置文件,log.conf:ui

* GLOBAL: FORMAT =   "[%level | %datetime] | %msg" ENABLED =   true TO_FILE =   true TO_STANDARD_OUTPUT =   false PERFORMANCE_TRACKING =   false MAX_LOG_FILE_SIZE =   209715200 ## Throw log files away after 2097152 2MB / 209715200 200MB / 4398046511104 1GB * INFO: FILENAME =   "log/info_%datetime{%Y%M%d%H}.log"
* DEBUG: FILENAME =   "log/debug_%datetime{%Y%M%d%H}.log"
* WARNING: FILENAME =   "log/warn_%datetime{%Y%M%d%H}.log"
* TRACE: * VERBOSE: FORMAT =   "%level-%vlevel | %datetime{%d/%M/%y} | %msg"
* ERROR: FILENAME =   "log/error_%datetime{%Y%M%d%H}.log" TO_STANDARD_OUTPUT =   true
* FATAL: FILENAME =   "log/fatal_%datetime{%Y%M%d%H}.log" TO_STANDARD_OUTPUT =   true

配置项都很是的简单明了,GLOBAL是全局配置,而后下面是各级配置,各级日志配置项若为空,就选用的全局日志的配置项。要让本身的配置生效,须要在代码中新增一些逻辑:spa

#include "easylogging++.h" INITIALIZE_EASYLOGGINGPP void init() { el::Configurations conf("log.conf"); el::Loggers::reconfigureAllLoggers(conf); } int main() { init(); LOG(INFO) << "My first info log using default logger"; return 0; }

接着加上一个编译宏ELPP_NO_DEFAULT_LOG_FILE进行编译,不然没法生成自定义的日志文件:线程

$ g++ test.cpp -o main -std=c++11 -DELPP_NO_DEFAULT_LOG_FILE $ ./main $ ls easylogging++.h log log.conf logs main test.cpp $ ls log debug_2018060800.log error_2018060800.log fatal_2018060800.log info_2018060800.log warn_2018060800.log

能够看到,运行新的代码以后,目录下多了一个文件夹log,log文件下面就是咱们自定义的日志文件了。这个效果是否是很是的棒棒呢?debug

另外easylogging++还支持线程安全,只须要编译的时候加上支持线程安全的编译宏——ELPP_THREAD_SAFE进行编译便可。其内部是经过mutex互斥锁的方式,实现的线程安全。这样在多线程环境下,也能够安全的使用easylogging++进行日志记录,确实很是的给力呢。c++11

相关文章
相关标签/搜索