一、log4cpp的使用
1. 下载log4cpp
log4cpp官方下载地址
2. 安装log4cpp
第一步:解压 tar zxvf log4cpp-1.1.4.tar.gz
第二步:进入log4cpp文件夹并执行 ./configure
tips:如果是ARM架构的CPU可能会失败,如下面这种情况,重新执行
./configure --build=aarch64-unknown-linux-gnu
即可
第三步:执行 make
第四步:执行 make check
第五步:执行 make install
3. 查看是否安装成功
安装成功后,在/usr/local/include
路径下会多一个log4cpp的文件夹
4. 更新动态库路径缓存
!!! 不更新缓存,会导致编译时找不到库文件
;请执行命令sudo ldconfig
5. 编写测试代码
#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>using namespace std;
using namespace log4cpp;int main(void) {OstreamAppender *osAppender = new OstreamAppender("console", &cout);osAppender->setLayout(new BasicLayout());Category &root = Category::getRoot();root.addAppender(osAppender);root.setPriority(Priority::DEBUG);root.error("this is a error");root.warn("this is a warn");root.shutdown();return 0;
}
6. 编译代码并运行测试
g++ test.cc -llog4cpp
二、自定义封装的MyLogger类
#include <iostream>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
#include <log4cpp/Category.hh>using namespace std;
using namespace log4cpp;// 定义LogWarn宏函数
#define LogWarn(str) { \MyLogger *log = MyLogger::getInstance(); \log->warn(str); \
}
// 定义LogError宏函数
#define LogError(str) { \MyLogger *log = MyLogger::getInstance(); \log->error(str); \
}
// 定义LogDebug宏函数
#define LogDebug(str) { \MyLogger *log = MyLogger::getInstance(); \log->debug(str); \
}
// 定义LogInfo宏函数
#define LogInfo(str) { \MyLogger *log = MyLogger::getInstance(); \log->info(str); \
}class MyLogger
{
public:static MyLogger *getInstance();static void destroyInstance();void warn(const char *msg);void error(const char *msg);void debug(const char *msg);void info(const char *msg);private:MyLogger();~MyLogger();Category &category;static MyLogger *pInstance;
};
// 初始化静态对象
MyLogger *MyLogger::pInstance = nullptr;
// 无参构造函数
MyLogger::MyLogger() :category(Category::getInstance("MyLogger")) {// 定义输出到命令行的AppenderOstreamAppender *pConmandLineAppender = new OstreamAppender("comandLine", &cout); // 定义输出到回卷文件的AppenderRollingFileAppender *pRollingAppender = new RollingFileAppender("rollingFile", "mylogger.log", 1024, 3);// 定义日志样式PatternLayout *layout = new PatternLayout();layout->setConversionPattern("%d [%p] %m%n");// 绑定输出样式pConmandLineAppender->setLayout(new BasicLayout());pRollingAppender->setLayout(layout); // 设置日志过滤等级 category.setPriority(Priority::DEBUG); // 设置输出位置category.setAppender(pConmandLineAppender);category.setAppender(pRollingAppender);
}
/*** 获取单例对象
*/
MyLogger *MyLogger::getInstance() {if (pInstance == nullptr) {pInstance = new MyLogger();}return pInstance;
}
/*** 销毁单例对象
*/
void MyLogger::destroyInstance() {if (pInstance != nullptr) {delete pInstance;pInstance = nullptr;}
}
/*** 析构函数
*/
MyLogger::~MyLogger() {category.shutdown();cout << "~MyLogger()" << endl;
}
/*** warn
*/
void MyLogger::warn(const char *msg) {category.warn("%s,%s,%d: %s", __FILE__, __func__, __LINE__, msg);
}
/*** error
*/
void MyLogger::error(const char *msg) {category.error("%s,%s,%d: %s", __FILE__, __func__, __LINE__, msg);
}
/*** debug
*/
void MyLogger::debug(const char *msg) {category.debug("%s,%s,%d: %s", __FILE__, __func__, __LINE__, msg);
}
/*** info
*/
void MyLogger::info(const char *msg) {category.info("%s,%s,%d: %s", __FILE__, __func__, __LINE__, msg);
}
三、MyLogger的使用示例
方式一:单例对象
void test0()
{MyLogger *log = MyLogger::getInstance();log->warn("The log is warn message");log->error("The log is error message");log->debug("The log is debug message");log->info("The log is info message");MyLogger::destroyInstance();
}
方式二:宏函数
void test1()
{LogWarn("The log is warn message");LogError("The log is error message");LogDebug("The log is debug message");LogInfo("The log is info message");
}
生成效果: