前言
Log4cpp是一个开源的C++类库,它提供了在C++程序中使用日志和跟踪调试的功能。使用log4cpp,能够非常便利地将日志或者跟踪调试信息写入字符流、内存字符串队列、文件、回滚文件、调试器、Windows日志、本地syslog和远程syslogserver中。
1.编译和配置
下载链接:https://github.com/orocos-toolchain/log4cpp
编译请参考:http://t.csdn.cn/56DY1
编译时注意:
1.VS 201X对应的版本号不要搞错
2.注意生成的lib和dll文件的类型,是debug还是release;是x86还是x64。在使用时也要注意别使用错误。这点非常重要。
2.Log4cpp的概念
见链接,我感觉讲的很好
https://www.cnblogs.com/hrhguanli/p/3809023.html
3.快速使用
见链接,代码写的很好,简单易懂。
https://blog.csdn.net/tanhuifang520/article/details/107666090
下面代码是我做的更改:
/**
* @brief log4cpp is library of C++ classes for flexible logging to file,syslog, IDSA and other destinations.
* 官网:http://log4cpp.sourceforge.net/
* 源码下载:https://sourceforge.net/projects/log4cpp/files/
*
* 线程安全
*/
/**
* @brief 需实现以下功能
* 1、日志格式
* 2、输出到console
* 3、输出到file
* 4、配置文件控制日志
*/
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/PropertyConfigurator.hh"
#include "log4cpp/LoggingEvent.hh"
#include <log4cpp/PatternLayout.hh>
//int main()
//{
// // 控制台标准输出
// log4cpp::Appender* appender1 = new log4cpp::OstreamAppender("console", &std::cout);
// appender1->setLayout(new log4cpp::BasicLayout());
//
// // 输出到文件program.log
// log4cpp::Appender* appender2 = new log4cpp::FileAppender("default", "program.log");
// appender2->setLayout(new log4cpp::BasicLayout());
//
// log4cpp::Category& root = log4cpp::Category::getRoot();
// root.setPriority(log4cpp::Priority::WARN);
// root.addAppender(appender1);
//
// log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
// sub1.addAppender(appender2);
//
// // use of functions for logging messages
// root.error("root error");
// root.info("root info");
// sub1.error("sub1 error");
// sub1.warn("sub1 warn");
//
// // printf-style for logging variables
// root.warn("%d + %d == %s ?", 1, 1, "two");
//
// // use of streams for logging messages
// root << log4cpp::Priority::ERROR << "Streamed root error";
// root << log4cpp::Priority::INFO << "Streamed root info";
// sub1 << log4cpp::Priority::ERROR << "Streamed sub1 error";
// sub1 << log4cpp::Priority::WARN << "Streamed sub1 warn";
//
// // or this way:
// root.errorStream() << "Another streamed error";
// return 0;
//}
/**
* @brief 以设置的日志格式输出到标准输出、日志文件
* 日志格式项
PatternLayout supports following set of format characters:
%% - a single percent sign
%c - the category
%d - the date\n Date format: The date format character may be followed by a date format specifier enclosed between braces. For example, %d{%H:%M:%S,%l} or %d{%d %m %Y %H:%M:%S,%l}. If no date format specifier is given then the following format is used: "Wed Jan 02 02:03:55 1980". The date format specifier admits the same syntax as the ANSI C function strftime, with 1 addition. The addition is the specifier %l for milliseconds, padded with zeros to make 3 digits.
%m - the message
%n - the platform specific line separator
%p - the priority
%r - milliseconds since this layout was created.
%R - seconds since Jan 1, 1970
%u - clock ticks since process start
%x - the NDC
%t - thread name
By default, ConversionPattern for PatternLayout is set to "%m%n".
*/
int main()
{
auto getLayout = []()
{
log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
layout->setConversionPattern("%d: %p %c %x: %m%n"); // 可设置自己喜欢的日志格式
//layout->setConversionPattern("%d{%d %b %Y %H:%M:%S} [%p] %c %m%n"); // 可设置自己喜欢的日志格式
return layout;
};
// 控制台标准输出
log4cpp::Appender* consoleAppender = new log4cpp::OstreamAppender("console", &std::cout);
consoleAppender->setLayout(getLayout());
// 日志文件txh.log
log4cpp::Appender* fileAppender = new log4cpp::FileAppender("default", "wang.log");
fileAppender->setLayout(getLayout());
//log4cpp::Category& root = log4cpp::Category::getRoot();
log4cpp::Category& root = log4cpp::Category::getInstance(std::string("wang"));
root.setPriority(log4cpp::Priority::WARN); // 设置日志优先级 FATAL,ALERT,CRIT,ERROR,WARN,NOTICE,INFO,DEBUG
root.addAppender(consoleAppender);
root.addAppender(fileAppender);
while (1)
{
root.error("%s %d times", "nihao", 100);
root.warn("%s %d times", "nihao", 100);
root.info("%s %d times", "nihao", 100); // Priority之后的不会打印
root.debug("%s %d times", "nihao", 100);
Sleep(1000);
}
return 0;
}