logging-config.asciidoc 12 KB

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