c++开源日志库log4cplus

2024-02-22 04:32
文章标签 c++ 日志 开源 log4cplus

本文主要是介绍c++开源日志库log4cplus,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 简介
log4cplus是C++编写的开源的日志系统, The purpose of this project is to port the excellent Log for Java (log4j)logging library to C++
log4cplus具有灵活、强大、使用简单、多线程安全的特点,实在是杂牌军、游击队的福音。

2. 安装使用(Linux)
log4cplus安装使用非常简单,从其官网:http://log4cplus.sourceforge.net/ 下载最新版本
运行:
tar xvzf log4cplus-x.x.x.tar.gz
cd log4cplus-x.x.x
./configure --prefix =/where/to/install
make
make install
在安装目录下生成include和lib两个文件夹,分别为头文件和库文件
使用:
g++ -o server   /mnt/hgfs/work_vm/project/work_project/server/obj/main.o    -L../..//third/log4cplus/lib/ -L../..//third/boost/lib/ -llog4cplus -lpthread -I/mnt/hgfs/work_vm/project/work_project/server/include  -I../..//third/log4cplus/include/-I../..//third/boost/include/
编译参数:
-L../..//third/log4cplus/lib/
-llog4cplus
-lpthread
-I../..//third/log4cplus/include/

3. 使用
log4cplus主要包括layout、appender、loglevel等内容,可以参考:
http://masterdog.bokee.com/153892.html
写的非常nice

4. 包装
logcplus包装下用起来非常方便,以下是我的包装,供参考
Log.h
[cpp] view plain copy
  1. // Log.h: interface for the Log class.  
  2. //  
  3. //  
  4.   
  5. #if !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)  
  6. #define AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_  
  7.   
  8. #include "log4cplus/loglevel.h"  
  9. #include "log4cplus/ndc.h"   
  10. #include "log4cplus/logger.h"  
  11. #include "log4cplus/configurator.h"  
  12. #include "iomanip"  
  13. #include "log4cplus/fileappender.h"  
  14. #include "log4cplus/layout.h"  
  15.   
  16. #include "const.h"  
  17. #include "common.h"  
  18. #include "Main_config.h"  
  19.   
  20. using namespace log4cplus;  
  21. using namespace log4cplus::helpers;  
  22.   
  23. //日志封装  
  24. #define TRACE(p) LOG4CPLUS_TRACE(Log::_logger, p)  
  25. #define DEBUG(p) LOG4CPLUS_DEBUG(Log::_logger, p)  
  26. #define NOTICE(p) LOG4CPLUS_INFO(Log::_logger, p)  
  27. #define WARNING(p) LOG4CPLUS_WARN(Log::_logger, p)  
  28. #define FATAL(p) LOG4CPLUS_ERROR(Log::_logger, p)  
  29.   
  30. // 日志控制类,全局共用一个日志  
  31. class Log  
  32. {  
  33. public:  
  34.     // 打开日志  
  35.     bool open_log();  
  36.   
  37.     // 获得日志实例  
  38.     static Log& instance();  
  39.       
  40.     static Logger _logger;  
  41.   
  42. private:  
  43.     Log();  
  44.   
  45.     virtual ~Log();  
  46.   
  47.     //log文件路径及名称  
  48.     char _log_path[PATH_SIZE];  
  49.     char _log_name[PATH_SIZE];  
  50. };  
  51.   
  52. #endif // !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)  

Log.cpp:
[cpp] view plain copy
  1. // Log.cpp: implementation of the Log class.  
  2. //  
  3. //  
  4.   
  5. #include "Log.h"  
  6.   
  7. //  
  8. // Construction/Destruction  
  9. //  
  10.   
  11. Logger Log::_logger = log4cplus::Logger::getInstance("main_log");  
  12.   
  13. Log::Log()  
  14. {  
  15.     snprintf(_log_path, sizeof(_log_path), "%s""../log");  
  16.     snprintf(_log_name, sizeof(_log_name), "%s/%s.%s", _log_path, execname, "log");  
  17. }  
  18.   
  19. Log::~Log()  
  20. {  
  21. }  
  22.   
  23. Log& Log::instance()  
  24. {  
  25.     static Log log;  
  26.     return log;  
  27. }  
  28.   
  29. bool Log::open_log()  
  30. {  
  31.       
  32.     int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0);    
  33.   
  34.     /* step 1: Instantiate an appender object */  
  35.     SharedAppenderPtr _append(new FileAppender(_log_name));  
  36.     _append->setName("file log test");  
  37.   
  38.     /* step 2: Instantiate a layout object */  
  39.     std::string pattern = "[%p] [%d{%m/%d/%y %H:%M:%S}] [%t] - %m %n";  
  40.     std::auto_ptr<Layout> _layout(new PatternLayout(pattern));  
  41.   
  42.     /* step 3: Attach the layout object to the appender */  
  43.     _append->setLayout(_layout);  
  44.   
  45.     /* step 4: Instantiate a logger object */  
  46.   
  47.     /* step 5: Attach the appender object to the logger  */  
  48.     Log::_logger.addAppender(_append);  
  49.   
  50.     /* step 6: Set a priority for the logger  */  
  51.     Log::_logger.setLogLevel(Log_level);  
  52.   
  53.     return true;  
  54. }  
int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0); 

Main_config是我自己包装的一个配置类(参考:http://blog.csdn.NET/yfkiss/article/details/6802451),通过读取配置设置log level。

使用:
#inlucde "Log.h"
程序初始化的时候:
    // 打开日志
    if (!Log::instance().open_log())
    {
        std::cout << "Log::open_log() failed" << std::endl;
        return false;
    }
然后使用NOTICE、FATAL等就可以打印日志到文件。

[cpp] view plain copy
  1. #include "Log.h"  
  2. ....  
  3.     // 打开日志  
  4.     if (!Log::instance().open_log())  
  5.     {  
  6.         std::cout << "Log::open_log() failed" << std::endl;  
  7.         return false;  
  8.     }  
  9. .....  
  10. NOTICE("Server init succ");  
  11. FATAL("Server run failed"); 

这篇关于c++开源日志库log4cplus的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/734130

相关文章

MySQL 打开binlog日志的方法及注意事项

《MySQL打开binlog日志的方法及注意事项》本文给大家介绍MySQL打开binlog日志的方法及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录一、默认状态二、如何检查 binlog 状态三、如何开启 binlog3.1 临时开启(重启后失效)

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中RAII资源获取即初始化

《C++中RAII资源获取即初始化》RAII通过构造/析构自动管理资源生命周期,确保安全释放,本文就来介绍一下C++中的RAII技术及其应用,具有一定的参考价值,感兴趣的可以了解一下... 目录一、核心原理与机制二、标准库中的RAII实现三、自定义RAII类设计原则四、常见应用场景1. 内存管理2. 文件操

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

C++作用域和标识符查找规则详解

《C++作用域和标识符查找规则详解》在C++中,作用域(Scope)和标识符查找(IdentifierLookup)是理解代码行为的重要概念,本文将详细介绍这些规则,并通过实例来说明它们的工作原理,需... 目录作用域标识符查找规则1. 普通查找(Ordinary Lookup)2. 限定查找(Qualif

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示