search
HomeDatabaseMysql Tutoriallogback配置详解2<appender>

logback配置详解2<appender>

Jun 07, 2016 pm 03:38 PM
amploglogbackDetailed explanationConfiguration

logback 常用配置详解(二) appender appender: appender是configuration的子节点,是负责写日志的组件。 appender有两个必要属性name和class。name指定appender名称,class指定appender的全限定名。 1.ConsoleAppender: 把日志添加到控制台,有以下子节点

logback 常用配置详解(二) 

 

logback配置详解2<appender>

的子节点,是负责写日志的组件。

有两个必要属性name和class。name指定appender名称,class指定appender的全限定名。

 

1.ConsoleAppender:

把日志添加到控制台,有以下子节点:

:对日志进行格式化。(具体参数稍后讲解 )

:字符串 System.out 或者 System.err ,默认 System.out ;

例如:

Xml代码  

  1. configuration>  
  2.   
  3.   appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
  4.     encoder>  
  5.       pattern>%-4relative [%thread] %-5level %logger{35} - %msg %npattern>  
  6.     encoder>  
  7.   appender>  
  8.   
  9.   root level="DEBUG">  
  10.     appender-ref ref="STDOUT" />  
  11.   root>  
  12. configuration>  

 

2.FileAppender:

把日志添加到文件,有以下子节点:

:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。

:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。

:对记录事件进行格式化。(具体参数稍后讲解 )

:如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。

例如:

Xml代码  

  1. configuration>  
  2.   
  3.   appender name="FILE" class="ch.qos.logback.core.FileAppender">  
  4.     file>testFile.logfile>  
  5.     append>trueappend>  
  6.     encoder>  
  7.       pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>  
  8.     encoder>  
  9.   appender>  
  10.           
  11.   root level="DEBUG">  
  12.     appender-ref ref="FILE" />  
  13.   root>  
  14. configuration>  


3.RollingFileAppender:

滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。有以下子节点:

:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。

:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。

:对记录事件进行格式化。(具体参数稍后讲解 )

:当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名。

triggeringPolicy >: 告知 RollingFileAppender 合适激活滚动。

:当为true时,不支持FixedWindowRollingPolicy。支持TimeBasedRollingPolicy,但是有两个限制,1不支持也不允许文件压缩,2不能设置file属性,必须留空。

 

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 触发当前活动文件滚动。只有一个节点:

:这是活动文件的大小,默认值是10MB。

 

 

 

例如:每天生成一个日志文件,保存30天的日志文件。

 

Java代码  

  1.    
  2.   "FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
  3.       
  4.     class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">   
  5.       logFile.%d{yyyy-MM-dd}.log   
  6.       30    
  7.        
  8.    
  9.        
  10.       %-4relative [%thread] %-5level %logger{35} - %msg%n   
  11.        
  12.       
  13.    
  14.   "DEBUG">   
  15.     "FILE" />   
  16.      
  17.   

 

  例如:按照固定窗口模式生成日志文件,当文件大于20MB时,生成新的日志文件。窗口大小是1到3,当保存了3个归档文件后,将覆盖最早的日志。

Xml代码  

  1. configuration>   
  2.   appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">   
  3.     file>test.logfile>   
  4.    
  5.     rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">   
  6.       fileNamePattern>tests.%i.log.zipfileNamePattern>   
  7.       minIndex>1minIndex>   
  8.       maxIndex>3maxIndex>   
  9.     rollingPolicy>   
  10.    
  11.     triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">   
  12.       maxFileSize>5MBmaxFileSize>   
  13.     triggeringPolicy>   
  14.     encoder>   
  15.       pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>   
  16.     encoder>   
  17.   appender>   
  18.            
  19.   root level="DEBUG">   
  20.     appender-ref ref="FILE" />   
  21.   root>   
  22. configuration>  

 

4.另外还有SocketAppender、SMTPAppender、DBAppender、SyslogAppender、SiftingAppender,并不常用,这些就不在这里讲解了,大家可以参考官方文档。当然大家可以编写自己的Appender。

 

 

负责两件事,一是把日志信息转换成字节数组,二是把字节数组写入到输出流。

目前PatternLayoutEncoder 是唯一有用的且默认的encoder ,有一个节点,用来设置日志的输入格式。使用“%”加“转换符”方式,如果要输出“%”,则必须用“\”对“\%”进行转义。

例如:

Xml代码  

  1. encoder>   
  2.    pattern>%-4relative [%thread] %-5level %logger{35} - %msg%npattern>   
  3. encoder  

 

里面的转换符说明:

 

转换符 作用
c {length } 
lo {length } 
logger {length } 
输出日志的logger名,可有一个整形参数,功能是缩短logger名,设置为0表示只输入logger最右边点符号之后的字符串。
Conversion specifier Logger name Result
%logger mainPackage.sub.sample.Bar mainPackage.sub.sample.Bar
%logger{0} mainPackage.sub.sample.Bar Bar
%logger{5} mainPackage.sub.sample.Bar m.s.s.Bar
%logger{10} mainPackage.sub.sample.Bar m.s.s.Bar
%logger{15} mainPackage.sub.sample.Bar m.s.sample.Bar
%logger{16} mainPackage.sub.sample.Bar m.sub.sample.Bar
%logger{26} mainPackage.sub.sample.Bar mainPackage.sub.sample.Bar

 

C {length } 
class {length } 
输出执行记录请求的调用者的全限定名。参数与上面的一样。尽量避免使用,除非执行速度不造成任何问题。
contextName 
cn
 
输出上下文名称。
d {pattern } 
date {pattern } 
输出日志的打印日志,模式语法与java.text.SimpleDateFormat 兼容。
Conversion Pattern Result
%d 2006-10-20 14:06:49,812
%date 2006-10-20 14:06:49,812
%date{ISO8601} 2006-10-20 14:06:49,812
%date{HH:mm:ss.SSS} 14:06:49.812
%date{dd MMM yyyy ;HH:mm:ss.SSS} 20 oct. 2006;14:06:49.812
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。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

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.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

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.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

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

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

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 vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

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.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

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.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

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.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

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

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.