log4j2.x官方文档翻译(一)

最近springboot 1.4项目中用到了log4j2这么个东东,不知道怎么配置就从apache官网找了log4j2的官方文档来看,但是都是英文的,由于自己英文不好,因此就用google翻译了出来,一来是自己看看学习,二来对有需要的同学提供个帮助,不正确的地方欢迎指出。

原文地址:http://logging.apache.org/log4j/2.x/manual/appenders.html

Appenders

Appenders are responsible for delivering LogEvents to their destination. Every Appender must implement the Appender interface. Most Appenders will extend AbstractAppender which adds Lifecycle and Filterable support. Lifecycle allows components to finish initialization after configuration has completed and to perform cleanup during shutdown. Filterable allows the component to have Filters attached to it which are evaluated during event processing.

应用程序负责将LogEvent传递到其目标。 每个Appender必须实现Appender接口。 大多数Appenders将扩展AbstractAppender,它增加了Lifecycle和Filterable支持。 生命周期允许组件在配置完成后完成初始化,并在关闭期间执行清理。 可过滤允许组件具有附加到它的过滤器,它们在事件处理期间被评估。

Appenders usually are only responsible for writing the event data to the target destination. In most cases they delegate responsibility for formatting the event to a layout. Some appenders wrap other appenders so that they can modify the LogEvent, handle a failure in an Appender, route the event to a subordinate Appender based on advanced Filter criteria or provide similar functionality that does not directly format the event for viewing.

附加器通常只负责将事件数据写入目标目标。 在大多数情况下,他们委托将事件格式化为布局的职责。 一些追加器包装其他追加器,以便他们可以修改LogEvent,处理Appender中的失败,根据高级过滤器条件将事件路由到下级Appender,或提供不直接格式化事件以供查看的类似功能。

Appenders always have a name so that they can be referenced from Loggers.

附加器始终具有名称,以便可以从日志记录器引用它们。

AsyncAppender

The AsyncAppender accepts references to other Appenders and causes LogEvents to be written to them on a separate Thread. Note that exceptions while writing to those Appenders will be hidden from the application. The AsyncAppender should be configured after the appenders it references to allow it to shut down properly.

AsyncAppender接受对其他Appender的引用,并导致LogEvents在单独的线程上写入它们。 请注意,写入这些Appenders时的异常将会从应用程序中隐藏。 AsyncAppender应该在其引用的appender之后配置,以允许它正常关闭。

By default, AsyncAppender uses java.util.concurrent.ArrayBlockingQueue which does not require any external libraries. Note that multi-threaded applications should exercise care when using this appender as such: the blocking queue is susceptible to lock contention and our tests showed performance may become worse when more threads are logging concurrently. Consider using lock-free Async Loggers for optimal performance.

默认情况下,AsyncAppender使用java.util.concurrent.ArrayBlockingQueue,它不需要任何外部库。 请注意,多线程应用程序在使用此应用程序时应小心谨慎:阻塞队列容易发生锁争用,我们的测试表明,当更多线程并发记录时,性能可能会变差。 考虑使用无锁的异步记录器以获得最佳性能。

AsyncAppender Parameters

Parameter Name Type Description 中文翻译
AppenderRef String The name of the Appenders to invoke asynchronously. Multiple AppenderRef elements can be configured. 要异步调用的Appender的名称。 可以配置多个Appender参考元素。
blocking boolean If true, the appender will wait until there are free slots in the queue. If false, the event will be written to the error appender if the queue is full. The default is true. 如果为true,则appender将等待,直到队列中有空闲插槽。 如果为false,则如果队列已满,事件将写入错误追加器。 默认值为true。
shutdownTimeout integer How many milliseconds the Appender should wait to flush outstanding log events in the queue on shutdown. The default is zero which means to wait forever. 在关闭时,Appender应在等待队列中清除未完成的日志事件多少毫秒。 默认为零,这意味着永远等待。
bufferSize integer Specifies the maximum number of events that can be queued. The default is 128. Note that when using a disruptor-style BlockingQueue, this buffer size must be a power of 2. 指定可以排队的事件的最大数量。 默认值为128.请注意,使用disruptor-style BlockingQueue时,此缓冲区大小必须为2的乘方。
errorRef String The name of the Appender to invoke if none of the appenders can be called, either due to errors in the appenders or because the queue is full. If not specified then errors will be ignored. 如果由于追加器中的错误或由于队列已满而无法调用任何追加器,则调用的Appender的名称。 如果未指定,则错误将被忽略。
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. 筛选以确定事件是否应由此Appender处理。 通过使用CompositeFilter可以使用多个过滤器。
name String The name of the Appender. 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 a FailoverAppender. 默认值为true,导致在将事件附加到内部记录然后忽略时遇到异常。 当设置为false时,异常将被传播到调用者。 在FailoverAppender中包装此Appender时,必须将此值设置为false。
includeLocation boolean Extracting location is an expensive operation (it can make logging 5 - 20 times slower). To improve performance, location is not included by default when adding a log event to the queue. You can change this by setting includeLocation=”true”. 提取位置是一个昂贵的操作(它可以使记录慢5 - 20倍)。 为了提高性能,在将日志事件添加到队列时,默认情况下不包括位置。 您可以通过设置includeLocation =“true”来更改此设置。
BlockingQueueFactory BlockingQueueFactory This element overrides what type of BlockingQueue to use. See below documentation for more details. 此元素覆盖要使用的BlockingQueue的类型。 有关详细信息,请参阅以下文档。

There are also a few system properties that can be used to maintain application throughput even when the underlying appender cannot keep up with the logging rate and the queue is filling up. See the details for system properties log4j2.AsyncQueueFullPolicy and log4j2.DiscardThreshold.

还有一些系统属性可用于维护应用程序吞吐量,即使底层追加器无法跟上记录速率和队列正在填满。 请参阅系统属性log4j2.AsyncQueueFullPolicy和log4j2.DiscardThreshold的详细信息。

A typical AsyncAppender configuration might look like:

典型的AsyncAppender配置可能如下所示:


<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <File name="MyFile" fileName="logs/app.log">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%nPattern>
      PatternLayout>
    File>
    <Async name="Async">
      <AppenderRef ref="MyFile"/>
    Async>
  Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Async"/>
    Root>
  Loggers>
Configuration>

Starting in Log4j 2.7, a custom implementation of BlockingQueue or TransferQueue can be specified using a BlockingQueueFactory plugin. To override the default BlockingQueueFactory, specify the plugin inside an element like so:

从Log4j 2.7开始,可以使用BlockingQueueFactory插件指定BlockingQueue或TransferQueue的自定义实现。 要覆盖默认的BlockingQueueFactory,请在元素中指定插件,如下所示:

<Configuration name="LinkedTransferQueueExample">
  <Appenders>
    <List name="List"/>
    <Async name="Async" bufferSize="262144">
      <AppenderRef ref="List"/>
      <LinkedTransferQueue/>
    Async>
  Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="Async"/>
    Root>
  Loggers>
Configuration>

FailoverAppender

The FailoverAppender wraps a set of appenders. If the primary Appender fails the secondary appenders will be tried in order until one succeeds or there are no more secondaries to try.

故障切换应用程序重复一组追加器。 如果主Appender失败,则二级追加器将按顺序尝试,直到一个成功或没有更多次要尝试。

FailoverAppender Parameters

Parameter Name Type Description 中文翻译
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. 筛选以确定事件是否应由此Appender处理。 通过使用CompositeFilter可以使用多个过滤器。
primary String The name of the primary Appender to use. 要使用的主要Appender的名称。
failovers String[] The names of the secondary Appenders to use. 要使用的辅助应用程序的名称。
name String The name of the Appender. Appender的名称。
retryIntervalSeconds integer The number of seconds that should pass before retrying the primary Appender. The default is 60. 重试主Appender之前应该经过的秒数。 默认值为60。
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. 默认值为true,导致在将事件附加到内部记录然后忽略时遇到异常。 当设置为false时,异常将被传播到调用者。
target String Either “SYSTEM_OUT” or “SYSTEM_ERR”. The default is “SYSTEM_ERR”. “SYSTEM_OUT”或“SYSTEM ERROR”。 默认值为“SYSTEM ERR”。

A Failover configuration might look like:

故障转移配置可能如下所示:


<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}.log.gz"
                 ignoreExceptions="false">
      <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%nPattern>
      PatternLayout>
      <TimeBasedTriggeringPolicy />
    RollingFile>
    <Console name="STDOUT" target="SYSTEM_OUT" ignoreExceptions="false">
      <PatternLayout pattern="%m%n"/>
    Console>
    <Failover name="Failover" primary="RollingFile">
      <Failovers>
        <AppenderRef ref="Console"/>
      Failovers>
    Failover>
  Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Failover"/>
    Root>
  Loggers>
Configuration>

你可能感兴趣的:(程序员周刊,电子资料,翻译,log4j2)