1
0

logging-config.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 = ESJsonLayout <3>
  35. appender.rolling.layout.type_name = 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> `type_name` is a flag populating the `type` field in a `ESJsonLayout`.
  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.transport` package has
  125. `logger.org.elasticsearch.transport` for logs related to communication between
  126. nodes.
  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`, `TRACE`, and `DEBUG`. The default log level is
  131. `INFO`.
  132. [source,console]
  133. ----
  134. PUT /_cluster/settings
  135. {
  136. "transient": {
  137. "logger.org.elasticsearch.transport": "TRACE"
  138. }
  139. }
  140. ----
  141. Other ways to change log levels include:
  142. 1. `elasticsearch.yml`:
  143. +
  144. --
  145. [source,yaml]
  146. ----
  147. logger.org.elasticsearch.transport: TRACE
  148. ----
  149. This is most appropriate when debugging a problem on a single node.
  150. --
  151. 2. `log4j2.properties`:
  152. +
  153. --
  154. [source,properties]
  155. ----
  156. logger.transport.level = trace
  157. ----
  158. This is most appropriate when you already need to change your Log4j 2
  159. configuration for other reasons. For example, you may want to send logs for a
  160. particular logger to another file. However, these use cases are rare.
  161. --
  162. [discrete]
  163. [[deprecation-logging]]
  164. === Deprecation logging
  165. In addition to regular logging, Elasticsearch allows you to enable logging
  166. of deprecated actions. For example this allows you to determine early, if
  167. you need to migrate certain functionality in the future. By default,
  168. deprecation logging is enabled at the WARN level, the level at which all
  169. deprecation log messages will be emitted.
  170. [source,properties]
  171. --------------------------------------------------
  172. logger.deprecation.level = warn
  173. --------------------------------------------------
  174. This will create a daily rolling deprecation log file in your log directory.
  175. Check this file regularly, especially when you intend to upgrade to a new
  176. major version.
  177. The default logging configuration has set the roll policy for the deprecation
  178. logs to roll and compress after 1 GB, and to preserve a maximum of five log
  179. files (four rolled logs, and the active log).
  180. You can disable it in the `config/log4j2.properties` file by setting the deprecation
  181. log level to `error` like this:
  182. [source,properties]
  183. --------------------------------------------------
  184. logger.deprecation.name = org.elasticsearch.deprecation
  185. logger.deprecation.level = error
  186. --------------------------------------------------
  187. You can identify what is triggering deprecated functionality if `X-Opaque-Id` was used as an HTTP header.
  188. The user ID is included in the `X-Opaque-ID` field in deprecation JSON logs.
  189. [source,js]
  190. ---------------------------
  191. {
  192. "type": "deprecation",
  193. "timestamp": "2019-08-30T12:07:07,126+02:00",
  194. "level": "WARN",
  195. "component": "o.e.d.r.a.a.i.RestCreateIndexAction",
  196. "cluster.name": "distribution_run",
  197. "node.name": "node-0",
  198. "message": "[types removal] Using include_type_name in create index requests is deprecated. The parameter will be removed in the next major version.",
  199. "x-opaque-id": "MY_USER_ID",
  200. "cluster.uuid": "Aq-c-PAeQiK3tfBYtig9Bw",
  201. "node.id": "D7fUYfnfTLa2D7y-xw6tZg"
  202. }
  203. ---------------------------
  204. // NOTCONSOLE
  205. [discrete]
  206. [[json-logging]]
  207. === JSON log format
  208. To make parsing Elasticsearch logs easier, logs are now printed in a JSON format.
  209. This is configured by a Log4J layout property `appender.rolling.layout.type = ESJsonLayout`.
  210. This layout requires a `type_name` attribute to be set which is used to distinguish
  211. logs streams when parsing.
  212. [source,properties]
  213. --------------------------------------------------
  214. appender.rolling.layout.type = ESJsonLayout
  215. appender.rolling.layout.type_name = server
  216. --------------------------------------------------
  217. :es-json-layout-java-doc: {elasticsearch-javadoc}/org/elasticsearch/common/logging/ESJsonLayout.html
  218. Each line contains a single JSON document with the properties configured in `ESJsonLayout`.
  219. See this class {es-json-layout-java-doc}[javadoc] for more details.
  220. However if a JSON document contains an exception, it will be printed over multiple lines.
  221. The first line will contain regular properties and subsequent lines will contain the
  222. stacktrace formatted as a JSON array.
  223. NOTE: You can still use your own custom layout. To do that replace the line
  224. `appender.rolling.layout.type` with a different layout. See sample below:
  225. [source,properties]
  226. --------------------------------------------------
  227. appender.rolling.type = RollingFile
  228. appender.rolling.name = rolling
  229. appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}_server.log
  230. appender.rolling.layout.type = PatternLayout
  231. appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %.-10000m%n
  232. appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
  233. --------------------------------------------------