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 import java.util.zip.Deflater;
23
24 import org.apache.logging.log4j.core.Filter;
25 import org.apache.logging.log4j.core.Layout;
26 import org.apache.logging.log4j.core.LogEvent;
27 import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
28 import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
29 import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
30 import org.apache.logging.log4j.core.appender.rolling.TriggeringPolicy;
31 import org.apache.logging.log4j.core.config.Configuration;
32 import org.apache.logging.log4j.core.config.plugins.Plugin;
33 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
34 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
35 import org.apache.logging.log4j.core.config.plugins.PluginElement;
36 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
37 import org.apache.logging.log4j.core.layout.PatternLayout;
38 import org.apache.logging.log4j.core.net.Advertiser;
39 import org.apache.logging.log4j.core.util.Booleans;
40 import org.apache.logging.log4j.core.util.Integers;
41
42
43
44
45 @Plugin(name = "RollingFile", category = "Core", elementType = "appender", printObject = true)
46 public final class RollingFileAppender extends AbstractOutputStreamAppender<RollingFileManager> {
47
48 private static final int DEFAULT_BUFFER_SIZE = 8192;
49 private static final long serialVersionUID = 1L;
50
51 private final String fileName;
52 private final String filePattern;
53 private Object advertisement;
54 private final Advertiser advertiser;
55
56
57 private RollingFileAppender(final String name, final Layout<? extends Serializable> layout, final Filter filter,
58 final RollingFileManager manager, final String fileName, final String filePattern,
59 final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser) {
60 super(name, layout, filter, ignoreExceptions, immediateFlush, manager);
61 if (advertiser != null) {
62 final Map<String, String> configuration = new HashMap<String, String>(layout.getContentFormat());
63 configuration.put("contentType", layout.getContentType());
64 configuration.put("name", name);
65 advertisement = advertiser.advertise(configuration);
66 }
67 this.fileName = fileName;
68 this.filePattern = filePattern;
69 this.advertiser = advertiser;
70 }
71
72 @Override
73 public void stop() {
74 super.stop();
75 if (advertiser != null) {
76 advertiser.unadvertise(advertisement);
77 }
78 }
79
80
81
82
83
84
85 @Override
86 public void append(final LogEvent event) {
87 getManager().checkRollover(event);
88 super.append(event);
89 }
90
91
92
93
94
95 public String getFileName() {
96 return fileName;
97 }
98
99
100
101
102
103 public String getFilePattern() {
104 return filePattern;
105 }
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 @PluginFactory
129 public static RollingFileAppender createAppender(
130 @PluginAttribute("fileName") final String fileName,
131 @PluginAttribute("filePattern") final String filePattern,
132 @PluginAttribute("append") final String append,
133 @PluginAttribute("name") final String name,
134 @PluginAttribute("bufferedIO") final String bufferedIO,
135 @PluginAttribute("bufferSize") final String bufferSizeStr,
136 @PluginAttribute("immediateFlush") final String immediateFlush,
137 @PluginElement("Policy") final TriggeringPolicy policy,
138 @PluginElement("Strategy") RolloverStrategy strategy,
139 @PluginElement("Layout") Layout<? extends Serializable> layout,
140 @PluginElement("Filter") final Filter filter,
141 @PluginAttribute("ignoreExceptions") final String ignore,
142 @PluginAttribute("advertise") final String advertise,
143 @PluginAttribute("advertiseURI") final String advertiseURI,
144 @PluginConfiguration final Configuration config) {
145
146 final boolean isAppend = Booleans.parseBoolean(append, true);
147 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
148 final boolean isBuffered = Booleans.parseBoolean(bufferedIO, true);
149 final boolean isFlush = Booleans.parseBoolean(immediateFlush, true);
150 final boolean isAdvertise = Boolean.parseBoolean(advertise);
151 final int bufferSize = Integers.parseInt(bufferSizeStr, DEFAULT_BUFFER_SIZE);
152 if (!isBuffered && bufferSize > 0) {
153 LOGGER.warn("The bufferSize is set to {} but bufferedIO is not true: {}", bufferSize, bufferedIO);
154 }
155 if (name == null) {
156 LOGGER.error("No name provided for FileAppender");
157 return null;
158 }
159
160 if (fileName == null) {
161 LOGGER.error("No filename was provided for FileAppender with name " + name);
162 return null;
163 }
164
165 if (filePattern == null) {
166 LOGGER.error("No filename pattern provided for FileAppender with name " + name);
167 return null;
168 }
169
170 if (policy == null) {
171 LOGGER.error("A TriggeringPolicy must be provided");
172 return null;
173 }
174
175 if (strategy == null) {
176 strategy = DefaultRolloverStrategy.createStrategy(null, null, null,
177 String.valueOf(Deflater.DEFAULT_COMPRESSION), config);
178 }
179
180 if (layout == null) {
181 layout = PatternLayout.createDefaultLayout();
182 }
183
184 final RollingFileManager manager = RollingFileManager.getFileManager(fileName, filePattern, isAppend,
185 isBuffered, policy, strategy, advertiseURI, layout, bufferSize);
186 if (manager == null) {
187 return null;
188 }
189
190 return new RollingFileAppender(name, layout, filter, manager, fileName, filePattern,
191 ignoreExceptions, isFlush, isAdvertise ? config.getAdvertiser() : null);
192 }
193 }