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

 

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>

 

posted on 2019-09-17 16:15  _ZXP  阅读(9889)  评论(0编辑  收藏  举报

导航