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;
}
相关推荐
IT女孩儿12 分钟前
JavaScript--WebAPI查缺补漏(二)
开发语言·前端·javascript·html·ecmascript
m0_7482389213 分钟前
webgis入门实战案例——智慧校园
开发语言·ios·swift
悲伤小伞18 分钟前
C++_数据结构_详解二叉搜索树
c语言·数据结构·c++·笔记·算法
Clockwiseee26 分钟前
PHP伪协议总结
android·开发语言·php
小灰灰搞电子28 分钟前
Qt实现Android的图案密码(图形解锁)源码分享
开发语言·qt
m0_675988231 小时前
Leetcode3218. 切蛋糕的最小总开销 I
c++·算法·leetcode·职场和发展
吴冰_hogan1 小时前
JVM(Java虚拟机)的组成部分详解
java·开发语言·jvm
白宇横流学长2 小时前
基于java出租车计价器设计与实现【源码+文档+部署讲解】
java·开发语言
数据小爬虫@4 小时前
Java爬虫实战:深度解析Lazada商品详情
java·开发语言
songroom4 小时前
Rust: offset祼指针操作
开发语言·算法·rust