1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender;
18
19 import java.io.Serializable;
20 import java.util.HashMap;
21 import java.util.Map;
22
23 import org.apache.logging.log4j.core.Filter;
24 import org.apache.logging.log4j.core.Layout;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.config.Configuration;
27 import org.apache.logging.log4j.core.config.plugins.Plugin;
28 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
30 import org.apache.logging.log4j.core.config.plugins.PluginElement;
31 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32 import org.apache.logging.log4j.core.layout.PatternLayout;
33 import org.apache.logging.log4j.core.net.Advertiser;
34 import org.apache.logging.log4j.core.util.Booleans;
35 import org.apache.logging.log4j.core.util.Integers;
36
37
38
39
40
41
42 @Plugin(name = "MemoryMappedFile", category = "Core", elementType = "appender", printObject = true)
43 public final class MemoryMappedFileAppender extends AbstractOutputStreamAppender<MemoryMappedFileManager> {
44
45 private static final int BIT_POSITION_1GB = 30;
46 private static final int MAX_REGION_LENGTH = 1 << BIT_POSITION_1GB;
47 private static final int MIN_REGION_LENGTH = 256;
48
49 private final String fileName;
50 private Object advertisement;
51 private final Advertiser advertiser;
52
53 private MemoryMappedFileAppender(final String name, final Layout<? extends Serializable> layout,
54 final Filter filter, final MemoryMappedFileManager manager, final String filename,
55 final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) {
56 super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
57 if (advertiser != null) {
58 final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
59 configuration.putAll(manager.getContentFormat());
60 configuration.put("contentType", layout.getContentType());
61 configuration.put("name", name);
62 advertisement = advertiser.advertise(configuration);
63 }
64 this.fileName = filename;
65 this.advertiser = advertiser;
66 }
67
68 @Override
69 public void stop() {
70 super.stop();
71 if (advertiser != null) {
72 advertiser.unadvertise(advertisement);
73 }
74 }
75
76
77
78
79
80
81 @Override
82 public void append(final LogEvent event) {
83
84
85
86
87
88
89
90 getManager().setEndOfBatch(event.isEndOfBatch());
91 super.append(event);
92 }
93
94
95
96
97
98
99 public String getFileName() {
100 return this.fileName;
101 }
102
103
104
105
106
107
108 public int getRegionLength() {
109 return getManager().getRegionLength();
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132 @PluginFactory
133 public static MemoryMappedFileAppender createAppender(
134
135 @PluginAttribute("fileName") final String fileName,
136 @PluginAttribute("append") final String append,
137 @PluginAttribute("name") final String name,
138 @PluginAttribute("immediateFlush") final String immediateFlush,
139 @PluginAttribute("regionLength") final String regionLengthStr,
140 @PluginAttribute("ignoreExceptions") final String ignore,
141 @PluginElement("Layout") Layout<? extends Serializable> layout,
142 @PluginElement("Filter") final Filter filter,
143 @PluginAttribute("advertise") final String advertise,
144 @PluginAttribute("advertiseURI") final String advertiseURI,
145 @PluginConfiguration final Configuration config) {
146
147
148 final boolean isAppend = Booleans.parseBoolean(append, true);
149 final boolean isForce = Booleans.parseBoolean(immediateFlush, false);
150 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
151 final boolean isAdvertise = Boolean.parseBoolean(advertise);
152 final int regionLength = Integers.parseInt(regionLengthStr, MemoryMappedFileManager.DEFAULT_REGION_LENGTH);
153 final int actualRegionLength = determineValidRegionLength(name, regionLength);
154
155 if (name == null) {
156 LOGGER.error("No name provided for MemoryMappedFileAppender");
157 return null;
158 }
159
160 if (fileName == null) {
161 LOGGER.error("No filename provided for MemoryMappedFileAppender with name " + name);
162 return null;
163 }
164 if (layout == null) {
165 layout = PatternLayout.createDefaultLayout();
166 }
167 final MemoryMappedFileManager manager = MemoryMappedFileManager.getFileManager(fileName, isAppend, isForce,
168 actualRegionLength, advertiseURI, layout);
169 if (manager == null) {
170 return null;
171 }
172
173 return new MemoryMappedFileAppender(name, layout, filter, manager, fileName, ignoreExceptions, false,
174 isAdvertise ? config.getAdvertiser() : null);
175 }
176
177
178
179
180 private static int determineValidRegionLength(final String name, final int regionLength) {
181 if (regionLength > MAX_REGION_LENGTH) {
182 LOGGER.info("MemoryMappedAppender[{}] Reduced region length from {} to max length: {}", name, regionLength,
183 MAX_REGION_LENGTH);
184 return MAX_REGION_LENGTH;
185 }
186 if (regionLength < MIN_REGION_LENGTH) {
187 LOGGER.info("MemoryMappedAppender[{}] Expanded region length from {} to min length: {}", name, regionLength,
188 MIN_REGION_LENGTH);
189 return MIN_REGION_LENGTH;
190 }
191 final int result = Integers.ceilingNextPowerOfTwo(regionLength);
192 if (regionLength != result) {
193 LOGGER.info("MemoryMappedAppender[{}] Rounded up region length from {} to next power of two: {}", name,
194 regionLength, result);
195 }
196 return result;
197 }
198 }