[log4cplus]: 快速搭建分布式日志系统

server/2024/9/24 6:04:21/

关键词: 日志系统>日志系统 、日志分类、自动分文件夹、按时间(月/周/日/小时/分)轮替

一、引言

这里我默认看此文的我的朋友们都已经具备一定的基础,所以,我们本篇不打算讲关于log4cplus的基础内容,文中如果涉及到没有吃透的点,需要朋友们动动自己聪明的脑袋和发财的手指,进一步寻找答案。

如标题所示,本篇我们不是单纯的在应用程序中集成log4cplus,然后按照配置的日志生成规则生成日志文件;再此基础上我们玩点儿高级的(只能说是log4cplus已经提供高级的模块),对,没错,我们要使用log4cplus快速打造一个支持分布式日志系统>日志系统。

这里,我们有如下图所示的简单设计目标:
在这里插入图片描述
多个机台的服务数据,通过tcp 网络发送到日志服务>日志服务器,然后日志服务>日志服务处理日志信息存储到日志数据库或者文件中(本文以存储到文件为例

想想,聪明的你肯定也觉得超级简单,一个C/S的框架足以搞定一切;只需要一点时间编写一个客户端,编写一个高性能的服务端,编写同一的日志接口;最多再额外的添加点扩展功能,譬如脚本的拓展,配置文件的拓展(脚本也好,配置文件也好,都是小case,自己写点解析的接口)… 诸多的小问题放在一起,就变成了时间成本。
所以,本文将基于log4plus,教你如何快速打造一个高性能、可定制化的分布式日志系统>日志系统。

二、日志服务>日志服务端

这里不多说,无非是创建一个tcp的服务端,循环监听和读日志信息,数据分发处理(写文件)。


#include <cstdlib>
#include <list>
#include <iostream>
#include <log4cplus/configurator.h>
#include <log4cplus/socketappender.h>
#include <log4cplus/helpers/socket.h>
#include <log4cplus/thread/threads.h>
#include <log4cplus/spi/loggingevent.h>
#include <log4cplus/thread/syncprims.h>
#include <log4cplus/log4cplus.h>namespace loggingserver
{typedef std::list<log4cplus::thread::AbstractThreadPtr> ThreadQueueType;class ReaperThread: public log4cplus::thread::AbstractThread
{
public:ReaperThread (log4cplus::thread::Mutex & mtx_,log4cplus::thread::ManualResetEvent & ev_,ThreadQueueType & queue_): mtx (mtx_), ev (ev_), queue (queue_), stop (false){ }virtual~ReaperThread (){ }virtual void run ();void signal_exit ();private:log4cplus::thread::Mutex & mtx;log4cplus::thread::ManualResetEvent & ev;ThreadQueueType & queue;bool stop;
};typedef log4cplus::helpers::SharedObjectPtr<ReaperThread> ReaperThreadPtr;void
ReaperThread::signal_exit ()
{log4cplus::thread::MutexGuard guard (mtx);stop = true;ev.signal ();
}void
ReaperThread::run ()
{ThreadQueueType q;while (true){ev.timed_wait (30 * 1000);{log4cplus::thread::MutexGuard guard (mtx);// Check exit condition as the very first thing.if (stop){std::cout << "Reaper thread is stopping..." << std::endl;return;}ev.reset ();q.swap (queue);}if (! q.empty ()){std::cout << "Reaper thread is reaping " << q.size () << " threads."<< std::endl;for (ThreadQueueType::iterator it = q.begin (), end_it = q.end ();it != end_it; ++it){AbstractThread & t = **it;t.join ();}q.clear ();}}
}/**This class wraps ReaperThread thread and its queue.*/
class Reaper
{
public:Reaper (){reaper_thread = ReaperThreadPtr (new ReaperThread (mtx, ev, queue));reaper_thread->start ();}~Reaper (){reaper_thread->signal_exit ();reaper_thread->join ();}void visit (log4cplus::thread::AbstractThreadPtr const & thread_ptr);private:log4cplus::thread::Mutex mtx;log4cplus::thread::ManualResetEvent ev;ThreadQueueType queue;ReaperThreadPtr reaper_thread;
};void
Reaper::visit (log4cplus::thread::AbstractThreadPtr const & thread_ptr)
{log4cplus::thread::MutexGuard guard (mtx);queue

http://www.ppmy.cn/server/100233.html

相关文章

“论软件体系结构的演化”写作框架,软考高级,系统架构设计师

论文真题 软件体系结构的演化是在构件开发过程中或软件开发完毕投入运行后&#xff0c;由于用户需求发生变化&#xff0c;就必须相应地修改原有软件体系结构&#xff0c;以满足新的变化了的软件需求的过程。体系结构的演化是一个复杂的、难以管理的问题。 请围绕“论软件体系…

Java项目:依赖包缺失、Maven获取异常,手动下载安装

Java项目&#xff1a;依赖包缺失、Maven获取异常&#xff0c;手动下载安装 1. 依赖报缺失&#xff0c;手动下载jar包2. 执行以下命令&#xff0c;安装。 1. 依赖报缺失&#xff0c;手动下载jar包 直接到Maven 仓库库&#xff1a;https://mvnrepository.com 2. 执行以下命令&a…

在Bash shell脚本中如何检查一个目录是否存在

问题 在 Bash shell 脚本中什么命令检查某个目录是否存在? 回答 要检查目录是否存在&#xff0c;请执行以下操作&#xff1a; if [ -d "$DIRECTORY" ]; thenecho "$DIRECTORY does exist." fi一行代码的形式则如下&#xff1a; [ -d "$DIRECTORY…

Qt for MCUs: 为嵌入式微控制器(MCU)设计和开发高性能应用程序

Qt for MCUs是一个完整的图形框架和工具包 软件平台 Linux、macOS、Windows 支持的开发语言 C、QML、Python、JavaScript等 开发商 The Qt Company 最近更新时间 2024年3月13日 最新版本 2.7 简介 Qt for MCUs提供了一个轻量级图形框架和工具包&#xff0c;使您能够…

针对thinkphp站点的漏洞挖掘和经验分享

0x1 前言 浅谈 目前在学习和研究thinkphp相关漏洞的打法&#xff0c;然后最近对于thinkphp资产的收集方面有了一个简单的认识&#xff0c;然后写一篇新手看的thinkphp相关的漏洞收集和挖掘的文章来分享下。然后后面是给师傅们分享下后台文件上传&#xff0c;然后直接打一个ge…

高级网络渗透测试技术(第一篇)

一、概述 网络渗透测试&#xff08;Penetration Testing, Pen Test&#xff09;是通过模拟恶意攻击者的行为来评估计算机系统、网络或Web应用的安全性。高级网络渗透测试技术则涵盖了更复杂和深入的测试方法&#xff0c;能够更有效地发现并利用系统中的潜在漏洞。 二、前期准…

ApiPost7 和 PostMan 区别

ApiPost7 和 PostMan 都是常用的 API 测试工具。 1. 用户界面和操作体验 ApiPost7 的界面设计可能更简洁直观&#xff0c;对新手更友好。 PostMan 则功能布局相对更复杂&#xff0c;但提供了更多的定制选项。 2. 协作和团队支持 PostMan 在团队协作和共享方面可能具有更强…

BUUCTF lovesql 1

我们使用万能密码登录进去 会获取到用户名和密码 Hello admin&#xff01; Your password is 0ed76caed1e7afe0bb1eaa590fa465ef 我们使用一下order by&#xff0c;发现2&#xff0c;3没问题&#xff0c;当我们输入4列的时候&#xff0c;4列是不存在的 也就是说一共3列&…