日志配置
Elasticsearch使用Log4j2记录日志, 可以使用log4j2.properties配置文件来配置日志. Elasticsearch暴露3个属性, ${sys:es.logs.base_path}, ${sys:es.logs.cluster_name}, and ${sys:es.logs.node_name}(如果在node.name里明确声明节点名称)可以在配置文件里引用. ${sys:es.logs.base_path}会被解析成配置文件的目录, ${sys:es.logs.cluster_name}会被解析成集群名称(默认作为配置日志文件的前缀). ${sys:es.logs.node_name}会被解析成节点名(如果显示声明的话).
For example, if your log directory (path.logs) is /var/log/elasticsearch and your cluster is named production then ${sys:es.logs.base_path} will resolve to /var/log/elasticsearch and ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log will resolve to /var/log/elasticsearch/production.log.
比如, 如果你的日志目录(path.logs)是/var/log/elasticsearch并且你的集群名称是production, 这个时候
${sys:es.logs.base_path} 会被解析成 /var/log/elasticsearch
${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log 会被解析成
/var/log/elasticsearch/production.log
appender.rolling.type = RollingFile 1
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log 2
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz 3
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy 4
appender.rolling.policies.time.interval = 1 5
appender.rolling.policies.time.modulate = true 6
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy 7
appender.rolling.policies.size.size = 256MB 8
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
appender.rolling.strategy.action.type = Delete 9
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName 10
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* 11
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize 12
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB 13
- 设置 RollingFile 作为输出器类型
- 设置日志文件目录位置文件名为 /var/log/elasticsearch/production.log
- 自动滚动日志, 到/var/log/elasticsearch/production-yyyy-MM-dd-i.log 这样的格式里. 每次滚动压缩i都会递增
- 使用基于时间的策略
- 滚动间隔是1天
- 时间调整24小时格式
- 使用size based策略
- 256MB以后滚动
- 滚动的时候使用Delete动作
- 只删除符合给定模式的文件
- 这个模式只展示主日志
- 只在累积了指定大小的日志s会删除
- 2GB的时候压缩
log4j2配置文件不能有多余的空格, 复制的时候特别要注意
appender.rolling.filePattern中你可以使用.zip替换.gz. 如果你移除.gz后缀.那么滚动日志不会压缩
如果你想保留一个特性时间段的日志, 你可以使用一个delete动作的滚动策略
appender.rolling.strategy.type = DefaultRolloverStrategy 1
appender.rolling.strategy.action.type = Delete 2
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path} 3
appender.rolling.strategy.action.condition.type = IfFileName 4
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* 5
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified 6
appender.rolling.strategy.action.condition.nested_condition.age = 7D 7
- DefaultRolloverStrategy
- 配置delete 动作
- Elasticsearch日志的基础目录
- 滚动条件
- delete符合条件的日志文件
- 内嵌条件
- 保留7天的日志
多个配置文件会被加载(这种情况下, 他们会合并), 只要他们在Elasticsearch的配置文件目录下, 并且名为log4j2.properties.这对挂在额外的日志记录器很有用, logger部分包含了java包和他们响应的日志级别,appender部分包含了日志的目的地, 额外的日志信息, 可以在这里找到http://logging.apache.org/log4j/2.x/manual/configuration.html
配置日志级别
有4种方法配置日志级别, 他们各有各的使用场景
- 通过命令行-E <name of logging hierarchy>=<level> (比如说, -E logger.org.elasticsearch.transport=trace). 当时暂时在一个节点上调试的时候, 这种用法非常适合, (比如说启动或者部署的时候出现问题).
- 通过elasticsearch.yml: <name of logging hierarchy>: <level> (e.g., logger.org.elasticsearch.transport: trace).这也是为了调试的时候使用, 但是不是通过命令行启动Elasticsearch,而是作为服务启动Elasticsearch.
- 通过cluster-setting:
PUT /_cluster/settings
{
"transient": {
"<name of logging hierarchy>": "<level>"
}
}
比如说
PUT /_cluster/settings
{
"transient": {
"logger.org.elasticsearch.transport": "trace"
}
}
这在动态改变一个运行的集群是十分适合的.
- 通过log4j2.properties配置文件
logger.<unique_identifier>.name = <name of logging hierarchy>
logger.<unique_identifier>.level = <level>
比如说
logger.transport.name = org.elasticsearch.transport
logger.transport.level = trace
这个适合需要细微控制的情况, 这种用法十分罕见