当前位置: 首页 > news >正文

聊聊logback的StatusManager

本文主要研究一下logback的StatusManager

StatusManager

ch/qos/logback/core/status/StatusManager.java

public interface StatusManager {/*** Add a new status message.* * @param status*/void add(Status status);/*** Obtain a copy of the status list maintained by this StatusManager.* * @return*/List<Status> getCopyOfStatusList();/*** Return the highest level of all the statii.* * @return*/// int getLevel();/*** Return the number of status entries.* * @return*/int getCount();/*** Add a status listener.* * @param listener*//*** Add a status listener. The StatusManager may decide to skip installation if* an earlier instance was already installed.* * @param listener* @return true if actually added, false if skipped*/boolean add(StatusListener listener);/*** ); Remove a status listener.* * @param listener*/void remove(StatusListener listener);/*** Clear the list of status messages.*/void clear();/*** Obtain a copy of the status listener list maintained by this StatusManager* * @return*/List<StatusListener> getCopyOfStatusListenerList();}

StatusManager接口针对status定义了add、getCopyOfStatusList、getCount、clear方法,针对StatusListener定义了add、remove、getCopyOfStatusListenerList方法

BasicStatusManager

ch/qos/logback/core/BasicStatusManager.java

public class BasicStatusManager implements StatusManager {final protected List<Status> statusList = new ArrayList<Status>();final protected CyclicBuffer<Status> tailBuffer = new CyclicBuffer<Status>(TAIL_SIZE);final protected LogbackLock statusListLock = new LogbackLock();final protected List<StatusListener> statusListenerList = new ArrayList<StatusListener>();final protected LogbackLock statusListenerListLock = new LogbackLock();//......
}

BasicStatusManager实现了StatusManager接口,它使用statusList及statusListLock来操作status,使用statusListenerList及statusListenerListLock来操作StatusListener;另外针对status还提供了tailBuffer

add status

    public void add(Status newStatus) {// LBCORE-72: fire event before the count checkfireStatusAddEvent(newStatus);count++;if (newStatus.getLevel() > level) {level = newStatus.getLevel();}synchronized (statusListLock) {if (statusList.size() < MAX_HEADER_COUNT) {statusList.add(newStatus);} else {tailBuffer.add(newStatus);}}}

add方法先加锁,再判断是否超出限制,没有则添加到statusList,超出则添加到tailBuffer

getCopyOfStatusList

    public List<Status> getCopyOfStatusList() {synchronized (statusListLock) {List<Status> tList = new ArrayList<Status>(statusList);tList.addAll(tailBuffer.asList());return tList;}}

getCopyOfStatusList则加锁,然后从statusList及tailBuffer获取status

clear

    public void clear() {synchronized (statusListLock) {count = 0;statusList.clear();tailBuffer.clear();}}

clear则加锁,重置count,清空statusList及tailBuffer

add listener

    public boolean add(StatusListener listener) {synchronized (statusListenerListLock) {if (listener instanceof OnConsoleStatusListener) {boolean alreadyPresent = checkForPresence(statusListenerList, listener.getClass());if (alreadyPresent)return false;}statusListenerList.add(listener);}return true;}private boolean checkForPresence(List<StatusListener> statusListenerList, Class<?> aClass) {for (StatusListener e : statusListenerList) {if (e.getClass() == aClass)return true;}return false;}    

add listener方法先加锁,然后判断是否已经存在,不存在则添加到statusListenerList

remove

    public void remove(StatusListener listener) {synchronized (statusListenerListLock) {statusListenerList.remove(listener);}}

remove则先加锁,然后从statusListenerList中移除

getCopyOfStatusListenerList

    public List<StatusListener> getCopyOfStatusListenerList() {synchronized (statusListenerListLock) {return new ArrayList<StatusListener>(statusListenerList);}}

getCopyOfStatusListenerList则先加锁然后拷贝statusListenerList

Status

ch/qos/logback/core/status/Status.java

public interface Status {int INFO = 0;int WARN = 1;int ERROR = 2;int getLevel();int getEffectiveLevel();Object getOrigin();String getMessage();Throwable getThrowable();/*** @eprecated. Use getTimestamp instead.* @return*/@Deprecateddefault Long getDate() {return getTimestamp();}long getTimestamp();boolean hasChildren();void add(Status child);boolean remove(Status child);Iterator<Status> iterator();}

Status接口定义了getLevel、getEffectiveLevel、getOrigin、getMessage、getThrowable、getTimestamp、hasChildren、add、remove、iterator方法

StatusBase

ch/qos/logback/core/status/StatusBase.java

abstract public class StatusBase implements Status {static private final List<Status> EMPTY_LIST = new ArrayList<Status>(0);int level;final String message;final Object origin;List<Status> childrenList;Throwable throwable;long timestamp;public synchronized void add(Status child) {if (child == null) {throw new NullPointerException("Null values are not valid Status.");}if (childrenList == null) {childrenList = new ArrayList<Status>();}childrenList.add(child);}public synchronized boolean hasChildren() {return ((childrenList != null) && (childrenList.size() > 0));}public synchronized Iterator<Status> iterator() {if (childrenList != null) {return childrenList.iterator();} else {return EMPTY_LIST.iterator();}}public synchronized boolean remove(Status statusToRemove) {if (childrenList == null) {return false;}// TODO also search in childrens' childrenreturn childrenList.remove(statusToRemove);}public synchronized int getEffectiveLevel() {int result = level;int effLevel;Iterator<Status> it = iterator();Status s;while (it.hasNext()) {s = (Status) it.next();effLevel = s.getEffectiveLevel();if (effLevel > result) {result = effLevel;}}return result;}//......}

StatusBase声明实现Status接口,它通过childrenList来存储子status

InfoStatus

ch/qos/logback/core/status/InfoStatus.java

public class InfoStatus extends StatusBase {public InfoStatus(String msg, Object origin) {super(Status.INFO, msg, origin);}public InfoStatus(String msg, Object origin, Throwable t) {super(Status.INFO, msg, origin, t);}}

InfoStatus继承了StatusBase,它的level为Status.INFO

WarnStatus

ch/qos/logback/core/status/WarnStatus.java

public class WarnStatus extends StatusBase {public WarnStatus(String msg, Object origin) {super(Status.WARN, msg, origin);}public WarnStatus(String msg, Object origin, Throwable t) {super(Status.WARN, msg, origin, t);}}

WarnStatus继承了StatusBase,它的level为Status.WARN

ErrorStatus

ch/qos/logback/core/status/ErrorStatus.java

public class ErrorStatus extends StatusBase {public ErrorStatus(String msg, Object origin) {super(Status.ERROR, msg, origin);}public ErrorStatus(String msg, Object origin, Throwable t) {super(Status.ERROR, msg, origin, t);}}

ErrorStatus继承了StatusBase,它的level为Status.ERROR

小结

logback定义了StatusManager用于管理status及其listener,其add方法会回调listener,之后加锁,再判断是否超出限制,没有则添加到statusList,超出则添加到tailBuffer;Status是个接口,它有一个抽象类为StatusBase,而InfoStatus、WarnStatus、ErrorStatus都继承了StatusBase。

相关文章:

聊聊logback的StatusManager

序 本文主要研究一下logback的StatusManager StatusManager ch/qos/logback/core/status/StatusManager.java public interface StatusManager {/*** Add a new status message.* * param status*/void add(Status status);/*** Obtain a copy of the status list maintain…...

[PyTorch][chapter 61][强化学习-免模型学习1]

前言&#xff1a; 在现实的学习任务中&#xff0c;环境 其中的转移概率P,奖赏函数R 是未知的&#xff0c;或者状态X也是未知的 称为免模型学习&#xff08;model-free learning&#xff09; 目录&#xff1a; 1: 蒙特卡洛强化学习 2&#xff1a;同策略-蒙特卡洛强化学习 3&am…...

网络运维Day04-补充

文章目录 周期性计划任务周期性计划任务使用案例一案例二 周期性计划任务 在固定时间可以完成相同的任务&#xff0c;被称之为周期性计划任务由crond服务提供需要将定时任务&#xff0c;写到一个文件书写格式如下 分 时 日 月 周 任务(绝对路径)分&#xff1a;0-59时&#xff…...

前端埋点方式

前言&#xff1a; 想要了解用户在系统中所做的操作&#xff0c;从而得出用户在本系统中最常用的模块、在系统中停留的时间。对于了解用户的行为、分析用户的需求有很大的帮助&#xff0c;想实现这种需求可以通过前端埋点的方式。 埋点方式&#xff1a; 1.什么是埋点&#xff1f…...

iOS导航栏返回按钮

导航栏返回按钮隐藏&#xff1a; override func pushViewController(_ viewController: UIViewController, animated: Bool) {if let vc self.viewControllers.last {let backItem UIBarButtonItem()backItem.title ""vc.navigationItem.backBarButtonItem backI…...

2023中国视频云市场报告:腾讯云音视频解决方案份额连续六次蝉联榜首,加速全球化布局

近日&#xff0c;国际数据公司&#xff08;IDC&#xff09;发布了《中国视频云市场跟踪&#xff08;2023上半年&#xff09;》报告&#xff0c;腾讯云音视频的解决方案份额连续六次蝉联榜首&#xff0c;并在视频生产创作与媒资管理市场份额中排名第一。同时&#xff0c;在实时音…...

jpa Repository的常用写法总结

一、前言 之前项目在xml中写sql&#xff0c;感觉标签有很多&#xff0c;比较灵活&#xff1b; 最近在写新项目&#xff0c;使用了jpa&#xff0c;只能在java中写sql了&#xff0c;感觉不太灵活&#xff0c;但是也得凑付用。 以下总结下常用入参出参写法。 二、Repository代…...

笔记本电脑 禁用/启用 自带键盘

现在无论办公还是生活 很多人都会选择笔记本电脑 但很多人喜欢机械键盘 或者 用一些外接键盘 但是很多时候我们想操作 会碰到笔记本原来的键盘导致错误操作 那么 我们就需要将笔记本原来的键盘禁用掉 我们先以管理员身份运行命令窗口 然后 有两个命令 禁用默认键盘 sc conf…...

基于 golang 从零到一实现时间轮算法 (三)

引言 本文参考小徐先生的相关博客整理&#xff0c;项目地址为&#xff1a; https://github.com/xiaoxuxiansheng/timewheel/blob/main/redis_time_wheel.go。主要是完善流程以及记录个人学习笔记。 分布式版实现 本章我们讨论一下&#xff0c;如何基于 redis 实现分布式版本的…...

k8s 1.28安装

容器运行时&#xff0c;containerd 按照官方的指导&#xff0c;需要安装runc和cni插件&#xff0c;提示的安装方式&#xff0c;有三种&#xff1a; 二进制安装包源码apt-get 或 dnf安装 我们这里选用第三种&#xff0c;找到docker官方提供的安装方式 ubuntu-containerd # A…...

安装anaconda时控制台conda-version报错

今天根据站内的一篇博客教程博客在此安装anaconda时&#xff0c;检查conda版本时报错如下&#xff1a; >>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<< Traceback (most recent call last): File “D:\An…...

链表(1)

目录 单链表 主函数test.c test1 test2 test3 test4 头文件&函数声明SList.h 函数实现SList.c 打印SLPrint 创建节点CreateNode 尾插SLPushBack 头插SLPushFront 头删SLPopBck 尾删SLPopFront 易错点 本篇开始链表学习。今天主要是单链表&OJ题目。 单链…...

智慧农业:农林牧数据可视化监控平台

数字农业是一种现代农业方式&#xff0c;它将信息作为农业生产的重要元素&#xff0c;并利用现代信息技术进行农业生产过程的实时可视化、数字化设计和信息化管理。能将信息技术与农业生产的各个环节有机融合&#xff0c;对于改造传统农业和改变农业生产方式具有重要意义。 图扑…...

知识注入以对抗大型语言模型(LLM)的幻觉11.6

知识注入以对抗大型语言模型&#xff08;LLM&#xff09;的幻觉 摘要1 引言2 问题设置和实验2.1 幻觉2.2 生成响应质量 3 结果和讨论3.1 幻觉3.2 生成响应质量 4 结论和未来工作 摘要 大型语言模型&#xff08;LLM&#xff09;内容生成的一个缺点是产生幻觉&#xff0c;即在输…...

机器人物理交互场景及应用的实际意义

机器人物理交互场景是指机器人与物理世界或人类进行实际的物理互动和交互的情境。这些场景涉及机器人在不同环境和应用中使用其物理能力&#xff0c;以执行任务、提供服务或与人类互动。 医疗协助&#xff1a; 外科手术助手&#xff1a;机器人可以用于外科手术&#xff0c;提供…...

Kubernetes Dashboard 用户名密码方式登录

Author&#xff1a;rab 前言 为了 K8s 集群安全&#xff0c;默认情况下 Dashboard 以 Token 的形式登录的&#xff0c;那如果我们想以用户名/密码的方式登录该怎么操作呢&#xff1f;其实只需要我们创建用户并进行 ClusterRoleBinding 绑定即可&#xff0c;接下来是具体的操作…...

Redisson中的对象

Redisson - 是一个高级的分布式协调Redis客服端&#xff0c;能帮助用户在分布式环境中轻松实现一些Java的对象 (Bloom filter, BitSet, Set, SetMultimap, ScoredSortedSet, SortedSet, Map, ConcurrentMap, List, ListMultimap, Queue, BlockingQueue, Deque, BlockingDeque, …...

GNU ld链接器 lang_process()(二)

一、ldemul_create_output_section_statements() 位于lang_process()中11行 。 该函数用于创建与目标有关的输出段的语句。这些语句将用于描述输出段的属性和分配。 void ldemul_create_output_section_statements (void) {if (ld_emulation->create_output_section_sta…...

《国产服务器操作系统发展报告(2023)》重磅发布

11月1日&#xff0c;《国产服务器操作系统发展报告&#xff08;2023&#xff09;》&#xff08;以下简称“报告”&#xff09;在 2023 云栖大会上正式发布&#xff0c;开放原子开源基金会理事长孙文龙、中国信息通信研究院副总工程师石友康、阿里云基础软件部副总裁马涛、浪潮信…...

【PTE-day03 报错注入】

报错注入 1、报错注入 group by count2、报错注入 extractvalue3、报错注入updatexml1、报错注入 group by count http://124.222.124.9:8888/Less-5/?id=-1 union select 1,count(*),concat((select database()),ceil(rand(0)*2)) as a from information_schema.tables grou…...

5个快速排查Goss测试失败的高效调试技巧

5个快速排查Goss测试失败的高效调试技巧 【免费下载链接】goss Goss是一个开源的Go语言测试框架&#xff0c;用于简化Go应用程序的测试和验证。它提供了一系列的测试断言和测试工具&#xff0c;可以帮助开发者编写更加简洁和可维护的测试代码。 项目地址: https://gitcode.co…...

Protocol Buffer 入门:跨平台的高效序列化神器

&#x1f525;个人主页&#xff1a;Milestone-里程碑 ❄️个人专栏: <<力扣hot100>> <<C>><<Linux>> <<Git>><<MySQL>> &#x1f31f;心向往之行必能至 目录 一、什么是 Protobuf&#xff1f; 二、序列化与反…...

3大核心技术解析:猫抓cat-catch如何实现浏览器媒体资源精准捕获

3大核心技术解析&#xff1a;猫抓cat-catch如何实现浏览器媒体资源精准捕获 【免费下载链接】cat-catch 猫抓 chrome资源嗅探扩展 项目地址: https://gitcode.com/GitHub_Trending/ca/cat-catch 猫抓cat-catch是一款专为技术爱好者和开发者设计的浏览器扩展工具&#xf…...

P1061 Jam 的计数法【洛谷算法习题】

P1061 Jam 的计数法 网页链接 P1061 Jam 的计数法 题目描述 Jam 是个喜欢标新立异的科学怪人。他不使用阿拉伯数字计数&#xff0c;而是使用小写英文字母计数&#xff0c;他觉得这样做&#xff0c;会使世界更加丰富多彩。 在他的计数法中&#xff0c;每个数字的位数都是相…...

STM32F103重映射实战:GPIO_Remap1_CAN1与GPIO_Remap2_CAN1到底选哪个?

STM32F103重映射实战&#xff1a;GPIO_Remap1_CAN1与GPIO_Remap2_CAN1到底选哪个&#xff1f; 第一次在STM32F103上配置CAN总线时&#xff0c;看到GPIO_Remap1_CAN1和GPIO_Remap2_CAN1这两个选项&#xff0c;我完全懵了——它们有什么区别&#xff1f;为什么需要两个重映射选项…...

从报错到解决:Pycharm中Tensorflow2.x与1.x代码兼容性问题全解析

从报错到解决&#xff1a;Pycharm中Tensorflow2.x与1.x代码兼容性问题全解析 在深度学习领域&#xff0c;TensorFlow作为最受欢迎的框架之一&#xff0c;其版本迭代带来的变化常常让开发者感到头疼。特别是从TensorFlow 1.x升级到2.x版本后&#xff0c;许多核心API发生了重大改…...

如何在1小时内掌握TinySAM:从零开始构建高效图像分割模型

如何在1小时内掌握TinySAM&#xff1a;从零开始构建高效图像分割模型 【免费下载链接】TinySAM 项目地址: https://gitcode.com/gh_mirrors/ti/TinySAM 想象一下&#xff0c;你需要在移动设备上实时分割图像中的任意物体&#xff0c;但传统模型动辄几百兆&#xff0c;运…...

Gurobi Python接口避坑指南:从安装、建模到求解电影排片问题的实战记录

Gurobi Python实战避坑手册&#xff1a;电影排片优化全流程解析 第一次接触Gurobi时&#xff0c;我被它号称的"商业求解器性能标杆"吸引&#xff0c;却在安装环节就被Anaconda环境冲突绊住了脚步。作为从开源求解器转战商业工具的用户&#xff0c;我完整记录了从零开…...

M2LOrder模型Mathtype公式编辑器的趣味扩展:为数学证明添加情感注释

M2LOrder模型Mathtype公式编辑器的趣味扩展&#xff1a;为数学证明添加情感注释 你有没有过这样的经历&#xff1f;面对一篇复杂的数学论文或教材&#xff0c;读到某个证明步骤时&#xff0c;心里忍不住嘀咕&#xff1a;“这一步也太巧妙了&#xff0c;怎么想到的&#xff1f;…...

PolSARPro软件安装全攻略:从下载到处理Sentinel-1A数据的保姆级教程

PolSARPro软件安装全攻略&#xff1a;从下载到处理Sentinel-1A数据的保姆级教程 在遥感数据处理领域&#xff0c;PolSARPro无疑是一颗璀璨的明珠。这款由法国雷恩第一大学开发的极化合成孔径雷达处理软件&#xff0c;已经成为科研人员和学生处理Sentinel-1A等卫星数据的首选工具…...