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是默认目标。必须提供布局以格式化日志事件。

ConsoleAppender Parameters
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应用程序可以有自己的配置并安全地写入同一文件。

FileAppender Parameters
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详解的更多相关文章

  1. CentOS 6.3下Samba服务器的安装与配置方法(图文详解)

    这篇文章主要介绍了CentOS 6.3下Samba服务器的安装与配置方法(图文详解),需要的朋友可以参考下   一.简介  Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件, ...

  2. MYSQL服务器my.cnf配置文档详解

    MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...

  3. Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解

    http://hi.baidu.com/ltb6w/item/3a51f11926fda60ce75c361d Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解 ...

  4. webpack安装配置使用教程详解

    webpack安装配置使用教程详解 www.111cn.net 更新:2015-09-01 编辑:swteen 来源:转载 本文章来为各位详细的介绍一下关于webpack安装配置使用教程吧,这篇文章对 ...

  5. OpenVPN下载、安装、配置及使用详解

    OpenVPN下载.安装.配置及使用详解   OpenVPN简介 OpenVPN是一个用于创建虚拟专用网络(Virtual Private Network)加密通道的免费开源软件.使用OpenVPN可 ...

  6. MySql绿色版配置及使用详解

    原文:MySql绿色版配置及使用详解 最近在做项目开发时用到了MySql数据库,在看了一些有关MySql的文章后,很快就上手使用了.在使用的过程中还是出现了一些问题,因为使用的是绿色免安装版的MySq ...

  7. Linux NFS服务器的安装与配置方法(图文详解)

    这篇文章主要介绍了Linux NFS服务器的安装与配置方法(图文详解),需要的朋友可以参考下(http://xb.xcjl0834.com) 一.NFS服务简介 NFS 是Network File S ...

  8. Linux中redis安装配置及使用详解

    Linux中redis安装配置及使用详解 一. Redis基本知识 1.Redis 的数据类型 字符串 , 列表 (lists) , 集合 (sets) , 有序集合 (sorts sets) , 哈 ...

  9. 全网最详细的Windows系统里PLSQL Developer 64bit安装之后的一些配置(图文详解)

    不多说,直接上干货! 注意的是: 本地若没有安装Oracle服务端,Oracle server服务端64位,是远程连接,因此本地配置PLSQL Developer64位. PLSQL Develope ...

随机推荐

  1. deepin Linux 安装+工作学习配置

    一 安装 在官网下载 U盘安装,神舟优雅x4开机按F7,选择U盘启动. U盘安装器在下载的镜像文件中. 二 配置 升级最新系统 设置root用户密码: dawn@dawn-PC:~$ sudo pas ...

  2. 测试Promise与Async/await的基本使用

    想在项目中用, 发现自己不是很熟 promise基本使用 基本使用-思路 new Promise()返回了一个状态机 一个完全无法被外界影响的状态机 构造函数, 传入一个函数, 两个参数, 分别是re ...

  3. CF1207A

    CF1207A-There Are Two Types Of Burgers 题意: 出售普通汉堡和鸡肉汉堡,并且两种汉堡所需的原材料价格不同,问最多能卖多少钱. 解法: 对于这道题,我们优先考虑先卖 ...

  4. SAE上配置Django静态文件

    很简单,步骤如下: 1.修改配置文件 setting.py 中的STATIC_ROOT为 '/static/' 2. 运行 python manage.py collectstatic , 将静态文件 ...

  5. Mongodb内存管理和使用情况查询

    overview MongoDB使用的是内存映射存储引擎,即Memory Mapped Storage Engine,简称MMAP.MMAP可以把磁盘文件的一部分或全部内容直接映射到内存,这样文件中的 ...

  6. 论一种基于JS技术的WEB前端动态生成框图的方法

    前言 HTML是一种标记语言,由HTML的标签元素和文本编写的文档可被浏览器描述为一幅网页.通常情况下网页的实现是由HTML.CSS和Javascript三者结合完成的,HTML负责网页的结构,CSS ...

  7. Nginx-HTTP之静态网页访问流程分析一

    假设访问静态网页的配置如下: worker_processes 1; error_log stderr debug; daemon off; master_process on; events { w ...

  8. 从源码看 Vue 中的 Mixin

    最近在做项目的时候碰到了一个奇怪的问题,通过 Vue.mixin 方法注入到 Vue 实例的一个方法不起作用了,后来经过仔细排查发现这个实例自己实现了一个同名方法,导致了 Vue.mixin 注入方法 ...

  9. Introduction to statistical learning:with Applications in R (书,数据,R代码,链接)

    http://faculty.marshall.usc.edu/gareth-james/ http://faculty.marshall.usc.edu/gareth-james/ISL/

  10. sql拼接中的小错误

    字符串类型变量拼接到sql字符串上,容易忘记添加单引号,使用jdbcTemplate执行,报如下错误 正确写法如下: