- 浏览: 147003 次
- 性别:
- 来自: 北京
文章分类
最新评论
logback 常用配置详解(二)
<appender>:
<appender>是<configuration>的子节点,是负责写日志的组件。
<appender>有两个必要属性name和class。name指定appender名称,class指定appender的全限定名。
1.ConsoleAppender:
把日志添加到控制台,有以下子节点:
<encoder>:对日志进行格式化。(具体参数稍后讲解)
<target>:字符串System.out或者<wbr><em>System.err</em>,默认<em>System.out</em>;</wbr>
例如:
Xml代码:
- <configuration><wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">name</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"STDOUT"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.ConsoleAppender"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">%-4relative<wbr>[%thread]<wbr>%-5level<wbr>%logger{35}<wbr>-<wbr>%msg<wbr>%n</wbr></wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">level</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"DEBUG"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender-ref</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">ref</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"STDOUT"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">/></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- </configuration><wbr><wbr><wbr></wbr></wbr></wbr>
2.FileAppender:
把日志添加到文件,有以下子节点:
<file>:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。
<append>:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。
<encoder>:对记录事件进行格式化。(具体参数稍后讲解)
<prudent>:如果是 true,日志会被安全的写入文件,即使其他的FileAppender也在向此文件做写入操作,效率低,默认是 false。
例如:
Xml代码:
- <configuration><wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">name</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.FileAppender"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">file</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">testFile.log</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">file</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">append</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">true</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">append</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">%-4relative<wbr>[%thread]<wbr>%-5level<wbr>%logger{35}<wbr>-<wbr>%msg%n</wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">level</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"DEBUG"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender-ref</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">ref</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">/></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr></wbr></wbr></wbr></span></wbr></wbr>
- </configuration><wbr><wbr><wbr></wbr></wbr></wbr>
3.RollingFileAppender:
滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件。有以下子节点:
<file>:被写入的文件名,可以是相对目录,也可以是绝对目录,如果上级目录不存在会自动创建,没有默认值。
<append>:如果是 true,日志被追加到文件结尾,如果是 false,清空现存文件,默认是true。
<encoder>:对记录事件进行格式化。(具体参数稍后讲解)
<rollingPolicy>:当发生滚动时,决定RollingFileAppender的行为,涉及文件移动和重命名。
<triggeringPolicy>:告知RollingFileAppender合适激活滚动。
<prudent>:当为true时,不支持FixedWindowRollingPolicy<wbr>。支持TimeBasedRollingPolicy,但是有两个限制,1不支持也不允许文件压缩,2不能设置file属性,必须留空。</wbr>
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<wbr>:</wbr>根据固定窗口算法重命名文件的滚动策略。有以下子节点:
<minIndex>:窗口索引最小值
<maxIndex>:窗口索引最大值,当用户指定的窗口过大时,会自动将窗口设置为12。
<fileNamePattern>:
必须包含“%i”例如,假设最小值和最大值分别为1和2,命名模式为 mylog%i.log,会产生归档文件mylog1.log和mylog2.log。还可以指定文件压缩选项,例如,mylog%i.log.gz 或者 没有log%i.log.zip
triggeringPolicy:
SizeBasedTriggeringPolic<wbr>y:</wbr>查看当前活动文件的大小,如果超过指定大小会告知RollingFileAppender触发当前活动文件滚动。只有一个节点:
<maxFileSize>:这是活动文件的大小,默认值是10MB。
例如:每天生成一个日志文件,保存30天的日志文件。
Java代码:
- <configuration><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><appender<wbr>name=<span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.rolling.RollingFileAppender"</span><span style="word-wrap:normal; word-break:normal">><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><rollingPolicy<wbr><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.rolling.TimeBasedRollingPolicy"</span><span style="word-wrap:normal; word-break:normal">><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><maxHistory><span style="word-wrap:normal; word-break:normal">30</span><span style="word-wrap:normal; word-break:normal"></maxHistory><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></rollingPolicy><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><encoder><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><pattern>%-4relative<wbr>[%thread]<wbr>%-5level<wbr>%logger{<span style="word-wrap:normal; word-break:normal">35</span><span style="word-wrap:normal; word-break:normal">}<wbr>-<wbr>%msg%n</pattern><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></encoder><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></appender><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><root<wbr>level=<span style="word-wrap:normal; word-break:normal">"DEBUG"</span><span style="word-wrap:normal; word-break:normal">><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><appender-ref<wbr>ref=<span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr>/><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr></root><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- </configuration><wbr><wbr><wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr>例如:按照固定窗口模式生成日志文件,当文件大于20MB时,生成新的日志文件。窗口大小是1到3,当保存了3个归档文件后,将覆盖最早的日志。</wbr>
Xml代码:
- <configuration><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">name</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.rolling.RollingFileAppender"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">file</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">test.log</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">file</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">rollingPolicy</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.rolling.FixedWindowRollingPolicy<wbr>"</wbr></span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">fileNamePattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">tests.%i.log.zip</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">fileNamePattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">minIndex</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">1</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">minIndex</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">maxIndex</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">3</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">maxIndex</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">rollingPolicy</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">triggeringPolicy</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">class</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"ch.qos.logback.core.rolling.SizeBasedTriggeringPolic<wbr>y"</wbr></span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">maxFileSize</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">5MB</span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">maxFileSize</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">triggeringPolicy</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">%-4relative<wbr>[%thread]<wbr>%-5level<wbr>%logger{35}<wbr>-<wbr>%msg%n</wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">encoder</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">appender</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">level</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"DEBUG"</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr>
- <wbr><wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">appender-ref</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">ref</span><span style="word-wrap:normal; word-break:normal">=</span><span style="word-wrap:normal; word-break:normal">"FILE"</span><span style="word-wrap:normal; word-break:normal"><wbr></wbr></span><span style="word-wrap:normal; word-break:normal">/></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr></wbr>
- <wbr><wbr><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">root</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr>
- </configuration><wbr><wbr><wbr></wbr></wbr></wbr>
4.另外还有SocketAppender、SMTPAppender、DBAppender、SyslogAppender、SiftingAppender,并不常用,这些就不在这里讲解了,大家可以参考官方文档。当然大家可以编写自己的Appender。
<encoder>:
负责两件事,一是把日志信息转换成字节数组,二是把字节数组写入到输出流。
目前PatternLayoutEncoder是唯一有用的且默认的encoder,有一个<pattern>节点,用来设置日志的输入格式。使用“%”加“转换符”方式,如果要输出“%”,则必须用“\”对“\%”进行转义。
例如:
Xml代码:
- <encoder><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr>
- <wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal"><</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal">%-4relative<wbr>[%thread]<wbr>%-5level<wbr>%logger{35}<wbr>-<wbr>%msg%n</wbr></wbr></wbr></wbr></wbr></span><span style="word-wrap:normal; word-break:normal"></</span><span style="word-wrap:normal; word-break:normal">pattern</span><span style="word-wrap:normal; word-break:normal">></span><span style="word-wrap:normal; word-break:normal"><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></span></wbr></wbr></wbr>
- </encoder<wbr><wbr><wbr></wbr></wbr></wbr>
<wbr></wbr>
<pattern>里面的转换符说明:
转换符 作用
c{length} lo{length} logger{length} |
输出日志的logger名,可有一个整形参数,功能是缩短logger名,设置为0表示只输入logger最右边点符号之后的字符串。 Conversion specifier Logger name Result
<wbr></wbr> |
||||||||||||||||||||||
C{length} class{length} |
输出执行记录请求的调用者的全限定名。参数与上面的一样。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||
contextName cn |
输出上下文名称。 | ||||||||||||||||||||||
d{pattern} date{pattern} |
输出日志的打印日志,模式语法与java.text.SimpleDateFormat 兼容。 Conversion Pattern Result
|
||||||||||||||||||||||
F / file | 输出执行记录请求的java源文件名。尽量避免使用,除非执行速度不造成任何问题。 | ||||||||||||||||||||||
caller{depth}caller{depth, evaluator-1, ... evaluator-n} |
输出生成日志的调用者的位置信息,整数选项表示输出信息深度。
例如,�ller{2}<wbr>输出为:</wbr> 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) 例如,�ller{3}<wbr>输出为:</wbr> 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。
相关推荐
SLF4J(Simple Logging Facade for Java)和Logback是两个常用的日志处理框架,它们提供了高效且灵活的日志解决方案。SLF4J作为一个抽象层,允许开发者选择底层的日志实现,而Logback则是一个优秀的SLF4J实现。在...
SpringBoot之LogBack配置详解 LogBack是基于Slf4j的日志框架,默认集成在Spring Boot中。默认情况下,Spring Boot是以INFO级别输出到控制台。LogBack的日志级别是:ALL 。 配置LogBack可以直接在application....
本篇文章主要介绍了基于Spring Boot的Logback日志轮转配置详解,包括了日志的重要性、默认配置、独立配置文件配置、logback.xml文件的配置等内容。 日志的重要性: 在生产环境下,日志是最好的问题调试和跟踪方法...
`logback logback_xml常用配置详解(一).mht、logback logback_xml常用配置详解(二).mht、logback logback_xml常用配置详解(三).mht`这三份文档可能是关于Logback配置的详细教程,涵盖了多个方面: 1. **配置...
Java 中 Log4j 日志级别配置详解 Log4j 是 Java 中最常用的日志记录工具之一,通过配置 Log4j,可以实现灵活的日志记录和管理。下面将详细介绍 Log4j 的日志级别配置。 1. Log4j 的发展历史 Log4j 的历史可以追溯...
5. **配置详解** - 在Log4j和Logback的配置文件中,我们可以设置全局日志级别、定义Appender(日志输出目的地)和Layout(日志格式)。例如,我们可以配置一个ConsoleAppender将日志输出到控制台,一个FileAppender...
### Hibernate 日志配置详解 1. **日志框架选择**:Hibernate 默认支持多种日志框架,如 Log4j、Logback 和 JDK 内建的日志。选择合适的日志框架主要取决于项目的需求和个人偏好。Log4j 和 Logback 因其灵活性和...
#### 二、配置文件的基本结构 配置文件可以采用多种格式,如XML或Java properties文件。在实践中,XML文件因其灵活性而被广泛使用,但properties文件也常用于简单的配置场景。配置文件通常包含以下关键元素: - **...
### SpringBoot入门配置详解 #### 一、新建SpringBoot项目 在开始开发SpringBoot应用之前,首先需要创建一个新的SpringBoot项目。SpringBoot简化了项目的搭建过程,提供了多种方式来创建项目,包括使用IDE(集成...
9. **配置文件详解**:`applicationContext.xml`中,我们需要配置Bean、事务管理器、数据源以及Spring与其他组件的集成;`servlet-context.xml`中,主要配置SpringMVC的视图解析器、处理器映射器、拦截器等;`...
1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 ...
- **步骤二**:配置项目的基本信息,如项目名称、项目描述等,并选择需要的依赖(例如Web、Thymeleaf等)。 - **步骤三**:编写Controller、Service、Repository等组件,实现业务逻辑。 - **示例代码**:创建一个...
本教程的PDF文件可能包含了SpringBoot的基础概念、配置详解、实战案例等内容,覆盖了从入门到进阶的各个方面。通过学习这些材料,你应该能够熟练地运用SpringBoot进行Java应用开发,提高开发效率,同时享受到...
# 1 项目搭建 ...- [1.1.2 常用配置介绍]...- 2.1.5 [日志规范-logback配置详解]...
本文将深入讲解Rhapsody 7.0的安装配置、重置密码、日志分析、内存调整、端口修改、数据库连接以及IDE编辑器的常用功能。 1. **安装与配置** - JVM内存建议:为了保证Rhapsody的稳定运行,建议初始设置JVM内存为2...
**SSM框架整合详解** SSM框架是Java Web开发中常用的三大组件——Spring、Struts2和MyBatis的组合,常用于构建大型企业级应用。在这个项目中,`IntelliJ IDEA`作为强大的Java集成开发环境,被用来进行整个项目的...
3. **自动配置详解**:解释自动配置的工作原理和如何自定义配置。 4. **Web开发**:讲解如何使用Spring Boot进行Web开发,包括RESTful API的设计和模板引擎的使用。 5. **数据访问**:涵盖JPA和Hibernate的使用,...
这份文档可能涵盖了SpringBoot的使用示例、配置详解以及常见问题解答等内容,对于深化理解SpringBoot的实战运用大有裨益。 总之,SpringBoot以其简洁、高效的特点深受开发者喜爱。通过这个基础教程,你将能够快速...
它集成了大量常用的第三方库配置,如数据库、邮件服务、缓存、消息队列等,可以极大地提高开发效率。 二、Spring Boot核心特性 1. 自动配置:Spring Boot会根据项目依赖自动配置相关组件,比如引入了Spring Data ...
### Spring Boot 常见面试问题详解 #### 1. 什么是 Spring Boot? Spring Boot 是 Spring 开源组织下的一个子项目,它提供了 Spring 组件的一站式解决方案,旨在简化 Spring 应用的创建、配置和管理。通过减少大量...