log4cxx配合boost::format

本文对比了log4cxx和log4cpp这两个日志记录库的特点与区别,包括配置方式、性能表现及安全性等方面,并探讨了两者在实际项目中的适用场景。
log4cpp用了多年了, 一直用得很得心应手.
现在做网游服务器, 日志类换成了log4cxx.

log4xx会自动加载工作目录下的log4j.properties, 这一点比较好, 使用更方便了.
log4j.properties应该与log4j的配置文件完全一样吧? 通用性好.

其他方面两个日志类差不多. log4cxx::Logger对应log4cpp::Category.
Appender, Layout都是相同的概念.

自从log4cxx从0.9.7升到0.10, 据说内部架构重大更改, 而且有apache的大名顶着, 好像支持者大增.
而log4cpp成熟较早, 一直以来没什么更新, 日渐衰落.

有一点重大区别, log4cxx不支持如printf格式的变参数日志:
log4cpp::Category::getRoot().info("%s %d", "test", 3);

而log4cxx只能先格式化一个字符串后再记日志.
log4cxx::Logger::getRootLogger()->info("A string");

这样log4cxx的性能会差很多. 当日志等级不需要记日志时, 就应该不要格式化字符串.

log4cxx有一组宏, 可以先判断日志等级, 然后再输出.
LOG4CXX_DEBUG(logger, message)

可以这样调用:
LOG4CXX_DEBUG(logger, "test " << 3);

可能是出于安全的考虑, 故意禁止用户使用格式化串. 因为格式化串无法检测类型.
我以前在使用log4cpp的时候, 总是要多次检查格式化串参数是否匹配.
也碰到过因为日志导致程序崩溃的例子.
因为像警告,错误类的日志几乎是不太可能出现的.
测试时, 可能无法覆盖这类日志代码.
而使用流就可以大胆放心.

但是有大半的使用情况下, 流方式输出会显得冗长, 格式控制也没有printf简洁.
只有简单的输出才会使用流.

幸好有boost::format. 它就是类型安全的printf. log4cxx配合boost::format真是绝配.
例如:
LOG4CXX_DEBUG(logger, format("%1% %2%") % "test" % 3)

想起了好用的MFC CString::Format()和Delphi的Format(),
boost::format虽然用'%'显得怪怪的, 但绝对是更安全的格式化串.

(转载请注明来源于金庆的专栏)





[root@dcvserver03 build]# cmake .. -DCMAKE_BUILD_TYPE=Release /root/py/Sunshine Sunshine Branch: master PROJECT_NAME: Sunshine PROJECT_VERSION: 0.0.0 PROJECT_VERSION_MAJOR: 0 PROJECT_VERSION_MINOR: 0 PROJECT_VERSION_PATCH: 0 CMAKE_PROJECT_VERSION: 0.0.0 CMAKE_PROJECT_VERSION_MAJOR: 0 CMAKE_PROJECT_VERSION_MINOR: 0 CMAKE_PROJECT_VERSION_PATCH: 0 PROJECT_YEAR: 1990 PROJECT_MONTH: 01 PROJECT_DAY: 01 -- Could NOT find Boost (missing: Boost_DIR) -- Boost v1.89.0 package not found in the system. Falling back to FetchContent. -- Boost: Release build, static libraries, MPI OFF, Python OFF, testing OFF -- Boost: libraries included: filesystem;locale;log;program_options;system;asio;crc;format;process;property_tree -- Boost.Charconv: quadmath support ON -- Boost.Context: architecture x86_64, binary format elf, ABI sysv, assembler gas, suffix .S, implementation fcontext -- Found the following ICU libraries: -- data (required): /usr/lib64/libicudata.so -- i18n (required): /usr/lib64/libicui18n.so -- uc (required): /usr/lib64/libicuuc.so -- Found ICU: /usr/include (found suitable version "50.2", minimum required is "4.8.1") -- Boost.Locale: iconv ON, ICU ON, POSIX ON, std ON, winapi OFF -- Found the following ICU libraries: -- data (required): /usr/lib64/libicudata.so -- i18n (required): /usr/lib64/libicui18n.so -- uc (required): /usr/lib64/libicuuc.so -- Found ICU: /usr/include (found version "50.2") -- Boost.Thread: threading API is pthread -- Boost include dirs: $<BUILD_INTERFACE:/root/py/Sunshine/build/_deps/boost-src/libs/headers/include> -- Boost libraries: Boost::filesystem;Boost::locale;Boost::log;Boost::program_options;Boost::system;Boost::asio;Boost::crc;Boost::format;Boost::process;Boost::property_tree CMake Error at cmake/dependencies/common.cmake:10 (add_subdirectory): add_subdirectory given source "/root/py/Sunshine/third-party/moonlight-common-c/enet" which is not an existing directory. Call Stack (most recent call first): CMakeLists.txt:61 (include) CMake Error at cmake/dependencies/common.cmake:13 (add_subdirectory): The source directory /root/py/Sunshine/third-party/Simple-Web-Server does not contain a CMakeLists.txt file. Call Stack (most recent call first): CMakeLists.txt:61 (include) CMake Error at cmake/dependencies/common.cmake:16 (add_subdirectory): The source directory /root/py/Sunshine/third-party/libdisplaydevice does not contain a CMakeLists.txt file. Call Stack (most recent call first): CMakeLists.txt:61 (include) -- nlohmann_json v3.11.x package not found in the system. Falling back to FetchContent. -- Using the multi-header code from /root/py/Sunshine/build/_deps/json-src/include/ -- Checking for module 'miniupnpc' -- No package 'miniupnpc' found CMake Error at /usr/local/share/cmake-3.28/Modules/FindPkgConfig.cmake:619 (message): The following required packages were not found: - miniupnpc Call Stack (most recent call first): /usr/local/share/cmake-3.28/Modules/FindPkgConfig.cmake:841 (_pkg_check_modules_internal) cmake/dependencies/common.cmake:26 (pkg_check_modules) CMakeLists.txt:61 (include) -- Configuring incomplete, errors occurred! [root@dcvserver03 build]# make -j$(nproc) make: *** No targets specified and no makefile found. Stop. [root@dcvserver03 build]# sudo make install make: *** No rule to make target `install'. Stop.
最新发布
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值