logback日志框架学习(2)logback的构造

news2024/12/23 6:46:21

官网

https://logback.qos.ch/manual/architecture.html

Logback构造

Logback’s basic architecture is sufficiently generic so as to apply under different circumstances. At the present time, logback is divided into three modules, logback-core, logback-classic and logback-access.

这里说logback很牛,主要分为三个模块 core classic access

The core module lays the groundwork for the other two modules. The classic module extends core. The classic module corresponds to a significantly improved version of log4j. Logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging systems such as log4j or java.util.logging (JUL) introduced in JDK 1.4. The third module called access integrates with Servlet containers to provide HTTP-access log functionality. A separate document covers access module documentation.

core模块是其他两个的基础,
classic继承了core,是对应log4j的进版本,实现了SLF4J API,可以轻松地在Logback和其他日志记录系统之间来回切换
access是提供http日志功能

In the remainder of this document, we will write “logback” to refer to the logback-classic module

其实我们常说的logback这日志框架,指的就是classic。

Logger, Appenders and Layouts

Logback is built upon three main classes: Logger, Appender and Layout. These three types of components work together to enable developers to log messages according to message type and level, and to control at runtime how these messages are formatted and where they are reported.
The Logger class is part of the logback-classic module. On the other hand, the Appender and Layout interfaces are part of logback-core. As a general-purpose module, logback-core has no notion of loggers.

logback主要是Logger、Appender和Layout 这三个类,来设置日志级别,日志输出的方式,日志的输出格式
Logger类是logback-classic模块的一部分。
Appender和Layout接口是logback-core核心的一部分。
作为一个通用模块,logback-core没有logger的概念。

Logger context

The first and foremost advantage of any logging API over plain System.out.println resides in its ability to disable certain log statements while allowing others to print unhindered. This capability assumes that the logging space, that is, the space of all possible logging statements, is categorized according to some developer-chosen criteria. In logback-classic, this categorization is an inherent part of loggers. Every single logger is attached to a LoggerContext which is responsible for manufacturing loggers as well as arranging them in a tree like hierarchy.

这里说log和sout的区别,说log可以让日志一部分打印一部分不打印,其实就是日志级别,但是log比sout有个优秀的地方,sout如果看了底层就会发现它有synchronized,多线程情况sout的时候会阻塞效率低下。

Loggers are named entities. Their names are case-sensitive and they follow the hierarchical naming rule:

Named Hierarchy

A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.

For example, the logger named “com.foo” is a parent of the logger named “com.foo.Bar”. Similarly, “java” is a parent of “java.util” and an ancestor of “java.util.Vector”. This naming scheme should be familiar to most developers.
The root logger resides at the top of the logger hierarchy. It is exceptional in that it is part of every hierarchy at its inception. Like every logger, it can be retrieved by its name, as follows:
The root logger resides at the top of the logger hierarchy. It is exceptional in that it is part of every hierarchy at its inception. Like every logger, it can be retrieved by its name, as follows:
Logger rootLogger = LoggerFactory.​getLogger(org.slf4j.Logger.​ROOT_LOGGER_NAME);
All other loggers are also retrieved with the class static getLogger method found in the org.slf4j.LoggerFactory class. This method takes the name of the desired logger as a parameter. Some of the basic methods in the Logger interface are listed below.

这里说了这么多就是说日志级别的层级关系,
例如 com.chenchi就是com.chenchi.age的父级关系

Effective Level aka Level Inheritance

日志的有效级别
TRACE, DEBUG, INFO, WARN and ERROR 被定义在ch.qos.logback.classic.Level这个类里 这个类是final类不能被继承所以日志级别就这么多了
日志级别一般默认是debug模式,这里的默认指的是root=debug

demo1

在这里插入图片描述
设置root的日志级别=debug, 所有其他的日志不用设置,都是debug级别了。

demo2

在这里插入图片描述
设置root=error,x=info ,x.y=debug ,x.y.z=warn,最后的日志级别就是他们自己设置的级别。

demo3

在这里插入图片描述
设置root=error,x=info ,x.y=NONE ,x.y.z=warn,最后x.y的日志级别是继承了x=INFO

demo4
在这里插入图片描述
设置root=debug,x=info ,x.y=NONE ,x.y.z=NONE,最后x.y和x.y.z的日志级别都继承了x=INFO

总结来说就是。
1.祖宗级别的日志类别是root设置的
2.其余的类或包如果没有父类,日志级别就是祖宗级别root
3.如果自己当前类或包设置了日志级别就是自己的 不用管父类。

Printing methods and the basic selection rule

TRACE < DEBUG < INFO < WARN < ERROR
在这里插入图片描述

import ch.qos.logback.classic.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
....

// get a logger instance named "com.foo". Let us further assume that the
// logger is of type  ch.qos.logback.classic.Logger so that we can
// set its level
ch.qos.logback.​classic.Logger logger = 
        (ch.qos.logback.​classic.Logger) LoggerFactory.​getLogger("com.foo");
//set its Level to INFO. The setLevel() method requires a logback logger
logger.setLevel(Level. INFO);

Logger barlogger = LoggerFactory.​getLogger(​"com.foo.Bar");

// This request is enabled, because WARN >= INFO
logger.warn("Low fuel level.");

// This request is disabled, because DEBUG < INFO. 
logger.debug("Starting search for nearest gas station.");

// The logger instance barlogger, named "com.foo.Bar", 
// will inherit its level from the logger named 
// "com.foo" Thus, the following request is enabled 
// because INFO >= INFO. 
barlogger.info("Located nearest gas station.");

// This request is disabled, because DEBUG < INFO. 
barlogger.debug("Exiting gas station search");

这个就不说了。

Retrieving Loggers

下面这两个是指向同一个对象的。
Logger x = LoggerFactory.getLogger(“wombat”);
Logger y = LoggerFactory.getLogger(“wombat”);

Appenders and Layouts

The ability to selectively enable or disable logging requests based on their logger is only part of the picture. Logback allows logging requests to print to multiple destinations. In logback speak, an output destination is called an appender. Currently, appenders exist for the console, files, remote socket servers, to MySQL, PostgreSQL, Oracle and other databases, JMS, and remote UNIX Syslog daemons.
More than one appender can be attached to a logger.

TheaddAppendermethod adds an appender to a given logger. Each enabled logging request for a given logger will be forwarded to all the appenders in that logger as well as the appenders higher in the hierarchy. In other words, appenders are inherited additively from the logger hierarchy. For example, if a console appender is added to the root logger, then all enabled logging requests will at least print on the console. If in addition a file appender is added to a logger, sayL, then enabled logging requests forLandL's children will print on a fileandon the console. It is possible to override this default behavior so that appender accumulation is no longer additive by setting the additivity flag of a logger to false.

基于日志记录程序选择性地启用或禁用日志记录请求的能力只是其中的一部分(日志级别)。Logback允许将日志记录请求打印到多个目的地。在logback语言中,输出目的地被称为appender。目前,存在用于控制台、文件、远程套接字服务器、MySQL、PostgreSQL、Oracle和其他数据库、JMS和远程UNIX Syslog守护进程的附加程序。
一个记录器可以连接多个附加程序。
AddAppender方法向给定的记录器添加一个appender。后面的翻译不太好我自己翻一下。其实就是说appender也是和日志级别一样是可追加或者说可继承的。如果root=consoleAppender 那么其余的包和类下的日志都有这个consoleAppender,但是如果我x.y包add了一个mysqlAppender那么x.y包下的类 就有了mysql+consoleAppender,但是某些包又比较重要不希望继承上层的appender我们可以将add设置为false,那么就只会拥有自己设置的appender
在这里插入图片描述
root.appender=A1 root不能设置additivty.flag 最后输出appender=A1
x.append.add=a-x1,a-x2 additivty.flag=true 所以最后appender=A1,a-x1,a-x2
x.y.appender.add=Node additivty.flag=true x.y继承x最后appender=A1,a-x1,a-x2
x.y.z.appender.add=a-xyz1 additivty.flag=true x.y.z继承x最后appender=A1,a-x1,a-x2,a-xyz1
security.appender.add=a-sec additivty.flag=false 不继承root最后appender=a-sec
security_access.appender.add=NONE additivty.flag=true 不继承root,但是继承secuirty 最后appender=a-sec

layout

For example, the PatternLayout with the conversion pattern “%-4relative [%thread] %-5level %logger{32} - %msg%n” will output something akin to:

176  [main] DEBUG manual.architecture.HelloWorld2 - Hello world.

第一个字段是自程序启动以来经过的毫秒数。
第二个字段是发出日志请求的线程。
第三个字段是日志请求的级别。
第四个字段是与日志请求关联的记录器的名称。
“-”后面的文本是请求的消息。

Parameterized logging

错误的写法
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
正确的写法
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
因为前者还需要构造msg,结果构造了发现日志级别不对又不打印也不输出,所以浪费了时间,而后者只需要检查日志级别即可,耗费前者1%的时间。当然无所谓了,我也没见人这么详细过。

Better alternative

logger.debug(“The new entry is “+entry+”.”);
logger.debug(“The new entry is {}.”, entry);
后者比前者好30倍。。因为前者是字符串拼接多个对象,后者是string.format。

A peek under the hood

一下子没看懂官网说的啥。感觉也不重要

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1045378.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue3 +elementplus | vue2+elementui 动态地通过验证规则子新增或删除单个表单字段

效果图 点击 ‘’ 新增一行&#xff0c;点击‘-’ 删除一行 vue3elementplus写法 template <el-dialog v-model"dialogFormVisible" :title"title"><el-form ref"ruleFormRef" :model"form" :inline"true" lab…

强势得分套路之一:单基因泛癌+实验验证

今天给同学们分享一篇单基因泛癌实验验证的生信文章“A human pan-cancer system analysis of heat shock protein family A member 5”&#xff0c;这篇文章于2023年5月15日发表在Am J Cancer Res期刊上&#xff0c;影响因子为5.2。 热休克蛋白家族A成员5&#xff08;HSPA5&am…

2024年湖北中级工程师职称申报需要准备什么资料呢?

湖北中级职称申报每个地方需要的资料和要求都不一样&#xff0c;但是大部分申报材料、条件和要求基本是一致的&#xff0c;有的只是细微差别。那么湖北中级工程师职称申报需要准备什么资料呢&#xff1f;今天甘建二先告诉你&#xff0c;职称资料需要提早准备哟&#xff0c;有的…

command not found

一 问题 连着几次登录&#xff0c;输入ls&#xff0c;都not found 二 解决方案 1. 临时生效&#xff0c;下次登录还是not found export PATH/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin 2. 永久生效&#xff0c;将上面的配置&#xff0c;添加…

iTunes怎么用?iTunes如何恢复备份?(果粉必备教程)

iTunes是由苹果公司推出的一款免费数字媒体播放应用软件。通过使用iTunes软件&#xff0c;用户可以在软件上管理和播放数字音乐、视频、电影&#xff0c;以及电视节目等等。此外&#xff0c;iTunes还是管理数据的好帮手&#xff0c;用户可以通过iTunes备份管理自己手机上的数据…

“TaiChiHealthPreservation“ app Tech Support(URL)

If you have any questions, you can either leave a message or send the questions to our email address. We will answer them for you in the first time. Address: drrjownbnhotmail.com thank you.

机器人过程自动化(RPA)入门 4. 数据处理

到目前为止,我们已经了解了RPA的基本知识,以及如何使用流程图或序列来组织工作流中的步骤。我们现在了解了UiPath组件,并对UiPath Studio有了全面的了解。我们用几个简单的例子制作了我们的第一个机器人。在我们继续之前,我们应该了解UiPath中的变量和数据操作。它与其他编…

“智”造未来,江西同为科技(TOWE)参展第四届广州“两交会”圆满落幕

2023年9月13日—15日&#xff0c;由全国工商联科技装备业商会主办的第四届“两交会”&#xff08;广州两用技术装备成果交易会&#xff09;在广州广交会展馆圆满举行。本次展会以市场为主导&#xff0c;以需求为牵引&#xff0c;围绕相关单位“作训、后勤、装备”任务&#xff…

一文教你学会ArcGIS Pro地图设计与制图系列全流程(3)

ArcGIS Pro做的成果图及系列文章目录&#xff1a; 系列文章全集&#xff1a; 《一文教你学会ArcGIS Pro地图设计与制图系列全流程&#xff08;1&#xff09;》《一文教你学会ArcGIS Pro地图设计与制图系列全流程&#xff08;2&#xff09;》《一文教你学会ArcGIS Pro地图设计与…

喜讯 | 怿星科技获评SAE“优秀核心零部件企业”,测试软件平台工具广受赞誉

2023年9月22日-23日&#xff0c;SAE 2023汽车智能与网联技术国际学术会议成功举行。此次学术会议由SAE International与南昌智能新能源汽车研究院联合主办&#xff0c;大会汇聚了来自国内外智能网联领域的顶尖专家和学者。大会同期颁布的奖项旨在向行业推选出更多新时代涌现的杰…

人人都是项目经理-项目管理概述(一)

一、重新认识项目管理 1. 什么是项目 项目&#xff08;Project&#xff09;&#xff0c;是为提供某项独特的产品&#xff08;交付物&#xff09;&#xff0c;服务或成果所做的临时性努力。 – PMBOK指南 项目是指一系列独特的、复杂的并相互关联的活动&#xff0c;这些活动有着…

4、ARM异常处理

一、异常处理 1、异常 概念 处理器在正常执行程序的过程中可能会遇到一些不正常的的事件发生&#xff0c;这时处理器就要将当前的程序暂停下来转去处理这个异常的事件&#xff0c;异常事件完成后再返回到之前被异常打断的点继续执行 2、异常处理机制 不同的处理器对异常的…

Pandas行列转换

一、问题描述 在实际的数据处理过程中&#xff0c;常常会遇到需要将DataFrame中的列转换为行或将行转换为列的情况。但是&#xff0c;如果使用传统的Python方法&#xff0c;这种操作会非常繁琐且容易出错。因此&#xff0c;我们可以使用pandas库提供的优雅方式来完成列转行或行…

RDMA操作类型(三)

Send操作 引用一下&#xff0c;IB协议第9.4.1章节原文&#xff1a; The SEND Operation is sometimes referred to as a Push operation or as having channel semantics. Both terms refer to how the SW client of the transport service views the movement of data. With …

uni-app:canvas-绘制图形4(获取画布宽高,根据画布宽高进行图形绘制)

效果 代码 var width ; var height ; const query uni.createSelectorQuery(); //获取宽度 query.select(#firstCanvas).fields({ size: true }, (res) > { width res.width; height res.height; }).exec(); console.log(宽度width); console.log(高…

代码随想录算法训练营 动态规划part04

一、动态规划&#xff1a;01背包理论基础 挺详细的代码随想录 (programmercarl.com) 二、动态规划&#xff1a;01背包理论基础&#xff08;滚动数组&#xff09; 代码随想录 (programmercarl.com) 三、 分割等和子集 416. 分割等和子集 - 力扣&#xff08;LeetCode&#x…

软件安全测试和渗透测试有什么区别和联系?

随着网络攻击和数据泄露事件的频繁发生&#xff0c;软件系统的安全性已成为用户和企业关注的重要问题。通过进行软件安全测试和渗透测试&#xff0c;可以有效地发现和修复系统中的潜在漏洞和安全隐患&#xff0c;提高系统的抵御能力和可靠性。这不仅可以保护用户的隐私和数据安…

C语言进阶第四课-----------指针的进阶----------指针和数组笔试解释

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…

手把手教你集成环信ReactNative离线推送

前言&#xff1a;在集成ReactNative推送之前&#xff0c;需要了解ReactNative与Android原生交互 一、RN与Android原生交互 RN给原生传递参数 步骤&#xff1a; 1.用Android Studio打开一个已经存在的RN项目&#xff0c;即用AS打开 项目文件夹/android,如下图所示 2.在An…

如何将微信、支付宝、字节等小程序直接迁移到自有app中

简要回顾一下 FinClip Studio 的相关能力&#xff1a; FinClip Studio 是一款强大的小程序云端容器&#xff0c;它可以将小程序嵌入到您的原生 App 中&#xff0c;实现无缝的融合体验。不仅如此&#xff0c;FinClip 还提供了一系列的工具和功能&#xff0c;以简化小程序的开发…