使用log4cplus 1.2.0版本,C++98 标准下编译可用
#include <log4cplus/logger.h> #include <log4cplus/consoleappender.h> #include <log4cplus/fileappender.h> #include <log4cplus/loggingmacros.h> #include <unistd.h> using namespace log4cplus; using namespace log4cplus::helpers; int main() { /* step 1: Instantiate an appender object */ //SharedObjectPtr<Appender> _append (new ConsoleAppender()); SharedAppenderPtr _append(new RollingFileAppender("Test.log", 5*1024, 5)); _append->setName("append for test"); /* step 2: Instantiate a layout object */ std::string pattern = "%d{%m/%d/%y %H:%M:%S} - %m [%l]%n"; std::auto_ptr<Layout> _layout(new PatternLayout(pattern)); /* step 3: Attach the layout object to the appender */ _append->setLayout( _layout ); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* step 6: Set a priority for the logger */ _logger.setLogLevel(ALL_LOG_LEVEL); /* log activity */ for(int i = 0; i < 100000; i++ ){ LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message..."); LOG4CPLUS_WARN(_logger, "This is the SECOND log message..."); } return 0; }
CMakeLists.txt
cmake_minimum_required (VERSION 2.8) project(logtest) aux_source_directory(. DIR_SRCS) include_directories("/home/zm/log4cplus/include") link_directories("/home/zm/log4cplus/lib") #add_compile_options(-std=gnu++11) add_executable(test ${DIR_SRCS} ) target_link_libraries(test liblog4cplus.so)