logback 常用配置详解(二) appender appender: appender是configuration的子节点,是负责写日志的组件。 appender有两个必要属性name和class。name指定appender名称,class指定appender的全限定名。 1.ConsoleAppender: 把日志添加到控制台,有以下子节点
logback 常用配置详解(二)
1.ConsoleAppender:
把日志添加到控制台,有以下子节点:
例如:
Xml代码
- configuration>
- appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- encoder>
- pattern>%-4relative [%thread] %-5level %logger{35} - %msg %npattern>
- encoder>
- appender>
- root level="DEBUG">
- appender-ref ref="STDOUT" />
- root>
- configuration>
2.FileAppender:
把日志添加到文件,有以下子节点:
例如:
Xml代码
- configuration>
- appender name="FILE" class="ch.qos.logback.core.FileAppender">
- file>testFile.logfile>
- append>trueappend>
- encoder>
- pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>
- encoder>
- appender>
- root level="DEBUG">
- appender-ref ref="FILE" />
- root>
- configuration>
3.RollingFileAppender:
滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。有以下子节点:
triggeringPolicy >: 告知 RollingFileAppender 合适激活滚动。
rollingPolicy:
TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动。有以下子节点:
fileNamePattern>:
必要节点,包含文件名及“%d”转换符, “%d”可以包含一个java.text.SimpleDateFormat指定的时间格式,如:%d{yyyy-MM}。如果直接使用 %d,默认格式是 yyyy-MM-dd。
RollingFileAppender 的file字节点可有可无,通过设置file,可以为活动文件和归档文件指定不同位置,当前日志总是记录到file指定的文件(活动文件),活动文件的名字不会改变;如果没设置file,活动文件的名字会根据fileNamePattern 的值,每隔一段时间改变一次。“/”或者“\”会被当做目录分隔符。
maxHistory>:
可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件。假设设置每个月滚动,且maxHistory>是6,则只保存最近6个月的文件,删除之前的旧文件。注意,删除旧文件是,那些为了归档而创建的目录也会被删除。
FixedWindowRollingPolicy: 根据固定窗口算法重命名文件的滚动策略。有以下子节点:
minIndex>:窗口索引最小值
maxIndex>:窗口索引最大值,当用户指定的窗口过大时,会自动将窗口设置为12。
fileNamePattern >:
必须包含“%i”例如,假设最小值和最大值分别为1和2,命名模式为 mylog%i.log,会产生归档文件mylog1.log和mylog2.log。还可以指定文件压缩选项,例如,mylog%i.log.gz 或者 没有log%i.log.zip
triggeringPolicy:
SizeBasedTriggeringPolicy: 查看当前活动文件的大小,如果超过指定大小会告知RollingFileAppender 触发当前活动文件滚动。只有一个节点:
例如:每天生成一个日志文件,保存30天的日志文件。
Java代码
-
-
"FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> -
class ="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> -
logFile.%d{yyyy-MM-dd}.log -
30 -
-
%-4relative [%thread] %-5level %logger{35} - %msg%n -
"DEBUG" > -
"FILE" />
例如:按照固定窗口模式生成日志文件,当文件大于20MB时,生成新的日志文件。窗口大小是1到3,当保存了3个归档文件后,将覆盖最早的日志。
Xml代码
- configuration>
- appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- file>test.logfile>
- rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
- fileNamePattern>tests.%i.log.zipfileNamePattern>
- minIndex>1minIndex>
- maxIndex>3maxIndex>
- rollingPolicy>
- triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- maxFileSize>5MBmaxFileSize>
- triggeringPolicy>
- encoder>
- pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>
- encoder>
- appender>
- root level="DEBUG">
- appender-ref ref="FILE" />
- root>
- configuration>
4.另外还有SocketAppender、SMTPAppender、DBAppender、SyslogAppender、SiftingAppender,并不常用,这些就不在这里讲解了,大家可以参考官方文档。当然大家可以编写自己的Appender。
负责两件事,一是把日志信息转换成字节数组,二是把字节数组写入到输出流。
目前PatternLayoutEncoder 是唯一有用的且默认的encoder ,有一个
例如:
Xml代码
- encoder>
- pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>
- encoder
转换符 | 作用 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
c {length } lo {length } logger {length } |
输出日志的logger名,可有一个整形参数,功能是缩短logger名,设置为0表示只输入logger最右边点符号之后的字符串。
|
||||||||||||||||||||||||
C {length } class {length } |
输出执行记录请求的调用者的全限定名。参数与上面的一样。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
contextName cn |
输出上下文名称。 | ||||||||||||||||||||||||
d {pattern } date {pattern } |
输出日志的打印日志,模式语法与java.text.SimpleDateFormat 兼容。
|
||||||||||||||||||||||||
F / file | 输出执行记录请求的java源文件名。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
caller{depth}caller{depth, evaluator-1, ... evaluator-n} | 输出生成日志的调用者的位置信息,整数选项表示输出信息深度。
例如, %caller{2} 输出为: 0 [main] DEBUG - logging statement Caller+0 at mainPackage.sub.sample.Bar.sampleMethodName(Bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest(Bar.java:17) 例如, %caller{3} 输出为: 16 [main] DEBUG - logging statement Caller+0 at mainPackage.sub.sample.Bar.sampleMethodName(Bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest(Bar.java:17) Caller+2 at mainPackage.ConfigTester.main(ConfigTester.java:38) |
||||||||||||||||||||||||
L / line | 输出执行日志请求的行号。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
m / msg / message |
输出应用程序提供的信息。 |
||||||||||||||||||||||||
M / method | 输出执行日志请求的方法名。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||||
n | 输出平台先关的分行符“\n”或者“\r\n”。 | ||||||||||||||||||||||||
p / le / level | 输出日志级别。 | ||||||||||||||||||||||||
r / relative | 输出从程序启动到创建日志记录的时间,单位是毫秒 | ||||||||||||||||||||||||
t / thread | 输出产生日志的线程名。 | ||||||||||||||||||||||||
replace(p ){r, t} |
p 为日志内容,r 是正则表达式,将p 中符合r 的内容替换为t 。 例如, "%replace(%msg){'\s', ''}" |
格式修饰符,与转换符共同使用:
可选的格式修饰符位于“%”和转换符之间。
第一个可选修饰符是左对齐 标志,符号是减号“-”;接着是可选的最小宽度 修饰符,用十进制数表示。如果字符小于最小宽度,则左填充或右填充,默认是左填充(即右对齐),填充符为空格。如果字符大于最小宽度,字符永远不会被截断。最大宽度 修饰符,符号是点号"."后面加十进制数。如果字符大于最大宽度,则从前面截断。点符号“.”后面加减号“-”在加数字,表示从尾部截断。
例如:%-4relative 表示,将输出从程序启动到创建日志记录的时间 进行左对齐 且最小宽度为4。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.