我试图将我的应用程序容器化,但是我在log4cplus的工作上遇到了麻烦。最重要的是,当在主机上运行时,日志可以工作,但是当我从长时间运行的循环中日志时,在容器中就不行了。看起来好像某个地方的缓冲区没有被刷新。下面是一个最小的例子。
此外,删除长寿命循环也解决了这个问题,大概log4cplus在关闭之前会最后一次刷新。延长休眠持续时间似乎没有帮助。
main.cpp
# include <iostream>
# include <unistd.h>
# include <log4cplus/logger.h>
# include <log4cplus/loggingmacros.h>
# include <log4cplus/configurator.h>
# include <log4cplus/initializer.h>
int main(int argc, char**argv)
{
std::cout << "Sanity Check" << std::endl;
auto log4cplus = ::log4cplus::Initializer();
std::string logconfig("log4cplus_configure.ini");
::log4cplus::PropertyConfigurator::doConfigure(logconfig);
auto logger = ::log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("main"));
while (true) {
LOG4CPLUS_ERROR(logger, "Sleeping...");
// std::cout << "cout sleep..." << std::endl; // uncomment to get log messages working
sleep(1);
}
return 0;
}
log4cplus_configure.ini
log4cplus.rootLogger=INFO, MyConsoleAppender
log4cplus.appender.MyConsoleAppender=log4cplus::ConsoleAppender
log4cplus.appender.MyConsoleAppender.layout=log4cplus::PatternLayout
log4cplus.appender.MyConsoleAppender.layout.ConversionPattern=[%-5p][%d] %m%n
停靠文件
FROM rockylinux:latest
RUN dnf install -y boost-system
COPY ./build/sandbox /
COPY ./log4cplus_configure.ini /
CMD ["/sandbox"]
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
set (CMAKE_CXX_STANDARD 17)
# Project executable and library
add_executable(sandbox main.cpp)
target_link_libraries(sandbox
PUBLIC liblog4cplus.a
PUBLIC pthread
PUBLIC boost_system
)
1条答案
按热度按时间hc2pp10m1#
不知道为什么,但是把
log4cplus.appender.MyConsoleAppender.ImmediateFlush=true
加到log4cplus_configure.ini
解决了我的问题。