Skip to content

Commit 86e9438

Browse files
authored
Prevent excessive disk consumption by log files
This commit enables management of the main Elasticsearch log files out-of-the-box by the following changes: - compress rolled logs - roll logs every 128 MB - maintain a sliding window of logs - remove the oldest logs maintaining no more than 2 GB of compressed logs on disk Relates #25660
1 parent 8b846f9 commit 86e9438

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

distribution/src/main/resources/config/log4j2.properties

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ appender.rolling.name = rolling
1414
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log
1515
appender.rolling.layout.type = PatternLayout
1616
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
17-
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}.log
17+
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz
1818
appender.rolling.policies.type = Policies
1919
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
2020
appender.rolling.policies.time.interval = 1
2121
appender.rolling.policies.time.modulate = true
22+
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
23+
appender.rolling.policies.size.size = 128MB
24+
appender.rolling.strategy.type = DefaultRolloverStrategy
25+
appender.rolling.strategy.fileIndex = nomax
26+
appender.rolling.strategy.action.type = Delete
27+
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
28+
appender.rolling.strategy.action.condition.type = IfFileName
29+
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-*
30+
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize
31+
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB
2232

2333
rootLogger.level = info
2434
rootLogger.appenderRef.console.ref = console

docs/reference/setup/configuration.asciidoc

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,47 @@ appender.rolling.type = RollingFile <1>
117117
appender.rolling.name = rolling
118118
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log <2>
119119
appender.rolling.layout.type = PatternLayout
120-
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %.10000m%n
121-
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}.log <3>
120+
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c{1.}] %marker%.-10000m%n
121+
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}-%i.log.gz <3>
122122
appender.rolling.policies.type = Policies
123123
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy <4>
124124
appender.rolling.policies.time.interval = 1 <5>
125125
appender.rolling.policies.time.modulate = true <6>
126+
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy <7>
127+
appender.rolling.policies.size.size = 256MB <8>
128+
appender.rolling.strategy.type = DefaultRolloverStrategy
129+
appender.rolling.strategy.fileIndex = nomax
130+
appender.rolling.strategy.action.type = Delete <9>
131+
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}
132+
appender.rolling.strategy.action.condition.type = IfFileName <10>
133+
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* <11>
134+
appender.rolling.strategy.action.condition.nested_condition.type = IfAccumulatedFileSize <12>
135+
appender.rolling.strategy.action.condition.nested_condition.exceeds = 2GB <13>
126136
--------------------------------------------------
127137

128138
<1> Configure the `RollingFile` appender
129139
<2> Log to `/var/log/elasticsearch/production.log`
130-
<3> Roll logs to `/var/log/elasticsearch/production-yyyy-MM-dd.log`
131-
<4> Using a time-based roll policy
140+
<3> Roll logs to `/var/log/elasticsearch/production-yyyy-MM-dd-i.log`; logs
141+
will be compressed on each roll and `i` will be incremented
142+
<4> Use a time-based roll policy
132143
<5> Roll logs on a daily basis
133144
<6> Align rolls on the day boundary (as opposed to rolling every twenty-four
134145
hours)
146+
<7> Using a size-based roll policy
147+
<8> Roll logs after 256 MB
148+
<9> Use a delete action when rolling logs
149+
<10> Only delete logs matching a file pattern
150+
<11> The pattern is to only delete the main logs
151+
<12> Only delete if we have accumulated too many compressed logs
152+
<13> The size condition on the compressed logs is 2 GB
135153

136154
NOTE: Log4j's configuration parsing gets confused by any extraneous whitespace;
137155
if you copy and paste any Log4j settings on this page, or enter any Log4j
138156
configuration in general, be sure to trim any leading and trailing whitespace.
139157

140-
If you append `.gz` or `.zip` to `appender.rolling.filePattern`, then the logs
141-
will be compressed as they are rolled.
158+
Note than you can replace `.gz` by `.zip` in `appender.rolling.filePattern` to
159+
compress the rolled logs using the zip format. If you remove the `.gz`
160+
extension then logs will not be compressed as they are rolled.
142161

143162
If you want to retain log files for a specified period of time, you can use a
144163
rollover strategy with a delete action.
@@ -148,22 +167,22 @@ rollover strategy with a delete action.
148167
appender.rolling.strategy.type = DefaultRolloverStrategy <1>
149168
appender.rolling.strategy.action.type = Delete <2>
150169
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path} <3>
151-
appender.rolling.strategy.action.condition.type = IfLastModified <4>
152-
appender.rolling.strategy.action.condition.age = 7D <5>
153-
appender.rolling.strategy.action.PathConditions.type = IfFileName <6>
154-
appender.rolling.strategy.action.PathConditions.glob = ${sys:es.logs.cluster_name}-* <7>
170+
appender.rolling.strategy.action.condition.type = IfFileName <4>
171+
appender.rolling.strategy.action.condition.glob = ${sys:es.logs.cluster_name}-* <5>
172+
appender.rolling.strategy.action.condition.nested_condition.type = IfLastModified <6>
173+
appender.rolling.strategy.action.condition.nested_condition.age = 7D <7>
155174
--------------------------------------------------
156175

157176
<1> Configure the `DefaultRolloverStrategy`
158177
<2> Configure the `Delete` action for handling rollovers
159178
<3> The base path to the Elasticsearch logs
160179
<4> The condition to apply when handling rollovers
161-
<5> Retain logs for seven days
162-
<6> Only delete files older than seven days if they match the specified glob
163-
<7> Delete files from the base path matching the glob
180+
<5> Delete files from the base path matching the glob
164181
`${sys:es.logs.cluster_name}-*`; this is the glob that log files are rolled
165182
to; this is needed to only delete the rolled Elasticsearch logs but not also
166183
delete the deprecation and slow logs
184+
<6> A nested condition to apply to files matching the glob
185+
<7> Retain logs for seven days
167186

168187
Multiple configuration files can be loaded (in which case they will get merged)
169188
as long as they are named `log4j2.properties` and have the Elasticsearch config

0 commit comments

Comments
 (0)