Log4cpp使用

下载地址:https://github.com/ietjxdl/log4cpp

系统环境:windows 7 32

开发环境:vs2010

软件版本:log4cpp-1.1.tar.gz

 

一 编译

解压log4cpp-1.1.tar.gz到D: \log4cpp,解压缩后在目录下找到log4cpp\msvc10文件夹,进入后打开msvc10.sln工程。该工程下有log4cpp的静态库、动态库及相应的一些demo工程,在本文中使用的是静态库,只需要编译log4cppLIB工程即可。在debug模式和release模式下分别编译生成相应的库文件,至此库文件编译完成。

为方便使用把需要的文件拷贝到C:\Program Files\log4cpp(这个文件是新添加的)文件夹下。

1.   拷贝库文件,把log4cppLIB工程生成的库文件:log4cppD.lib、log4cppLIB.lib拷贝至C:\Program Files\log4cpp\lib文件夹下;

2.   拷贝头文件,把该库用到的头文件拷贝到C:\Program Files\log4cpp\include文件夹下,库中头文件在D: \log4cpp\include下。

二 配置环境变量

为了代码的可在不同的电脑进行编译,避免引用问题,建议使用环境变量对文件引用路径进行配置。

> 打开系统的环境变量中添加:

变量名:LOG4CPP_ROOT

变量值:C:\Program Files\log4cpp

> 在系统的path环境变量的变量值中添加

变量值:;%LOG4CPP_ROOT%\lib

重启电脑配置的环境变量生效。

三 使用demo

eclipse环境进行android开发有Log类提供系统的log方法,用起来非常方便。查看了log4cpp有定义相关的宏LOG4CPP_DEBUG、LOG4CPP_ERROR等,在使用上没有Log类这么方便,因此打算仿照Log对log4cpp进行再次封装。

class LogUtil

{

public:

    LogUtil() {}

    ~LogUtil() {}

    static inline int init(stringinitFileName = "./log4cpp.nt.property") {

        try {

            log4cpp::PropertyConfigurator::configure(initFileName);

        } catch(log4cpp::ConfigureFailure&f) {

            std::cout <<"Configure Problem " << f.what() << std::endl;

            return -1;

        }

        return 0;

    }

 

    static inline void close() {

        log4cpp::Category::shutdown();

    }

 

    /// debug

    static inline void d(stringTag, stringmsg) {

       log4cpp::Category&sub1 = log4cpp::Category::getInstance(Tag);// GetCategory(Tag);

        LOG4CPP_DEBUG(sub1,msg);

    }

 

    /// debug

    static inline void D(stringTag, stringmsg) {

        d(Tag,msg);

}

}

调用方法:

         LogUtil::init();

         LogUtil::d("sub1","testststststtstst");

         LogUtil::d("sub1","teststst55555ststtstst");

         LogUtil::d("sub2","teststststdfasttstst");

         LogUtil::close();

……

 

补充:

1.         包含目录

2.         包含使用库

该domo工程共享路径为:http://download.youkuaiyun.com/detail/xinhuo11/6209637

### LOG4CPP 使用示例 #### ini 配置文件格式说明 `log4cpp` 的配置可以通过 `.ini` 文件完成,支持灵活的日志输出设置。以下是典型的 `log4cpp.properties` 配置文件结构: ```properties # Root category configuration log4cpp.rootCategory=DEBUG, FILEAPPENDER, CONSOLE # FileAppender settings log4cpp.appender.FILEAPPENDER=log4cpp::FileAppender log4cpp.appender.FILEAPPENDER.File=app.log log4cpp.appender.FILEAPPENDER.Append=true log4cpp.appender.FILEAPPENDER.layout=log4cpp::PatternLayout log4cpp.appender.FILEAPPENDER.layout.ConversionPattern=%d [%p] %m%n # Console output settings (optional) log4cpp.appender.CONSOLE=log4cpp::OstreamAppender log4cpp.appender.CONSOLE.Stream=std::cout log4cpp.appender.CONSOLE.layout=log4cpp::PatternLayout log4cpp.appender.CONSOLE.layout.ConversionPattern=%d [%p] %m%n ``` 此部分定义了日志的全局类别(root category),并指定了两个附加器(appenders):一个是用于写入文件的 `FILEAPPENDER`,另一个是控制台输出的 `CONSOLE`[^1]。 --- #### 封装 log4cpp 日志接口 CLogRecord (.h、.cpp) 为了便于在大型项目中使用 `log4cpp`,可以将其封装成一个简单的类 `CLogRecord`。以下是一个可能的设计方案: ##### **CLogRecord.h** ```cpp #ifndef CLOGRECORD_H #define CLOGRECORD_H #include <log4cpp/Category.hh> #include <log4cpp/Appender.hh> #include <log4cpp/FileAppender.hh> #include <log4cpp/OstreamAppender.hh> #include <log4cpp/Priority.hh> class CLogRecord { public: static void init(const char* config_file); static void debug(const std::string& message); static void info(const std::string& message); static void warn(const std::string& message); static void error(const std::string& message); private: static log4cpp::Category& m_rootLogger; }; #endif // CLOGRECORD_H ``` ##### **CLogRecord.cpp** ```cpp #include "CLogRecord.h" #include <iostream> #include <fstream> #include <log4cpp/PropertyConfigurator.hh> log4cpp::Category& CLogRecord::m_rootLogger = *(log4cpp::Category::getRoot()); void CLogRecord::init(const char* config_file) { try { log4cpp::PropertyConfigurator::configure(config_file); // 加载配置文件 } catch (log4cpp::ConfigureFailure& f) { std::cerr << "Configuration failure: " << f.what() << std::endl; } } void CLogRecord::debug(const std::string& message) { m_rootLogger.debug(message.c_str()); } void CLogRecord::info(const std::string& message) { m_rootLogger.info(message.c_str()); } void CLogRecord::warn(const std::string& message) { m_rootLogger.warn(message.c_str()); } void CLogRecord::error(const std::string& message) { m_rootLogger.error(message.c_str()); } ``` 通过这种方式,可以在项目的任何地方轻松调用日志记录函数,而无需重复初始化逻辑[^2]。 --- #### C++ 调用代码示例 以下是如何在一个程序中使用上述封装后的日志模块: ```cpp #include "CLogRecord.h" int main() { CLogRecord::init("log4cpp.properties"); // 初始化日志系统 CLogRecord::debug("This is a DEBUG level message."); CLogRecord::info("This is an INFO level message."); CLogRecord::warn("This is a WARN level message."); CLogRecord::error("This is an ERROR level message."); return 0; } ``` 运行该程序后,日志会按照 `log4cpp.properties` 中的配置被写入指定的目标位置,并显示相应的日志级别和内容[^3]。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值