log4cplus c++开源日志系统
1. 简介
log4cplus是C++编写的开源的日志系统,The purpose of this project is to port the excellentLog 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
- // Log.h: interface for the Log class.
- //
- //
- #if !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)
- #define AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_
- #include "log4cplus/loglevel.h"
- #include "log4cplus/ndc.h"
- #include "log4cplus/logger.h"
- #include "log4cplus/configurator.h"
- #include "iomanip"
- #include "log4cplus/fileappender.h"
- #include "log4cplus/layout.h"
- #include "const.h"
- #include "common.h"
- #include "Main_config.h"
- using namespace log4cplus;
- using namespace log4cplus::helpers;
- //日志封装
- #define TRACE(p) LOG4CPLUS_TRACE(Log::_logger, p)
- #define DEBUG(p) LOG4CPLUS_DEBUG(Log::_logger, p)
- #define NOTICE(p) LOG4CPLUS_INFO(Log::_logger, p)
- #define WARNING(p) LOG4CPLUS_WARN(Log::_logger, p)
- #define FATAL(p) LOG4CPLUS_ERROR(Log::_logger, p)
- // 日志控制类,全局共用一个日志
- class Log
- {
- public:
- // 打开日志
- bool open_log();
- // 获得日志实例
- static Log& instance();
- static Logger _logger;
- private:
- Log();
- virtual ~Log();
- //log文件路径及名称
- char _log_path[PATH_SIZE];
- char _log_name[PATH_SIZE];
- };
- #endif // !defined(AFX_LOG_H__B87F71E3_FFAE_4CFA_A528_3F4F2FF7D69E__INCLUDED_)
Log.cpp:
- // Log.cpp: implementation of the Log class.
- //
- //
- #include "Log.h"
- //
- // Construction/Destruction
- //
- Logger Log::_logger = log4cplus::Logger::getInstance("main_log");
- Log::Log()
- {
- snprintf(_log_path, sizeof(_log_path), "%s", "../log");
- snprintf(_log_name, sizeof(_log_name), "%s/%s.%s", _log_path, execname, "log");
- }
- Log::~Log()
- {
- }
- Log& Log::instance()
- {
- static Log log;
- return log;
- }
- bool Log::open_log()
- {
- int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0);
- /* step 1: Instantiate an appender object */
- SharedAppenderPtr _append(new FileAppender(_log_name));
- _append->setName("file log test");
- /* step 2: Instantiate a layout object */
- std::string pattern = "[%p] [%d{%m/%d/%y %H:%M:%S}] [%t] - %m %n";
- std::auto_ptr<Layout> _layout(new PatternLayout(pattern));
- /* step 3: Attach the layout object to the appender */
- _append->setLayout(_layout);
- /* step 4: Instantiate a logger object */
- /* step 5: Attach the appender object to the logger */
- Log::_logger.addAppender(_append);
- /* step 6: Set a priority for the logger */
- Log::_logger.setLogLevel(Log_level);
- return true;
- }
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等就可以打印日志到文件。
- #include "Log.h"
- ....
- // 打开日志
- if (!Log::instance().open_log())
- {
- std::cout << "Log::open_log() failed" << std::endl;
- return false;
- }
- .....
- NOTICE("Server init succ");
- FATAL("Server run failed");
转载于:https://www.cnblogs.com/chunlinge/p/3435537.html
log4cplus c++开源日志系统相关推荐
- [转]开源日志系统比较
(注:时间有限,整篇文章写得比较粗糙,如果想进一步探讨,可给我发email/msn:clseer@gmail.com,或者加入QQ讨论群:41172282) 1. 背景介绍 许多公司的平台每天会产生大 ...
- ELK搭建开源日志系统(window版本)—图文详细
ELK搭建开源日志系统(window版本)-图文详细 日志对于排查错误非常重要,使用linux命令awk sed grep find等命令查询日志非常麻烦,而且很难做数据分析,使用免费开源的ELK可以 ...
- 还在用笨重的 ELK?这个轻量级开源日志系统真香!
最近,在对公司容器云的日志方案进行设计的时候,发现主流的ELK或者EFK比较重,再加上现阶段对于ES复杂的搜索功能很多都用不上最终选择了Grafana开源的Loki日志系统,下面介绍下Loki的背景. ...
- 开源日志系统比较:scribe、chukwa、kafka、flume
1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...
- 大数据开源日志系统比较:scribe、chukwa、kafka、flume
1. 背景介绍 许多公司的平台每天会产生大量的日志(一般为流式数据,如,搜索引擎的pv,查询等),处理这些日志需要特定的日志系统,一般而言,这些系统需要具有以下特征: (1) 构建应用系统和分析系统的 ...
- 开源日志系统log4cplus(三)
本文介绍了三种控制输出格式的布局管理器的概念和使用情况,通过掌握这些知识,可以更有效地控制log系统输出尽可能贴近你需求的信息来. ### 如何控制输出消息的格式 ### 前面已经讲过,log4cpl ...
- 开源日志系统比较:scribe,chukwa,kafka,flume
2019独角兽企业重金招聘Python工程师标准>>> 百度文库上看到一篇文章,虽然有些老,有参考意义,记录在此: http://wenku.baidu.com/link?url=6 ...
- C++系统日志库精选:深入剖析glog与log4cplus,轻松搭建高效日志系统
目录标题 引言 日志系统的重要性 glog与log4cplus简介与应用场景 C++日志系统库的选择依据 glog基础知识 glog库的关键功能 glog库的安装与使用 glog库的基本日志功能与级别 ...
- 开源日志库log4cplus+VS2008使用整理
一.简介 log4cplus是C++编写的开源的日志系统,功能非常全面.本文介绍如何在Windows+VS2008中使用该日志库. 二.下载 可从网站[url]http://log4cplu ...
- 初次搭建Grafana 开源的Loki 日志系统
初次搭建Grafana 开源的Loki 日志系统 192.168.1.11 grafana + promtail + nginx 192.168.1.12 loki promtail 采集nginx日 ...
最新文章
- 【多线程】1.条件变量--std::condition_variable
- 查看oracle 的数据文件的版本信息
- 1019. 数字黑洞 (20)
- php超市管理系统论文,超市管理系统的设计与实现
- 超级玛丽游戏 C++简单实现
- TensorFlow零基础入门指南——计算模型、数据模型、运行模型!
- java indexof 通配符,字符串与含有通配符‘*’的字符串匹配(非正则表达式)
- 我提交的一个内核补丁—CFS的child-runs-first
- matplotlib 显示批量图片_matplotlib入门
- python拖拽获取文件路径_求助tkinter模块如何获取拖拽文件的内容
- nginx+redis 实现 jsp页面缓存,提升系统吞吐率
- new hashmap 初始大小_你们要的HashMap工作原理!它来了!!!
- create-react-app创建的项目npm run build之后静态文件找不到
- 求连续数组子序咧的最大和
- 动态规划实战1-leetcode 983.Minimum Cost For Tickets
- python菜鸟编程-Python 基础教程 | 菜鸟教程
- 显微镜下的中国--读《显微镜下的大明》有感
- Screw导出数据库表信息
- sqlite3:sqlite3_column 函数
- 因一个 Bug,谷歌、GitHub、亚马逊等网站全球大范围宕机!