项目日志 Log4cpp

项目日志 Log4cpp

http://log4cpp.sourceforge.net/

运用程序更方便的记录日志

Linux中执行

wget 

https://nchc.dl.sourceforge.net/project/log4cpp/log4cpp-1.1.x%20%28new%29/log4cpp-1.1/log4cpp-1.1.3.tar.gz

tar xzvf log4cpp-1.1.3.tar.gz//解压命令

cd log4cpp-1.1.3

./configure -prefix --with-omnithreads --with-pthreads  =绝对路径//安装到的路径

make

make install -prefix=绝对路径//头文件所在的路径

1.包含头文件

#include <log4cpp/Category.hh>

#include <log4cpp/FileAppender.hh>

#include <log4cpp/PatternLayout.hh>

#include <log4cpp/OstreamAppender.hh>

2.初始化日志输出的目的地(appenders)

// 输出到std::cout
log4cpp::Appender *appender = new log4cpp::OstreamAppender("root", &std::cout);
// 输出到log文件
log4cpp::Appender *appender = new log4cpp::FileAppender("root", "test.log");

***\*appender\****有以下这些:
log4cpp::FileAppender // 输出到文件
log4cpp::RollingFileAppender // 输出到回卷文件,即当文件到达某个大小后回卷
log4cpp::OstreamAppender // 输出到一个ostream类
log4cpp::RemoteSyslogAppender // 输出到远程syslog服务器
log4cpp::StringQueueAppender // 内存队列
log4cpp::SyslogAppender // 本地syslog
log4cpp::Win32DebugAppender // 发送到缺省系统调试器
log4cpp::NTEventLogAppender // 发送到win 事件日志

3.设置日志输出格式

log4cpp::PatternLayout *patternLayout = new log4cpp::PatternLayout();
patternLayout->setConversionPattern("%d [%p] - %m%n");
appender->setLayout(patternLayout);

日志输出格式控制有: 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".

4.设置类别输出的(category)和日志优先级(priority)

log4cpp::Category &root = log4cpp::Category::getRoot();
root.setPriority(log4cpp::Priority::NOTICE);
root.addAppender(appender);
日志的级别总共有:NOTSET < DEBUG < INFO < NOTICE < WARN < ERROR < CRIT < ALERT < FATAL = EMERG。
日志级别的意思是低于该级别的日志不会被记录。

5.定义一个宏//做个宏定义指定的地方输出日志

#define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << " " << __LINE__ << ": "

6使用宏定义记录日志

  LOG(DEBUG) << "i am happy.";

  LOG(INFO)  << "oh, you happy, we happy.";

  LOG(NOTICE)<< "please do not contact me. ";

  LOG(WARN)  << "i am very busy now.";

  LOG(ERROR) << "oh, what happed?";

指定得目录配置日志 vi log.conf

#定义Root category的属性
log4cpp.rootCategory=DEBUG, RootLog

#定义RootLog属性
log4cpp.appender.RootLog=RollingFileAppender
log4cpp.appender.RootLog.layout=PatternLayout
#log4cpp.appender.RootLog.layout.ConversionPattern=%d{% m-%d %H:%M:%S %l} [%t][%p]%m%n
log4cpp.appender.RootLog.layout.ConversionPattern=%d{
    
    \%\m-%d %H:%M:%S %l} [%t][%p]%m%n
log4cpp.appender.RootLog.fileName=/var/log/qiniu_bike.log
log4cpp.appender.RootLog.maxFileSize=268435456 #256MB
log4cpp.appender.RootLog.fileNamePattern=qiniu_bike_%i.log
log4cpp.appender.RootLog.maxBackupIndex=256

保存

创建单例

自定义类需要有读取日志配置文件得方法

还需创建单例static 需有自己得静态成员

还有写入到日志得方法 并定义日志得宏
在这里插入图片描述
在这里插入图片描述

将log4库文件添加到编译器

在这里插入图片描述
在这里插入图片描述

若出现问题

在这里插入图片描述

执行cmake .

执行make

就可以使用了

包含自定义得头文件

如果初始化日志路径失败了
在这里插入图片描述

然后就可以调用宏来输出错误比如在这里插入图片描述

当前项目目录 运用cmake编译

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45743563/article/details/111559610