在这里插入图片描述

ES7.15日志配置小结

  • ES7.0增加通过json记录ES日志,可在日志中加入node.id,cluster.uuid,type。其中type主要用于区分docker环境下的每个节点的日志。
  • ES日志包括集群节点日志、过时日志、查询慢日志和写入慢日志等。
  • 日志级别调整,可以细化到包级别,此外还支持动态修改,例如将discovery模块日志级别设置为debug,其他模块仍然保持info级别。

环境信息

ES版本7.15

集群日志

默认日志配置如下,详细的配置方式可以参考官网

######## Server JSON ############################
appender.rolling.type = RollingFile 
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.json 
appender.rolling.layout.type = ESJsonLayout 
appender.rolling.layout.type_name = server 
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.json.gz 
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy 
appender.rolling.policies.time.interval = 1 
appender.rolling.policies.time.modulate = true 
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy 
appender.rolling.policies.size.size = 256MB 
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.fileIndex = nomax
appender.rolling.strategy.action.type = Delete 
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
appender.rolling.strategy.action.condition.type = IfFileName 
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* 
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize 
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB 
################################################

其中appender.rolling.layout.type设置值ESJsonLayout表示通过JSON方式记录日志;

当ES节点运行于docker时,可设置appender.rolling.layout.type_name = server,将会在json日志中打印"type":"server"用于区分日志流。

注意:ES7.15默认是带两个格式的日志,这样会输出两种格式不同,内容相同的日志。可以在log4j2.properties中删除或注释。例如将集群志中老的日志注释,这样就不会打印老风格的日志了。

################################################
######## Server -  old style pattern ###########
#appender.rolling_old.type = RollingFile
#appender.rolling_old.name = rolling_old
#appender.rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
#appender.rolling_old.layout.type = PatternLayout
#appender.rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n

#appender.rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
#appender.rolling_old.policies.type = Policies
#appender.rolling_old.policies.time.type = TimeBasedTriggeringPolicy
#appender.rolling_old.policies.time.interval = 1
#appender.rolling_old.policies.time.modulate = true
#appender.rolling_old.policies.size.type = SizeBasedTriggeringPolicy
#appender.rolling_old.policies.size.size = 128MB
#appender.rolling_old.strategy.type = DefaultRolloverStrategy
#appender.rolling_old.strategy.fileIndex = nomax
#appender.rolling_old.strategy.action.type = Delete
#appender.rolling_old.strategy.action.basepath = ${sys:es.logs.base_path}
#appender.rolling_old.strategy.action.condition.type = IfFileName
#appender.rolling_old.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
#appender.rolling_old.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
#appender.rolling_old.strategy.action.condition.nested_condition.exceeds = 2GB

过期日志

过期日志:使用即将废弃的ES功能时,会打印告警日志到日志目录下的elasticsearch_deprecation.json文件中;日志级别包括CRITICALWARN,当使用功能会在下个主版本移除时会报CRITICAL级别日志,当使用功能可能会在未来版本移除时会报WARN。此外过期日志默认是打开状态,如果想关闭过时日志,设置方法如下:

logger.deprecation.level = OFF

如果想识别是什么触发了告警日志,可以通过日志中的x-opaque-id识别(当然你得在请求的header中携带x-opaque-id信息)。可以设置cluster.deprecation_indexing.enabled:true将过期日志写入到索引.logs-deprecation.elasticsearch-default中(该功能在ES7.16中添加,ES7.15暂不支持)。

查询慢日志

查询慢日志可以细化到分片级别,日志将详细记录搜索和获取数据两阶段的日志。默认情况为关闭状态(设置为-1)。查询慢日志的日志名称和格式在log4j2.properties中配置,默认名称是*_index_search_slowlog.json*表示${sys:es.logs.cluster_name}。查询慢日志都是在索引层面动态配置,配置方法如下:

# 表示配置my-index-000001 查询慢日志条件
PUT /my-index-000001/_settings
{
  "index.search.slowlog.threshold.query.warn": "10s",
  "index.search.slowlog.threshold.query.info": "5s",
  "index.search.slowlog.threshold.query.debug": "2s",
  "index.search.slowlog.threshold.query.trace": "500ms",
  "index.search.slowlog.threshold.fetch.warn": "1s",
  "index.search.slowlog.threshold.fetch.info": "800ms",
  "index.search.slowlog.threshold.fetch.debug": "500ms",
  "index.search.slowlog.threshold.fetch.trace": "200ms"
}
#关闭查询慢日志
PUT /my-index-000001/_settings
{
  "index.search.slowlog.threshold.query.warn": "-1",
  "index.search.slowlog.threshold.query.info": "-1",
  "index.search.slowlog.threshold.query.debug": "-1",
  "index.search.slowlog.threshold.query.trace": "-1",
  "index.search.slowlog.threshold.fetch.warn": "-1",
  "index.search.slowlog.threshold.fetch.info": "-1",
  "index.search.slowlog.threshold.fetch.debug": "-1",
  "index.search.slowlog.threshold.fetch.trace": "-1"
}

# 所有索引都配置查询慢日志
PUT  _settings
{
  "index.search.slowlog.threshold.query.warn": "10s",
  "index.search.slowlog.threshold.query.info": "5s",
  "index.search.slowlog.threshold.query.debug": "2s",
  "index.search.slowlog.threshold.query.trace": "500ms",
  "index.search.slowlog.threshold.fetch.warn": "1s",
  "index.search.slowlog.threshold.fetch.info": "800ms",
  "index.search.slowlog.threshold.fetch.debug": "500ms",
  "index.search.slowlog.threshold.fetch.trace": "200ms"
}

注意:这里warn、info、debug、trace表示超过10s的查询会打印warn级别日志,超过5s打印info日志以此类推。当info设置为3ms,warn设置为4ms,打印格式如下:

[INFO ][i.s.s.query              ] [cluster-node-5] [.kibana_task_manager_7.15.2_001][0] took[3.5ms]

索引慢日志

索引慢日志和查询慢日志类似,但索引慢日志默认是开启的;在log4j2.properties中配置日志格式和慢日志文件名称(名称为*_index_indexing_slowlog.json,*代表${sys:es.logs.cluster_name}).

在打印慢日志时,默认将真实索引数据的1000个字符打印出来("index.indexing.slowlog.source": "1000");可以将该值修改为false0跳过打印原始数据;当设置为true会打印全部数据。但是打印格式会变成一行。如果想保留原有格式可以通过设置index.indexing.slowlog.reformat:"false",同样索引慢日志也可以动态配置:

PUT /my-index-000001/_settings
{
  "index.indexing.slowlog.threshold.index.warn": "10s",
  "index.indexing.slowlog.threshold.index.info": "5s",
  "index.indexing.slowlog.threshold.index.debug": "2s",
  "index.indexing.slowlog.threshold.index.trace": "500ms",
  "index.indexing.slowlog.source": "1000",
  "index.indexing.slowlog.reformat": "false"
}

日志级别控制

ES可以为每个java包设置日志级别,且支持动态修改。

集群日志级别动态设置

第一种 实时修改

PUT /_cluster/settings
{
    "transient" : {
        "logger._root" : "info"
    }
}

第二种 在log4j2.properties中设置

rootLogger.level = debug
包级别日志动态设置

例如discovery可以通过三种方式设置日志级别:

第一种 实时修改

PUT /_cluster/settings
{
  "transient": {
    "logger.org.elasticsearch.discovery": "DEBUG"
  }
}

第二种 在elasticsearch.yml中设置

logger.org.elasticsearch.discovery: DEBUG

第三种 在log4j2.properties中设置

logger.discovery.name = org.elasticsearch.discovery
logger.discovery.level = debug

常见功能的包名如下所示,更多信息可以参考源码

  • org.elasticsearch.indices.recovery负责分片数据恢复
  • org.elasticsearch.http负责绑定端口或节点间的连接通信
  • org.elasticsearch.snapshots负责备份和备份恢复相关操作
  • org.elasticsearch.cluster.routing.allocation负责分片的分配决策过程

客官且慢,点赞、收藏+关注 谢谢~

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐