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 ...
随机推荐
- axios 是如何封装 HTTP 请求的
原载于 TutorialDocs 网站的文章<How to Implement an HTTP Request Library with Axios>.译者:zhangbao90shttp ...
- 如何知道,当前redis实例是处于阻塞状态?
随便get一个key,然后卡着不动就行,简单粗暴.优雅一点是看latency的延迟,blocked_clients的数量,rejected_connections的数量等 或者 方法一:登录 Redi ...
- Java 多线程:什么是线程安全性
线程安全性 什么是线程安全性 <Java Concurrency In Practice>一书的作者 Brian Goetz 是这样描述"线程安全"的:"当多 ...
- 带有时间间隔的dp
Uberwatch 题意:一个人打一群敌人,每间隔时间m能释放一次大招,消灭这个时刻上的所有敌人,起始时刻开始计算冷却时间 solution: dp[i]=max(dp[i],dp[i-m]); /* ...
- LeetCode 279. 完全平方数(Perfect Squares)
题目描述 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解释 ...
- ZT:在mybatis的Mapping文件写入表名 出现异常ORA-00903: 表名无效 的解决
简而言之,把#{tablename}换成${tablename}就能解决问题. 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:htt ...
- C++ STL——常用算法
目录 一 常用查找算法 二 常用遍历算法 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 常用查找算法 /* find算法 查找元素 @param ...
- 启动eclipse导致Tomcat的配置文件重置
转: 启动eclipse导致Tomcat的配置文件重置 导入一个项目,需要在Tomcat的配置文件中配置JNDI数据源,需要修改Tomcat下的server.xml文件.但是当我们修改完后重启Tomc ...
- 转 MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
http://blog.sina.com.cn/s/blog_637e04c9010117ri.html 1 问题 [root@localhost mysql]# /etc/rc.d/init.d/m ...
- Collection Map Java数据结构
Collection Map 框架图 Collection 接口的接口 对象的集合 ├ List 子接口 按进入先后有序保存 可 ...