logging-config.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. [[logging]]
  2. === Logging
  3. include::{es-repo-dir}/tab-widgets/code.asciidoc[]
  4. You can use {es}'s application logs to monitor your cluster and diagnose issues.
  5. If you run {es} as a service, the default location of the logs varies based on
  6. your platform and installation method:
  7. include::{es-repo-dir}/tab-widgets/logging-widget.asciidoc[]
  8. If you run {es} from the command line, {es} prints logs to the standard output
  9. (`stdout`).
  10. [discrete]
  11. [[loggin-configuration]]
  12. === Logging configuration
  13. Elasticsearch uses https://logging.apache.org/log4j/2.x/[Log4j 2] for
  14. logging. Log4j 2 can be configured using the log4j2.properties
  15. file. Elasticsearch exposes three properties, `${sys:es.logs.base_path}`,
  16. `${sys:es.logs.cluster_name}`, and `${sys:es.logs.node_name}` that can be
  17. referenced in the configuration file to determine the location of the log
  18. files. The property `${sys:es.logs.base_path}` will resolve to the log directory,
  19. `${sys:es.logs.cluster_name}` will resolve to the cluster name (used as the
  20. prefix of log filenames in the default configuration), and
  21. `${sys:es.logs.node_name}` will resolve to the node name (if the node name is
  22. explicitly set).
  23. For example, if your log directory (`path.logs`) is `/var/log/elasticsearch` and
  24. your cluster is named `production` then `${sys:es.logs.base_path}` will resolve
  25. to `/var/log/elasticsearch` and
  26. `${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log`
  27. will resolve to `/var/log/elasticsearch/production.log`.
  28. [source,properties]
  29. --------------------------------------------------
  30. ######## Server JSON ############################
  31. appender.rolling.type = RollingFile <1>
  32. appender.rolling.name = rolling
  33. appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.json <2>
  34. appender.rolling.layout.type = ECSJsonLayout <3>
  35. appender.rolling.layout.dataset = elasticsearch.server <4>
  36. appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.json.gz <5>
  37. appender.rolling.policies.type = Policies
  38. appender.rolling.policies.time.type = TimeBasedTriggeringPolicy <6>
  39. appender.rolling.policies.time.interval = 1 <7>
  40. appender.rolling.policies.time.modulate = true <8>
  41. appender.rolling.policies.size.type = SizeBasedTriggeringPolicy <9>
  42. appender.rolling.policies.size.size = 256MB <10>
  43. appender.rolling.strategy.type = DefaultRolloverStrategy
  44. appender.rolling.strategy.fileIndex = nomax
  45. appender.rolling.strategy.action.type = Delete <11>
  46. appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
  47. appender.rolling.strategy.action.condition.type = IfFileName <12>
  48. appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* <13>
  49. appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize <14>
  50. appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB <15>
  51. ################################################
  52. --------------------------------------------------
  53. <1> Configure the `RollingFile` appender
  54. <2> Log to `/var/log/elasticsearch/production_server.json`
  55. <3> Use JSON layout.
  56. <4> `dataset` is a flag populating the `event.dataset` field in a `ECSJsonLayout`.
  57. It can be used to distinguish different types of logs more easily when parsing them.
  58. <5> Roll logs to `/var/log/elasticsearch/production-yyyy-MM-dd-i.json`; logs
  59. will be compressed on each roll and `i` will be incremented
  60. <6> Use a time-based roll policy
  61. <7> Roll logs on a daily basis
  62. <8> Align rolls on the day boundary (as opposed to rolling every twenty-four
  63. hours)
  64. <9> Using a size-based roll policy
  65. <10> Roll logs after 256 MB
  66. <11> Use a delete action when rolling logs
  67. <12> Only delete logs matching a file pattern
  68. <13> The pattern is to only delete the main logs
  69. <14> Only delete if we have accumulated too many compressed logs
  70. <15> The size condition on the compressed logs is 2 GB
  71. [source,properties]
  72. --------------------------------------------------
  73. ######## Server - old style pattern ###########
  74. appender.rolling_old.type = RollingFile
  75. appender.rolling_old.name = rolling_old
  76. appender.rolling_old.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.log <1>
  77. appender.rolling_old.layout.type = PatternLayout
  78. appender.rolling_old.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n
  79. appender.rolling_old.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.old_log.gz
  80. --------------------------------------------------
  81. <1> The configuration for `old style` pattern appenders. These logs will be saved in `*.log` files and if archived will be in `*
  82. .log.gz` files. Note that these should be considered deprecated and will be removed in the future.
  83. NOTE: Log4j's configuration parsing gets confused by any extraneous whitespace;
  84. if you copy and paste any Log4j settings on this page, or enter any Log4j
  85. configuration in general, be sure to trim any leading and trailing whitespace.
  86. Note than you can replace `.gz` by `.zip` in `appender.rolling.filePattern` to
  87. compress the rolled logs using the zip format. If you remove the `.gz`
  88. extension then logs will not be compressed as they are rolled.
  89. If you want to retain log files for a specified period of time, you can use a
  90. rollover strategy with a delete action.
  91. [source,properties]
  92. --------------------------------------------------
  93. appender.rolling.strategy.type = DefaultRolloverStrategy <1>
  94. appender.rolling.strategy.action.type = Delete <2>
  95. appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path} <3>
  96. appender.rolling.strategy.action.condition.type = IfFileName <4>
  97. appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* <5>
  98. appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified <6>
  99. appender.rolling.strategy.action.condition.nested_condition.age = 7D <7>
  100. --------------------------------------------------
  101. <1> Configure the `DefaultRolloverStrategy`
  102. <2> Configure the `Delete` action for handling rollovers
  103. <3> The base path to the Elasticsearch logs
  104. <4> The condition to apply when handling rollovers
  105. <5> Delete files from the base path matching the glob
  106. `${sys:es.logs.cluster_name}-*`; this is the glob that log files are rolled
  107. to; this is needed to only delete the rolled Elasticsearch logs but not also
  108. delete the deprecation and slow logs
  109. <6> A nested condition to apply to files matching the glob
  110. <7> Retain logs for seven days
  111. Multiple configuration files can be loaded (in which case they will get merged)
  112. as long as they are named `log4j2.properties` and have the Elasticsearch config
  113. directory as an ancestor; this is useful for plugins that expose additional
  114. loggers. The logger section contains the java packages and their corresponding
  115. log level. The appender section contains the destinations for the logs.
  116. Extensive information on how to customize logging and all the supported
  117. appenders can be found on the
  118. https://logging.apache.org/log4j/2.x/manual/configuration.html[Log4j
  119. documentation].
  120. [discrete]
  121. [[configuring-logging-levels]]
  122. === Configuring logging levels
  123. Each Java package in the {es-repo}[{es} source code] has a related logger. For
  124. example, the `org.elasticsearch.discovery` package has
  125. `logger.org.elasticsearch.discovery` for logs related to the
  126. <<modules-discovery-hosts-providers,discovery>> process.
  127. To get more or less verbose logs, use the <<cluster-update-settings,cluster
  128. update settings API>> to change the related logger's log level. Each logger
  129. accepts Log4j 2's built-in log levels, from least to most verbose: `OFF`,
  130. `FATAL`, `ERROR`, `WARN`, `INFO`, `DEBUG`, and `TRACE`. The default log level is
  131. `INFO`. Messages logged at higher verbosity levels (`DEBUG` and `TRACE`) are
  132. only intended for expert use.
  133. [source,console]
  134. ----
  135. PUT /_cluster/settings
  136. {
  137. "transient": {
  138. "logger.org.elasticsearch.discovery": "DEBUG"
  139. }
  140. }
  141. ----
  142. Other ways to change log levels include:
  143. 1. `elasticsearch.yml`:
  144. +
  145. --
  146. [source,yaml]
  147. ----
  148. logger.org.elasticsearch.discovery: DEBUG
  149. ----
  150. This is most appropriate when debugging a problem on a single node.
  151. --
  152. 2. `log4j2.properties`:
  153. +
  154. --
  155. [source,properties]
  156. ----
  157. logger.discovery.name = org.elasticsearch.discovery
  158. logger.discovery.level = debug
  159. ----
  160. This is most appropriate when you already need to change your Log4j 2
  161. configuration for other reasons. For example, you may want to send logs for a
  162. particular logger to another file. However, these use cases are rare.
  163. --
  164. [discrete]
  165. [[deprecation-logging]]
  166. === Deprecation logging
  167. In addition to regular logging, Elasticsearch allows you to enable logging
  168. of deprecated actions. For example this allows you to determine early, if
  169. you need to migrate certain functionality in the future. By default,
  170. deprecation logging is enabled at the WARN level, the level at which all
  171. deprecation log messages will be emitted.
  172. [source,properties]
  173. --------------------------------------------------
  174. logger.deprecation.level = warn
  175. --------------------------------------------------
  176. This will create a daily rolling deprecation log file in your log directory.
  177. Check this file regularly, especially when you intend to upgrade to a new
  178. major version.
  179. The default logging configuration has set the roll policy for the deprecation
  180. logs to roll and compress after 1 GB, and to preserve a maximum of five log
  181. files (four rolled logs, and the active log).
  182. You can disable it in the `config/log4j2.properties` file by setting the deprecation
  183. log level to `error` like this:
  184. [source,properties]
  185. --------------------------------------------------
  186. logger.deprecation.name = org.elasticsearch.deprecation
  187. logger.deprecation.level = error
  188. --------------------------------------------------
  189. You can identify what is triggering deprecated functionality if `X-Opaque-Id` was used as an HTTP header.
  190. The user ID is included in the `X-Opaque-ID` field in deprecation JSON logs.
  191. [source,js]
  192. ---------------------------
  193. {
  194. "type": "deprecation",
  195. "timestamp": "2019-08-30T12:07:07,126+02:00",
  196. "level": "WARN",
  197. "component": "o.e.d.r.a.a.i.RestCreateIndexAction",
  198. "cluster.name": "distribution_run",
  199. "node.name": "node-0",
  200. "message": "[types removal] Using include_type_name in create index requests is deprecated. The parameter will be removed in the next major version.",
  201. "x-opaque-id": "MY_USER_ID",
  202. "cluster.uuid": "Aq-c-PAeQiK3tfBYtig9Bw",
  203. "node.id": "D7fUYfnfTLa2D7y-xw6tZg"
  204. }
  205. ---------------------------
  206. // NOTCONSOLE
  207. [discrete]
  208. [[json-logging]]
  209. === JSON log format
  210. To make parsing Elasticsearch logs easier, logs are now printed in a JSON format.
  211. This is configured by a Log4J layout property `appender.rolling.layout.type = ECSJsonLayout`.
  212. This layout requires a `dataset` attribute to be set which is used to distinguish
  213. logs streams when parsing.
  214. [source,properties]
  215. --------------------------------------------------
  216. appender.rolling.layout.type = ECSJsonLayout
  217. appender.rolling.layout.dataset = elasticsearch.server
  218. --------------------------------------------------
  219. :es-json-layout-java-doc: {elasticsearch-javadoc}/org/elasticsearch/common/logging/ESJsonLayout.html
  220. Each line contains a single JSON document with the properties configured in `ECSJsonLayout`.
  221. See this class {es-json-layout-java-doc}[javadoc] for more details.
  222. However if a JSON document contains an exception, it will be printed over multiple lines.
  223. The first line will contain regular properties and subsequent lines will contain the
  224. stacktrace formatted as a JSON array.
  225. NOTE: You can still use your own custom layout. To do that replace the line
  226. `appender.rolling.layout.type` with a different layout. See sample below:
  227. [source,properties]
  228. --------------------------------------------------
  229. appender.rolling.type = RollingFile
  230. appender.rolling.name = rolling
  231. appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.log
  232. appender.rolling.layout.type = PatternLayout
  233. appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %.-10000m%n
  234. appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
  235. --------------------------------------------------