最近springboot 1.4项目中用到了log4j2这么个东东,不知道怎么配置就从apache官网找了log4j2的官方文档来看,但是都是英文的,由于自己英文不好,因此就用google翻译了出来,一来是自己看看学习,二来对有需要的同学提供个帮助,不正确的地方欢迎指出。
原文地址:http://logging.apache.org/log4j/2.x/manual/appenders.html
FileAppender
The FileAppender is an OutputStreamAppender that writes to the File named in the fileName parameter. The FileAppender uses a FileManager (which extends OutputStreamManager) to actually perform the file I/O. While FileAppenders from different Configurations cannot be shared, the FileManagers can be if the Manager is accessible. For example, two web applications in a servlet container can have their own configuration and safely write to the same file if Log4j is in a ClassLoader that is common to both of them.
FileAppender是一个OutputStreamAppender,用于写入fileName参数中指定的文件。 FileAppender使用FileManager(扩展OutputStreamManager)来实际执行文件I / O。 虽然不能共享来自不同配置的FileAppender,但如果管理器可访问,则FileManager可以是。 例如,servlet容器中的两个Web应用程序可以有自己的配置,并且如果Log4j在两个共同的ClassLoader中,则可以安全地写入同一个文件。
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. | 当为true时 - 默认情况下,记录将附加到文件的结尾。 当设置为false时,将在写入新记录之前清除文件。 |
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. | 当为true时,默认情况下,记录将被写入缓冲区,并且当缓冲区已满时,或者如果设置了immediateFlush,则在写入记录时将数据写入磁盘。 文件锁定不能与bufferedIO一起使用。 性能测试表明,即使启用了immediateFlush,使用缓冲I / O也能显着提高性能。 |
bufferSize | int | When bufferedIO is true, this is the buffer size, the default is 8192 bytes. | 当bufferedIO为true时,这是缓冲区大小,默认为8192字节。 |
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. | 追加器根据需要创建文件。 追加器仅在日志事件通过所有过滤器并被路由到此追加器时创建文件。 默认为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. | 筛选以确定事件是否应由此Appender处理。 通过使用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. | 当设置为true时 - 默认情况下,每次写入后都将进行flush。 这将确保数据写入磁盘,但可能会影响性能。每次写入后刷新仅在同步记录器使用此附加器时有用。 即使immediateFlush设置为false,异步记录器和追加器也将在一批事件结束时自动刷新。 这也保证了数据被写入磁盘,但是更有效率。 |
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. | 用于格式化LogEvent的布局。 如果没有提供布局,将使用“%m%n”的默认模式布局。 |
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. | 设置为true时,只有在保持文件锁定的情况下才会进行I / O操作,允许多个JVM中的FileAppender和潜在的多个主机同时写入同一文件。 这将显着影响性能,因此应谨慎使用。 此外,在许多系统上,文件锁是“咨询”,意味着其他应用程序可以对文件执行操作而不获取锁。 默认值为false。 |
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。 |
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>
FlumeAppender
This is an optional component supplied in a separate jar.
Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregating, and moving large amounts of log data from many different sources to a centralized data store. The FlumeAppender takes LogEvents and sends them to a Flume agent as serialized Avro events for consumption.
The Flume Appender supports three modes of operation.
It can act as a remote Flume client which sends Flume events via Avro to a Flume Agent configured with an Avro Source.
It can act as an embedded Flume Agent where Flume events pass directly into Flume for processing.
It can persist events to a local BerkeleyDB data store and then asynchronously send the events to Flume, similar to the embedded Flume Agent but without most of the Flume dependencies.
Usage as an embedded agent will cause the messages to be directly passed to the Flume Channel and then control will be immediately returned to the application. All interaction with remote agents will occur asynchronously. Setting the “type” attribute to “Embedded” will force the use of the embedded agent. In addition, configuring agent properties in the appender configuration will also cause the embedded agent to be used.
这是在单独的jar中提供的可选组件。
Apache Flume是一个分布式,可靠和可用的系统,用于从大量不同的源有效地收集,聚合和移动大量日志数据到集中式数据存储。 FlumeAppender使用LogEvents并将其作为序列化Avro事件发送到Flume代理以供消费。
Flume Appender支持三种操作模式。
*它可以充当远程Flume客户端,通过Avro将Flume事件发送到配置了Avro Source的Flume代理。
它可以充当嵌入式Flume代理,其中Flume事件直接传递到Flume进行处理。
它可以将事件持久化到本地BerkeleyDB数据存储,然后异步发送事件到Flume,类似于嵌入Flume代理,但没有大多数Flume依赖。*
作为嵌入式代理的用法将导致消息被直接传递到Flume信道,然后控制将立即返回到应用程序。所有与远程代理的交互都将异步进行。将“type”属性设置为“嵌入”将强制使用嵌入式代理。此外,在追加器配置中配置代理属性还将导致使用嵌入的代理。
FileAppender Parameters
Parameter Name | Type | Description | 中文翻译 |
---|---|---|---|
agents | Agent[] | An array of Agents to which the logging events should be sent. If more than one agent is specified the first Agent will be the primary and subsequent Agents will be used in the order specified as secondaries should the primary Agent fail. Each Agent definition supplies the Agents host and port. The specification of agents and properties are mutually exclusive. If both are configured an error will result. | 应向其发送日志记录事件的代理程序数组。 如果指定了多个代理,则第一个代理将是主要代理,如果主要代理失败,则后续代理将以指定的次序使用。 每个代理定义提供代理主机和端口。 代理和属性的规范是相互排斥的。 如果两者都配置,将导致错误。 |
agentRetries | integer | The number of times the agent should be retried before failing to a secondary. This parameter is ignored when type=”persistent” is specified (agents are tried once before failing to the next). | 代理在失败之前应重试的次数。 当指定type =“persistent”时将忽略此参数(代理在尝试失败前尝试一次)。 |
batchSize | integer | Specifies the number of events that should be sent as a batch. The default is 1. This parameter only applies to the Flume Appender. | 指定应作为批处理发送的事件数。 默认值为1.此参数仅适用于Flume Appender。 |
compress | boolean | When set to true the message body will be compressed using gzip | 当设置为true时,消息体将使用gzip进行压缩 |
connectTimeoutMillis | integer | The number of milliseconds Flume will wait before timing out the connection. | Flume在超时连接之前将等待的毫秒数。 |
dataDir | String | Directory where the Flume write ahead log should be written. Valid only when embedded is set to true and Agent elements are used instead of Property elements. | 应写入Flume向前写日志的目录。 仅当embedded设置为true并且使用代理元素而不是Property元素时有效。 |
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可以使用多个过滤器。 |
eventPrefix | String | The character string to prepend to each event attribute in order to distinguish it from MDC attributes. The default is an empty string | 为每个事件属性预先添加的字符串,以便与MDC属性区分开。 默认值为空字符串。 |
flumeEventFactory | FlumeEventFactory | Factory that generates the Flume events from Log4j events. The default factory is the FlumeAvroAppender itself. | 从Log4j事件生成Flume事件的工厂。 默认工厂是FlumeAvroAppender本身。 |
layout | Layout | The Layout to use to format the LogEvent. If no layout is specified RFC5424Layout will be used. | 用于格式化LogEvent的布局。 如果未指定布局,将使用RFC5424Layout。 |
lockTimeoutRetries | integer | The number of times to retry if a LockConflictException occurs while writing to Berkeley DB. The default is 5. | 写入Berkeley DB时发生LockConflictException时重试的次数。 默认值为5。 |
maxDelayMillis | integer | The maximum number of milliseconds to wait for batchSize events before publishing the batch. | 发布批处理之前等待batchSize事件的最大毫秒数。 |
mdcExcludes | String | A comma separated list of mdc keys that should be excluded from the FlumeEvent. This is mutually exclusive with the mdcIncludes attribute. | 应该从FlumeEvent中排除的mdc键的逗号分隔列表。 这与mdcIncludes属性互斥。 |
mdcIncludes | String | A comma separated list of mdc keys that should be included in the FlumeEvent. Any keys in the MDC not found in the list will be excluded. This option is mutually exclusive with the mdcExcludes attribute. | 应该包含在FlumeEvent中的mdc键的逗号分隔列表。 在列表中找不到的MDC中的任何键将被排除。 此选项与mdcExcludes属性互斥。 |
mdcRequired | String | A comma separated list of mdc keys that must be present in the MDC. If a key is not present a LoggingException will be thrown. | 必须在MDC中存在的mdc键的逗号分隔列表。 如果一个键不存在,将抛出一个LoggingException。 |
mdcPrefix | String | A string that should be prepended to each MDC key in order to distinguish it from event attributes. The default string is “mdc:”. | 应该为每个MDC键添加一个字符串,以便将它与事件属性区分开来。 默认字符串为“mdc:”。 |
name | String | The name of the Appender. | Appender的名称。 |
properties | Property[] | One or more Property elements that are used to configure the Flume Agent. The properties must be configured without the agent name (the appender name is used for this) and no sources can be configured. Interceptors can be specified for the source using “sources.log4j-source.interceptors”. All other Flume configuration properties are allowed. Specifying both Agent and Property elements will result in an error. When used to configure in Persistent mode the valid properties are: “keyProvider” to specify the name of the plugin to provide the secret key for encryption. | 用于配置Flume代理的一个或多个Property元素。 必须配置属性,而不使用代理名称(为此使用附加程序名称),并且不能配置任何源。 可以使用“sources.log4j-source.interceptors”为源指定拦截器。 允许所有其他Flume配置属性。 指定代理和属性元素将导致错误。 当用于在持久模式下配置时,有效的属性是:“keyProvider”指定插件的名称,以提供用于加密的密钥。 |
requestTimeoutMillis | integer | The number of milliseconds Flume will wait before timing out the request. | Flume在超时请求之前将等待的毫秒数。 |
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。 |
type | enumeration | One of “Avro”, “Embedded”, or “Persistent” to indicate which variation of the Appender is desired. | “Avro”,“Embedded”或“Persistent”之一,用于指示需要Appender的变体。 |
A sample FlumeAppender configuration that is configured with a primary and a secondary agent, compresses the body, and formats the body using the RFC5424Layout:
使用主代理和辅助代理配置的样本FlumeAppender配置,压缩主体并使用RFC5424Layout格式化主体:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Flume name="eventLogger" compress="true">
<Agent host="192.168.10.101" port="8800"/>
<Agent host="192.168.10.102" port="8800"/>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
</Flume>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="eventLogger"/>
</Root>
</Loggers>
</Configuration>
A sample FlumeAppender configuration that is configured with a primary and a secondary agent, compresses the body, formats the body using the RFC5424Layout, and persists encrypted events to disk:
使用主代理和辅助代理配置的示例FlumeAppender配置,压缩正文,使用RFC5424Layout格式化正文,并将加密事件保留到磁盘:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Flume name="eventLogger" compress="true" type="persistent" dataDir="./logData">
<Agent host="192.168.10.101" port="8800"/>
<Agent host="192.168.10.102" port="8800"/>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
<Property name="keyProvider">MySecretProvider</Property>
</Flume>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="eventLogger"/>
</Root>
</Loggers>
</Configuration>
A sample FlumeAppender configuration that is configured with a primary and a secondary agent, compresses the body, formats the body using RFC5424Layout and passes the events to an embedded Flume Agent.
配置有主代理和辅助代理的样本FlumeAppender配置压缩主体,使用RFC5424Layout格式化主体,并将事件传递到嵌入的Flume代理。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Flume name="eventLogger" compress="true" type="Embedded">
<Agent host="192.168.10.101" port="8800"/>
<Agent host="192.168.10.102" port="8800"/>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
</Flume>
<Console name="STDOUT">
<PatternLayout pattern="%d [%p] %c %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="EventLogger" level="info">
<AppenderRef ref="eventLogger"/>
</Logger>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
A sample FlumeAppender configuration that is configured with a primary and a secondary agent using Flume configuration properties, compresses the body, formats the body using RFC5424Layout and passes the events to an embedded Flume Agent.
使用Flume配置属性配置有主代理和辅助代理的样本FlumeAppender配置压缩主体,使用RFC5424Layout格式化主体,并将事件传递到嵌入的Flume代理。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" name="MyApp" packages="">
<Appenders>
<Flume name="eventLogger" compress="true" type="Embedded">
<Property name="channels">file</Property>
<Property name="channels.file.type">file</Property>
<Property name="channels.file.checkpointDir">target/file-channel/checkpoint</Property>
<Property name="channels.file.dataDirs">target/file-channel/data</Property>
<Property name="sinks">agent1 agent2</Property>
<Property name="sinks.agent1.channel">file</Property>
<Property name="sinks.agent1.type">avro</Property>
<Property name="sinks.agent1.hostname">192.168.10.101</Property>
<Property name="sinks.agent1.port">8800</Property>
<Property name="sinks.agent1.batch-size">100</Property>
<Property name="sinks.agent2.channel">file</Property>
<Property name="sinks.agent2.type">avro</Property>
<Property name="sinks.agent2.hostname">192.168.10.102</Property>
<Property name="sinks.agent2.port">8800</Property>
<Property name="sinks.agent2.batch-size">100</Property>
<Property name="sinkgroups">group1</Property>
<Property name="sinkgroups.group1.sinks">agent1 agent2</Property>
<Property name="sinkgroups.group1.processor.type">failover</Property>
<Property name="sinkgroups.group1.processor.priority.agent1">10</Property>
<Property name="sinkgroups.group1.processor.priority.agent2">5</Property>
<RFC5424Layout enterpriseNumber="18060" includeMDC="true" appName="MyApp"/>
</Flume>
<Console name="STDOUT">
<PatternLayout pattern="%d [%p] %c %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="EventLogger" level="info">
<AppenderRef ref="eventLogger"/>
</Logger>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>