本篇内容介绍了“C++日志库log4cplus如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
编译&&安装
下载完成在ubuntn中解压&&编译
(使用默认参数和路径)
1、sudo ./configure
2、sudo make
3、sudo make install
(安装完成后头文件路径默认在/usr/loca/include,库文件路径在/usr/local/lib)
4、把库文件路径加入到/etc//etc/ld.so.conf文件中然后执行sudo ldconfig
编写demo
测试需要四个文件,是 MyLogger.h , main.cpp , logconfig.property , CMakeLists.txt (用cmake编译时可用) ,文件内容如下,放入同一个文件夹进行编译
MyLogger.h
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/layout.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/helpers/stringhelper.h>
#define MY_LOG_FILE_PATH "logconfig.property"
class MyLogger
{
private:
MyLogger()
{
log4cplus::initialize();
log4cplus::PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(MY_LOG_FILE_PATH));
m_rootLog = log4cplus::Logger::getRoot();
m_loggerYW = log4cplus::Logger::getInstance("loggerYW");
m_loggerWJ = log4cplus::Logger::getInstance("loggerWJ");
m_loggerUI = log4cplus::Logger::getInstance("loggerUI");
}
~MyLogger()
{
if (m_logger)
{
delete m_logger;
}
}
static MyLogger* m_logger;
public:
static MyLogger * getInstance()
{
if (m_logger == NULL)
{
m_logger = new MyLogger();
}
return m_logger;
}
log4cplus::Logger m_rootLog;
log4cplus::Logger m_loggerYW;
log4cplus::Logger m_loggerWJ;
log4cplus::Logger m_loggerUI;
};
MyLogger* MyLogger::m_logger = NULL;
main.cpp
#include "MyLogger.h"
int main(int argc, char *argv[])
{
MyLogger * myLoger = NULL;
myLoger = MyLogger::getInstance();
LOG4CPLUS_FATAL(myLoger->m_loggerYW, "DeleteService failed,errCode=[" << 2 << "]");
LOG4CPLUS_DEBUG(myLoger->m_loggerWJ, " Service is removed");
LOG4CPLUS_DEBUG(myLoger->m_loggerUI, " Service ");
return 0;
}
logconfig.property
log4cplus.rootLogger=DEBUG,rootLogger
log4cplus.appender.rootLogger=log4cplus::RollingFileAppender
#log4cplus.appender.rootLogger=log4cplus::ConsoleAppender
log4cplus.appender.rootLogger.MaxFileSize=100MB
log4cplus.appender.rootLogger.MaxBackupIndex=2
log4cplus.appender.rootLogger.File=rootLogger.log
log4cplus.appender.rootLogger.layout=log4cplus::PatternLayout
log4cplus.appender.rootLogger.layout.ConversionPattern=%D:%d{%Q}|%p|%t|%l|%m|%n
log4cplus.appender.rootLogger.filters.1=log4cplus::spi::LogLevelRangeFilter
log4cplus.appender.rootLogger.filters.1.LogLevelMin=TRACE
log4cplus.appender.rootLogger.filters.1.LogLevelMax=FATAL
log4cplus.appender.rootLogger.filters.1.AcceptOnMatch=true
log4cplus.appender.rootLogger.filters.2=log4cplus::spi::DenyAllFilter
log4cplus.logger.loggerYW=DEBUG,loggerYW
log4cplus.appender.loggerYW=log4cplus::RollingFileAppender
log4cplus.appender.loggerYW.File=loggerYW.log
log4cplus.appender.loggerYW.layout=log4cplus::PatternLayout
log4cplus.appender.loggerYW.layout.ConversionPattern=%D:%d{%Q}|%p|%t|%l|%m|%n
log4cplus.appender.loggerYW.filters.1=log4cplus::spi::LogLevelRangeFilter
log4cplus.appender.loggerYW.filters.1.LogLevelMin=TRACE
log4cplus.appender.loggerYW.filters.1.LogLevelMax=FATAL
log4cplus.appender.loggerYW.filters.1.AcceptOnMatch=true
log4cplus.appender.loggerYW.filters.2=log4cplus::spi::DenyAllFilter
log4cplus.appender.loggerYW.MaxFileSize=100MB
log4cplus.appender.loggerYW.MaxBackupIndex=2
log4cplus.additivity.loggerYW=false
log4cplus.logger.loggerWJ=DEBUG,loggerWJ
log4cplus.appender.loggerWJ=log4cplus::RollingFileAppender
log4cplus.appender.loggerWJ.File=loggerWJ.log
log4cplus.appender.loggerWJ.layout=log4cplus::PatternLayout
log4cplus.appender.loggerWJ.layout.ConversionPattern=%D:%d{%Q}|%p|%t|%l|%m|%n
log4cplus.appender.loggerWJ.filters.1=log4cplus::spi::LogLevelRangeFilter
log4cplus.appender.loggerWJ.filters.1.LogLevelMin=TRACE
log4cplus.appender.loggerWJ.filters.1.LogLevelMax=FATAL
log4cplus.appender.loggerWJ.filters.1.AcceptOnMatch=true
log4cplus.appender.loggerWJ.filters.2=log4cplus::spi::DenyAllFilter
log4cplus.appender.loggerWJ.MaxFileSize=100MB
log4cplus.appender.loggerWJ.MaxBackupIndex=2
log4cplus.additivity.loggerWJ=false
log4cplus.logger.loggerUI=DEBUG,loggerUI
log4cplus.appender.loggerUI=log4cplus::RollingFileAppender
log4cplus.appender.loggerUI.File=loggerUI.log
log4cplus.appender.loggerUI.layout=log4cplus::PatternLayout
log4cplus.appender.loggerUI.layout.ConversionPattern=%D:%d{%Q}|%p|%t|%l|%m|%n
log4cplus.appender.loggerUI.filters.1=log4cplus::spi::LogLevelRangeFilter
log4cplus.appender.loggerUI.filters.1.LogLevelMin=TRACE
log4cplus.appender.loggerUI.filters.1.LogLevelMax=FATAL
log4cplus.appender.loggerUI.filters.1.AcceptOnMatch=true
log4cplus.appender.loggerUI.filters.2=log4cplus::spi::DenyAllFilter
log4cplus.appender.loggerUI.MaxFileSize=100MB
log4cplus.appender.loggerUI.MaxBackupIndex=2
log4cplus.additivity.loggerUI=false
CMakeLists.txt
# CMake 最低版本号要求
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 项目名
PROJECT(LOG)
# 添加第三方的库
SET(INC_DIR "/usr/local/include/")
SET(LINK_DIR "/usr/local/lib")
INCLUDE_DIRECTORIES(${INC_DIR}) # 设置头文件搜索路径
LINK_DIRECTORIES(${LINK_DIR}) #指定要链接的库文件的路径,一般不需要
# 将第三方的库链接到可执行文件,静态链接
LINK_LIBRARIES(log4cplus)
# 指定源文件
SET(SRC_LIST "main.cpp")
MESSAGE(STATUS "SRC_LIST :" ${SRC_LIST})
# 指定生成目标
ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST})
# 将第三方的库链接到可执行文件,动态链接
#TARGET_LINK_LIBRARIES(${PROJECT_NAME} log4cplus)
# 安装目标文件
SET(PROJECT_INSTALL_PREFIX "/mnt/hgfs/VM_Shared/Teselog4cplus/build/dd")
INSTALL(TARGETS ${PROJECT_NAME}
DESTINATION ${PROJECT_INSTALL_PREFIX}
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
# 安装配置文件
SET(CONFIG_INSTALL_PREFIX "/mnt/hgfs/VM_Shared/Teselog4cplus/build")
INSTALL(FILES logconfig.property
DESTINATION ${CONFIG_INSTALL_PREFIX})
用cmake进行编译
mkdir workspace //创建工作目录
cd workspace && 创建4个新文件
mkdir build/install -p
cd build
cmake …/
make
make install
以上就是C++日志库log4cplus如何使用的详细内容,更多关于C++日志库log4cplus如何使用的资料请关注九品源码其它相关文章!