Chapter 5: Encoders

2023-11-03 04:08
文章标签 chapter encoders

本文主要是介绍Chapter 5: Encoders,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

What is an encoder

编码器负责将事件转化为字节数组,并将字节数组转换成一个OutputStream 。

Encoder ---> Byte[] ---> OutputStream

 

在0.9.19版本之前,大多数的appender都依赖layout布局组件将事件转换为字符串String

在使用java.io.Writer将其写入到文件

在0.9.19版本之前,对FileAppender将在内部嵌套PatternLayout进行样式设置

从0.9.19版本开始,FileAppender将通过Encoder设置样式,不再设置layout属性了

 

This appender no longer admits a layout as a sub-component, set an encoder instead.

to (GOOD)

<appender name="FILE" class="ch.qos.logback.core.FileAppender"><file>testFile.log</file>...<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"><pattern>%msg%n</pattern></encoder>
</appender>  

or the shorter equivalent (GOOD) 

<appender name="FILE" class="ch.qos.logback.core.FileAppender"><file>testFile.log</file>...<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --><encoder><pattern>%msg%n</pattern></encoder>
</appender> 

 

For layout type other than PatternLayout, for example HTMLLayout, your configuration files need to be changed

to (GOOD)

<appender name="FILE" class="ch.qos.logback.core.FileAppender"><file>testFile.log</file>...<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"><layout class="ch.qos.logback.classic.html.HTMLLayout"><pattern>%msg%n</pattern></layout></encoder>
</appender> 

 

Layouts仅仅能够将event转换为某种样式的字符串String

此外,鉴于Layout无法控制何时对event进行写出,也没有批处理能力

Encoder不仅提供了写出字节数组的格式,还可以控制何时进行写出操作

Encoder兼备Layout样式和决定日志写出时机的功能

到目前为止,PatternLayoutEncoder是唯一真正使用的Encoder

PatternLayoutEncoder包装了一个LayoutPattern,从而提供了设置样式的功能

 

Encoder负责将event转换为Byte[],并将结果写到合适的OutputStream流中

这样,appender就可以通过Encoder完全控制何时什么bytes进行写出操作

 

LayoutWrappingEncoder

直到logback版本0.9.19 ,许多appender都依赖于布局实例来控制日志输出格式

由于Layout 的接口已经提供了大量的样式设置功能,我们只需要将Encoder与Layout结合在一起即可

LayoutWrappingEncoder作为一个桥梁,有效的将Encoder与Layout两者结合在一起

LayoutWrappingEncoder实现了Encoder接口,并对Layout进行了包装(Layout将event转换为字符串)

 

由此,得到:

Event ---> Layout ---> Formated String ---> Encoder ---> Byte[] ---> OutputStream 

 

LayoutWrappingEncoder

package ch.qos.logback.core.encoder;public class LayoutWrappingEncoder<E> extends EncoderBase<E> {protected Layout<E> layout;private Charset charset;private boolean immediateFlush = true;public void doEncode(E event) throws IOException {String txt = layout.doLayout(event);outputStream.write(convertToBytes(txt));if (immediateFlush)outputStream.flush();}private byte[] convertToBytes(String s) {if (charset == null) {return s.getBytes();} else {return s.getBytes(charset);}} 
}

 

 

PatternLayoutEncoder

鉴于PatternLayout是最常用的布局, logback这种常见的用例满足与PatternLayoutEncoder 

immediateFlush property

true: 保证日志及时输出到文件,会影响系统处理日志的吞吐量;

false:可以大大提高日志处理的throughout,大概5倍,但有可能造成日志丢失(appender没有正确关闭);

<appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>foo.log</file><encoder><pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern><!-- this quadruples logging throughput --><immediateFlush>false</immediateFlush></encoder> 
</appender>

 

 

Output pattern string as header

在输入日志的上方,输出日志的Pattern

默认为false,不在头部输出样式

<appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>foo.log</file><encoder><pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern><outputPatternAsHeader>true</outputPatternAsHeader></encoder> 
</appender>

 

This will result output akin to the following in the log file: 

#logback.classic pattern: %d [%thread] %-5level %logger{36} - %msg%n
2012-04-26 14:54:38,461 [main] DEBUG com.foo.App - Hello world
2012-04-26 14:54:38,461 [main] DEBUG com.foo.App - Hi again

 

%d                   日期:2012-04-26 14:54:38,461

[%thread]         线程:[mian]

%-5level           级别:DEBUG

%logger{36}  类路径:com.foo.App

%msg               消息:Hello world

%n                    换行

 

 

 

 

 

 

 

 

 

这篇关于Chapter 5: Encoders的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Chapter 13 普通组件的注册使用

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、组件创建二、局部注册三、全局注册 前言 在 Vue.js 中,组件是构建应用程序的基本单元。本章详细讲解了注册和使用 Vue 的普通组件的两种方式:局部注册和全局注册。 本篇文章参考黑马程序员 一、组件创建 ①定义 Vue 组件是一种具有特定功能的 Vue 实

Chapter 10 Stability and Frequency Compensation

Chapter 10 Stability and Frequency Compensation Chapter 8介绍了负反馈, 这一章介绍稳定性, 如果设计不好, 负反馈系统是要发生震荡的. 首先我们学习理解稳定判断标准和条件, 然后学习频率补偿, 介绍适用于不同运放的补偿方式, 同时介绍不同补偿对两级运放slew rate的影响, 最后介绍Nyquist’s判断标准 10.1 Gener

[学习笔记]《CSAPP》深入理解计算机系统 - Chapter 3 程序的机器级表示

总结一些第三章的一些关键信息 Chapter 3 程序的机器级表示结构 updating... Chapter 3 程序的机器级表示 局部变量通常保存在寄存器中,而不是内存中,访问寄存器比内存快的多. 有些时候,局部数据必须存放在内存中, 寄存器不足够存放所有的本地数据对一个局部变量使用地址运算符 &, 因此必须能够为它产生一个地址某些局部变量是数组或结构,因此必须能够通过数组或

Chapter 10 async函数 await关键字

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、async 函数二、await 关键字 前言 在现代 JavaScript 开发中,异步编程是一个重要的概念。随着 ES2017 的引入,async 函数和 await 关键字为处理异步操作提供了更简洁和可读的方式。本章详细讲解了这两个关键字的特性及其用法。 一、

Chapter 2 multi-armed Bandit

引用:https://blog.csdn.net/mmc2015/article/details/51247677 https://blog.csdn.net/coffee_cream/article/details/58034628 https://blog.csdn.net/heyc861221/article/details/80129310   The most importa

第三章 少量(无)标记增强现实——Chapter 3:Marker-less Augmented Reality

注释: 1、翻译书名:Mastering OpenCV with Practical Computer Vision Projects 2、翻译章节:Chapter 3:Marker-less Augmented Reality 3、电子书下载,源代码下载,请参考:http://blog.csdn.net/raby_gyl/article/details/11617875 4、本章程序

Focus On 3D Terrain Programming·Chapter 5(1)

写在前面的话 原著:《Focus On 3D Terrain Programming》 章节:Chapter5 Geomipmapping for the CLOD Impaired 说明:图形渣、英语渣,学习的同时记录下来的(主要是不翻译就读不下去TT),希望能和大家相互交流学习,翻译有误的地方欢迎指出^^ 正文开始↓ 哦吼!你将要学习地形编程中的核心内容,里面包含了相当复杂的算法。实

Chapter 07 watch侦听器

欢迎大家订阅【Vue2+Vue3】入门到实践 专栏,开启你的 Vue 学习之旅! 文章目录 前言一、基本用法二、深度侦听 前言 在 Vue 中,watch 侦听器是一个非常实用的工具,用于处理自定义数据的变化。本文详细讲解了 watch 侦听器的基本用法、深度侦听和常见应用场景。 一、基本用法 ①定义 watch 侦听器用于观察 Vue 实例上的数据变更。当被观察

cartographer analysis - second chapter

第一章中的代码是ros-cartographer,实现了对cartographer的调用。通过trajectory_builder_ 的AddHorizontalLaserFan ,AddLaserFan3D ,AddImuData 方法向cartographer提供了传感器数据。 trajectory_builder_ 属于类 GlobalTrajectoryBuilderInterface.