Log4j2配置之Appender详解
Log4j2配置之Appender详解
Appender负责将日志事件传递到其目标。每个Appender都必须实现Appender接口。大多数Appender将扩展AbstractAppender,它添加了生命周期和可过滤的支持。生命周期允许组件在配置完成后完成初始化,并在关闭期间执行清理。Filterable允许组件附加过滤器,在事件处理期间对其进行评估。
Appender通常只负责将事件数据写入目标目标目标。在大多数情况下,它们将格式化事件的责任委托给布局。一些appender包装其他Appender,以便它们可以修改logevent、处理Appender中的故障、根据高级筛选条件将事件路由到下级appender,或者提供类似的功能,这些功能不会直接格式化事件以供查看。
Appender总是有一个名称,以便可以从记录器引用它们。
在下面的表中,“类型”列对应于预期的Java类型。对于非jdk类,除非另有说明,否则这些类通常应该在log4j内核中。
1.常用的Appender
1.ConsoleAppender
如人们所料,consoleappender将其输出写入system.out或system.err,其中system.out是默认目标。必须提供布局以格式化日志事件。
| Parameter Name | Type | Description |
| filter | Filter | 用于确定事件是否应由此Appender处理的筛选器。使用CompositeFilter可以使用多个筛选器。 |
| layout | Layout | Layout用于格式化日志事件。如果未提供Layout,则将使用默认的模式布局“%m%n”。 |
| follow | Boolean | Appender是否接受在配置后通过System.setOut或System.setErr重新分配的System.out或System.err。请注意,follow属性不能用于windows上的Jansi。不能与direct一起使用。 |
| direct | Boolean | 直接写入java.io.FieldDebug,并绕过java.lang.System.out./err。当输出被重定向到文件或其他进程时,可以放弃高达10倍的性能提升。不能与Windows上的Jansi一起使用。不能与follow一起使用。输出不尊重 java.lang.System.setOut()/.setErr(),可能会与多线程应用程序中的java.lang.System.out./err的其他输出纠缠在一起。从2.6.2开始。请注意,这是一个新的添加,到目前为止,它只在linux和windows上使用oracle jvm进行了测试。 |
| name | String | Appender的名字 |
| ignoreExceptions | Boolean | 默认值为true,导致在将事件附加到内部日志中时遇到异常,然后将其忽略。当设置为false时,异常将传播到调用方。在将此附加程序包装为FailoverAppender时,必须将此设置为false。 |
| target | String |
值为 "SYSTEM_OUT" 或者 "SYSTEM_ERR". 默认值为 "SYSTEM_OUT". |
A typical Console configuration might look like:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
2.FileAppender
FileAppender是一个OutputStreamAppender,它写入fileName参数中指定的文件。FileAppender使用FileManager(它继承了OutputStreamAppender)来实际执行文件I/O。虽然来自不同配置的FileAppender无法共享,但如果管理器可访问,则FileAppender可以共享。例如,如果log4j位于它们共同的类加载器中,则servlet容器中的两个web应用程序可以有自己的配置并安全地写入同一文件。
| Parameter Name | Type | Description |
| append | boolean | When true - the default, records will be appended to the end of the file. When set to false, the file will be cleared before new records are written. |
| bufferedIO | boolean | When true - the default, records will be written to a buffer and the data will be written to disk when the buffer is full or, if immediateFlush is set, when the record is written. File locking cannot be used with bufferedIO. Performance tests have shown that using buffered I/O significantly improves performance, even if immediateFlush is enabled. |
| bufferSize | int | When bufferedIO is true, this is the buffer size, the default is 8192 bytes. |
| createOnDemand | boolean | The appender creates the file on-demand. The appender only creates the file when a log event passes all filters and is routed to this appender. Defaults to false. |
| filter | Filter | A Filter to determine if the event should be handled by this Appender. More than one Filter may be used by using a CompositeFilter. |
| fileName | String | The name of the file to write to. If the file, or any of its parent directories, do not exist, they will be created. |
| immediateFlush | boolean |
When set to true - the default, each write will be followed by a flush. This will guarantee the data is written to disk but could impact performance. Flushing after every write is only useful when using this appender with synchronous loggers. Asynchronous loggers and appenders will automatically flush at the end of a batch of events, even if immediateFlush is set to false. This also guarantees the data is written to disk but is more efficient. |
| layout | Layout | The Layout to use to format the LogEvent. If no layout is supplied the default pattern layout of "%m%n" will be used. |
| locking | boolean | When set to true, I/O operations will occur only while the file lock is held allowing FileAppenders in multiple JVMs and potentially multiple hosts to write to the same file simultaneously. This will significantly impact performance so should be used carefully. Furthermore, on many systems the file lock is "advisory" meaning that other applications can perform operations on the file without acquiring a lock. The default value is false. |
| name | String | The name of the Appender. |
| ignoreExceptions | boolean | The default is true, causing exceptions encountered while appending events to be internally logged and then ignored. When set to false exceptions will be propagated to the caller, instead. You must set this to false when wrapping this Appender in aFailoverAppender. |
| filePermissions | String |
File attribute permissions in POSIX format to apply whenever the file is created. Underlying files system shall support POSIX file attribute view. Examples: rw------- or rw-rw-rw- etc... |
| fileOwner | String |
File owner to define whenever the file is created. Changing file's owner may be restricted for security reason and Operation not permitted IOException thrown. Only processes with an effective user ID equal to the user ID of the file or with appropriate privileges may change the ownership of a file if _POSIX_CHOWN_RESTRICTED is in effect for path. Underlying files system shall support file owner attribute view. |
| fileGroup | String |
File group to define whenever the file is created. Underlying files system shall support POSIX file attribute view. |
Here is a sample File configuration:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<File name="MyFile" fileName="logs/app.log">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="MyFile"/>
</Root>
</Loggers>
</Configuration>
Log4j2配置之Appender详解的更多相关文章
- CentOS 6.3下Samba服务器的安装与配置方法(图文详解)
这篇文章主要介绍了CentOS 6.3下Samba服务器的安装与配置方法(图文详解),需要的朋友可以参考下 一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件, ...
- MYSQL服务器my.cnf配置文档详解
MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...
- Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解
http://hi.baidu.com/ltb6w/item/3a51f11926fda60ce75c361d Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解 ...
- webpack安装配置使用教程详解
webpack安装配置使用教程详解 www.111cn.net 更新:2015-09-01 编辑:swteen 来源:转载 本文章来为各位详细的介绍一下关于webpack安装配置使用教程吧,这篇文章对 ...
- OpenVPN下载、安装、配置及使用详解
OpenVPN下载.安装.配置及使用详解 OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可 ...
- MySql绿色版配置及使用详解
原文:MySql绿色版配置及使用详解 最近在做项目开发时用到了MySql数据库,在看了一些有关MySql的文章后,很快就上手使用了.在使用的过程中还是出现了一些问题,因为使用的是绿色免安装版的MySq ...
- Linux NFS服务器的安装与配置方法(图文详解)
这篇文章主要介绍了Linux NFS服务器的安装与配置方法(图文详解),需要的朋友可以参考下(http://xb.xcjl0834.com) 一.NFS服务简介 NFS 是Network File S ...
- Linux中redis安装配置及使用详解
Linux中redis安装配置及使用详解 一. Redis基本知识 1.Redis 的数据类型 字符串 , 列表 (lists) , 集合 (sets) , 有序集合 (sorts sets) , 哈 ...
- 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)
不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...
随机推荐
- 初次接触python,怎么样系统的自学呢?
关注专栏 写文章登录 给伸手党的福利:Python 新手入门引导 Crossin 2 个月前 这是一篇 Python 入门指南,针对那些没有任何编程经验,从零开始学习 Python 的同学.不管你 ...
- 初次使用自己写的testbench 验证了简单的NOT门。
先是简单的非门模型: module notgate(a,b); input a; output b; assign b=~a; endmodule 下面是自己写的简陋的testbench: `time ...
- 用java写一个两个任意长度字符串数字和的算法
package com.cn.test.string; public class StringTest { public static void main(String[] args) { Strin ...
- Linux设备驱动程序 之 异步通知
尽管大多数时候阻塞型和非阻塞型操作的组合以及select方法可以有效的查询设备,但是某些时候用这种技术处理就效率不搞了: 例如:一个进程在低优先级执行长的循环计算,但又需要尽可能快的处理输入数据,如果 ...
- QT .pro文件中的变量说明
https://blog.csdn.net/tanou3212/article/details/79942840 TEMPLATE:定义了工程的编译模式 赋值方式为:TEMPLATE=app (1 ...
- mysql数据库索引和引擎
1. 数据库索引 1.1 索引作用 当我们在数据库表中查询数据时,若没有索引,会逐个遍历表格中的所有记录,表格中数据记录量大时很耗时.建立索引就像创建目录一样,直接通过索引找到数据存储位置,加快查找. ...
- mac中的word内容丢失
改了一晚上好不容易快搞完了,结果1万字的内容丢了,并且不知道自己当时怎么想的还清理了回收站 还是用mac自带的工具吧,同时代码也要及时上传github
- MybatisUtil工具类的作用
1)在静态初始化块中加载mybatis配置文件和StudentMapper.xml文件一次 2)使用ThreadLocal对象让当前线程与SqlSession对象绑定在一起 3)获取当前线程中的Sql ...
- 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型
1) 在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action 中自定义类型转换器 <form action="${pa ...
- Swift开源parser
https://www.prowidesoftware.com/products/core https://github.com/prowide/prowide-core-examples/blob/ ...