log4cpp配置文件log4j.properties log4cpp.properties

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;
}
相关推荐
stm 学习ing3 分钟前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
湫ccc1 小时前
《Python基础》之字符串格式化输出
开发语言·python
mqiqe2 小时前
Python MySQL通过Binlog 获取变更记录 恢复数据
开发语言·python·mysql
AttackingLin2 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
n***85942 小时前
嵌入式 UI 开发的开源项目推荐
windows·开源·开源软件
Ysjt | 深2 小时前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++
ephemerals__2 小时前
【c++丨STL】list模拟实现(附源码)
开发语言·c++·list
码农飞飞2 小时前
深入理解Rust的模式匹配
开发语言·后端·rust·模式匹配·解构·结构体和枚举