cpp
log4cpp.rootCategory=DEBUG, Console
log4cpp.category.Debug=DEBUG, Debug
log4cpp.category.Info=DEBUG, Info
log4cpp.category.Data=DEBUG, Data
#ConsoleAppender target
log4cpp.appender.Console=ConsoleAppender
log4cpp.appender.Console.layout=PatternLayout
log4cpp.appender.Console.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l}] %m%n
#FileAppender
#RollingFileAppender fileName maxFileSize=10*1024*1024 maxBackupIndex append layout maxBackupIndex 日志文件备份数量
log4cpp.appender.Debug=RollingFileAppender
log4cpp.appender.Debug.name=Debug
log4cpp.appender.Debug.fileName=Log
log4cpp.appender.Debug.maxFileSize=10485760
log4cpp.appender.Debug.maxBackupIndex=1
log4cpp.appender.Debug.append=false
log4cpp.appender.Debug.layout=PatternLayout
log4cpp.appender.Debug.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l}] %m%n
log4cpp.appender.Info=DailyRollingFileAppender
log4cpp.appender.Info.fileName=Info
log4cpp.appender.Info.maxDaysKeep=1
log4cpp.appender.Info.append=false
log4cpp.appender.Info.layout=PatternLayout
log4cpp.appender.Info.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l}] %m%n
#DailyRollingFileAppender fileName maxDaysKeep=30 append=true mode=00644 maxDaysKeep=1100(三年) 100(三月) 最大保留天数 超过的会被删除
log4cpp.appender.Data=DailyRollingFileAppender
log4cpp.appender.Data.name=Data
log4cpp.appender.Data.fileName=Data
log4cpp.appender.Data.backupPattern=%Y-%m-%d
log4cpp.appender.Data.DatePattern=%Y-%m-%d
log4cpp.appender.Data.maxDaysKeep=1
log4cpp.appender.Data.append=false
log4cpp.appender.Data.layout=PatternLayout
log4cpp.appender.Data.layout.ConversionPattern=[%d{%Y-%m-%d %H:%M:%S.%l}] %m%n
使用代码
cpp
#ifndef LOGFACTORY_H
#define LOGFACTORY_H
class LogFactory
{
public:
LogFactory();
static int Init(const char* file);
static int Exit();
static int print_s(const char* _Format, ...);
static int print(const char* file, const char* _Format, ...);
};
#define LOG_DEBUE1(NAME, format, ...) LogFactory::print(NAME,format,##__VA_ARGS__);
#define LOG_DEBUG2(NAME, format, ...) LogFactory::print(NAME,"[%s %s %d]"##format,__FILE__,__FUNCTION__, __LINE__,##__VA_ARGS__);
#endif // LOGFACTORY_H
#include "LogFactory.h"
#include <log4cpp/Category.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/DailyRollingFileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
LogFactory::LogFactory()
{
}
int LogFactory::Init(const char *file)
{
int result = 0;
log4cpp::PropertyConfigurator::configure(file);
return result;
}
int LogFactory::Exit()
{
int result = 0;
log4cpp::Category::shutdown();
return result;
}
int LogFactory::print_s(const char* const _Format, ...)
{
int _Result;
va_list _ArgList;
const int _BufferCount = 1000;
char buffer[_BufferCount] = {0};
va_start(_ArgList, _Format);
_Result = vsnprintf(buffer, _BufferCount, _Format, _ArgList); //_Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
va_end(_ArgList);
log4cpp::Category::getInstance("").debug(buffer);
std::cout<<std::endl;
return _Result;
}
int LogFactory::print(const char *file, const char *_Format, ...)
{
int _Result;
va_list _ArgList;
const int _BufferCount = 1000;
char buffer[_BufferCount] = {0};
va_start(_ArgList, _Format);
_Result = vsnprintf(buffer, _BufferCount, _Format, _ArgList); //_Result = _vfprintf_l(stdout, _Format, NULL, _ArgList);
va_end(_ArgList);
log4cpp::Category::getInstance(file).debug(buffer);
std::cout<<std::endl;
return _Result;
}
cpp
#include <QCoreApplication>
#include "LogFactory.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string appdir = QCoreApplication::applicationDirPath().toStdString().append("/log4j.properties");//应用程序路径
LogFactory::Init(appdir.data());
LOG_DEBUE1("Info", "log4cpp Info");
LOG_DEBUE1("Data", "log4cpp Data");
LogFactory::Exit();
return 0;
}