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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM