1.Log4cpp是什么?
log4cpp是一个C++语言编写的开源日志库,它提供了一套简单易用的API,支持多线程和远程日志记录。log4cpp的设计灵感来源于Java中的log4j,它能够帮助开发人员在应用程序中记录日志,帮助开发人员进行调试和故障排除。
log4cpp支持输出到文件、控制台、syslog和网络套接字等多种日志输出方式,可以按照不同的级别(如debug、info、warning、error和fatal)对日志进行分类管理。它还提供了灵活的配置选项,可以通过配置文件或程序代码来配置日志记录方式和级别。
log4cpp已经被广泛地应用于各种C++应用程序中,包括网络应用、桌面应用、嵌入式系统和游戏开发等领域。
2.Log4cpp的安装
Log4cpp的官网:
http://log4cpp.sourceforge.net/
安装步骤:
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
./configure --prefix=/home/wxncom/shared_bike/third/
make
make install
3.Log4cpp的使用
- step 1 :包含头文件
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
- step 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 事件日志
上文,我们说过日志输出到终端或者文件中实际上是很慢的,会引起IO中断,所以我们可以输出到内存里
S
t
r
i
n
g
Q
u
e
u
e
A
p
p
e
n
d
e
r
StringQueueAppender
StringQueueAppender,然后从
S
t
r
i
n
g
Q
u
e
u
e
A
p
p
e
n
d
e
r
StringQueueAppender
StringQueueAppender输出到其它地方,这样我们的线程执行是比较高效的。
- step 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”.
- step 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。日志级别的意思是低于该级别的日志不会被记录。
- step 5 :定义一个宏
#define LOG(__level) log4cpp::Category::getRoot() << log4cpp::Priority::__level << __FILE__ << " " << __LINE__ << ": "
当然也可以使用Category定义的函数:
/**
* Log a message with the specified priority.
* @param priority The priority of this log message.
* @param stringFormat Format specifier for the string to write
* in the log file.
* @param ... The arguments for stringFormat
**/
virtual void log(Priority::Value priority, const char* stringFormat,
...) throw();
/**
* Log a message with the specified priority.
* @param priority The priority of this log message.
* @param message string to write in the log file
**/
virtual void log(Priority::Value priority,
const std::string& message) throw();
void debug(const char* stringFormat, ...) throw();
void debug(const std::string& message) throw();
void info(const char* stringFormat, ...) throw();
...
- step 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?";
当然我们在使用过程中,可以封装一个单例,oh,单例是什么?! 记得复习一下!
在实际工程上应用,我们是使用日志配置文件去控制日志记录的。接下来让我们先配置一个日志配置文件:
#定义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/shared_bike.log
log4cpp.appender.RootLog.maxFileSize=268435456 #256MB
log4cpp.appender.RootLog.fileNamePattern=shared_bike_%i.log
log4cpp.appender.RootLog.maxBackupIndex=256
那如何使用配置文件定义log呢?
//Logger.h
#ifndef DISTRIBUTED_LOGGER_H_
#define DISTRIBUTED_LOGGER_H_
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <string>
class Logger
{
public:
bool init(const std::string &log_conf_file);
static Logger *instance()
{
return &instance_;
}
log4cpp::Category *GetHandle()
{
return category_;
}
protected:
static Logger instance_;
log4cpp::Category *category_;
};
#define LOG_INFO Logger::instance()->GetHandle()->info
#define LOG_DEBUG Logger::instance()->GetHandle()->debug
#define LOG_ERROR Logger::instance()->GetHandle()->error
#define LOG_WARN Logger::instance()->GetHandle()->warn
#endif
// Logger.cpp
#include "Logger.h"
#include <iostream>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/RemoteSyslogAppender.hh>
#include <log4cpp/PropertyConfigurator.hh>
// 外部的static成员必须声明一下,不声明就会报错!!!
Logger Logger::instance_;
bool Logger::init(const std::string &log_conf_file)
{
try
{
log4cpp::PropertyConfigurator::configure(log_conf_file);
}
catch (log4cpp::ConfigureFailure &f)
{
std::cerr << " load log config file " << log_conf_file.c_str() << " failed with result : " << f.what() << std::endl;
return false;
}
category_ = &log4cpp::Category::getRoot(); // 输出日志category_
return true;
}