package org.apache.logging.log4j.core.appender;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.net.Advertiser;
import org.apache.logging.log4j.core.util.Booleans;
import org.apache.logging.log4j.core.util.Integers;
@Plugin(name = "MemoryMappedFile", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
public final class MemoryMappedFileAppender extends AbstractOutputStreamAppender<MemoryMappedFileManager> {
public static class Builder<B extends Builder<B>> extends AbstractOutputStreamAppender.Builder<B>
implements org.apache.logging.log4j.core.util.Builder<MemoryMappedFileAppender> {
@PluginBuilderAttribute("fileName")
private String fileName;
@PluginBuilderAttribute("append")
private boolean append = true;
@PluginBuilderAttribute("regionLength")
private int regionLength = MemoryMappedFileManager.DEFAULT_REGION_LENGTH;
@PluginBuilderAttribute("advertise")
private boolean advertise;
@PluginBuilderAttribute("advertiseURI")
private String advertiseURI;
@Override
public MemoryMappedFileAppender build() {
final String name = getName();
final int actualRegionLength = determineValidRegionLength(name, regionLength);
if (name == null) {
LOGGER.error("No name provided for MemoryMappedFileAppender");
return null;
}
if (fileName == null) {
LOGGER.error("No filename provided for MemoryMappedFileAppender with name " + name);
return null;
}
final Layout<? extends Serializable> layout = getOrCreateLayout();
final MemoryMappedFileManager manager = MemoryMappedFileManager.getFileManager(fileName, append, isImmediateFlush(),
actualRegionLength, advertiseURI, layout);
if (manager == null) {
return null;
}
return new MemoryMappedFileAppender(name, layout, getFilter(), manager, fileName, isIgnoreExceptions(), false,
advertise ? getConfiguration().getAdvertiser() : null, getPropertyArray());
}
public B setFileName(final String fileName) {
this.fileName = fileName;
return asBuilder();
}
public B setAppend(final boolean append) {
this.append = append;
return asBuilder();
}
public B setRegionLength(final int regionLength) {
this.regionLength = regionLength;
return asBuilder();
}
public B setAdvertise(final boolean advertise) {
this.advertise = advertise;
return asBuilder();
}
public B setAdvertiseURI(final String advertiseURI) {
this.advertiseURI = advertiseURI;
return asBuilder();
}
}
private static final int BIT_POSITION_1GB = 30;
private static final int MAX_REGION_LENGTH = 1 << BIT_POSITION_1GB;
private static final int MIN_REGION_LENGTH = 256;
private final String fileName;
private Object advertisement;
private final Advertiser advertiser;
private MemoryMappedFileAppender(final String name, final Layout<? extends Serializable> layout,
final Filter filter, final MemoryMappedFileManager manager, final String filename,
final boolean ignoreExceptions, final boolean immediateFlush, final Advertiser advertiser,
final Property[] properties) {
super(name, layout, filter, ignoreExceptions, immediateFlush, properties, manager);
if (advertiser != null) {
final Map<String, String> configuration = new HashMap<>(layout.getContentFormat());
configuration.putAll(manager.getContentFormat());
configuration.put("contentType", layout.getContentType());
configuration.put("name", name);
advertisement = advertiser.advertise(configuration);
}
this.fileName = filename;
this.advertiser = advertiser;
}
@Override
public boolean stop(final long timeout, final TimeUnit timeUnit) {
setStopping();
super.stop(timeout, timeUnit, false);
if (advertiser != null) {
advertiser.unadvertise(advertisement);
}
setStopped();
return true;
}
@Override
public void append(final LogEvent event) {
getManager().setEndOfBatch(event.isEndOfBatch());
super.append(event);
}
public String getFileName() {
return this.fileName;
}
public int getRegionLength() {
return getManager().getRegionLength();
}
@Deprecated
public static <B extends Builder<B>> MemoryMappedFileAppender createAppender(
final String fileName,
final String append,
final String name,
final String immediateFlush,
final String regionLengthStr,
final String ignore,
final Layout<? extends Serializable> layout,
final Filter filter,
final String advertise,
final String advertiseURI,
final Configuration config) {
final boolean isAppend = Booleans.parseBoolean(append, true);
final boolean isImmediateFlush = Booleans.parseBoolean(immediateFlush, false);
final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
final boolean isAdvertise = Boolean.parseBoolean(advertise);
final int regionLength = Integers.parseInt(regionLengthStr, MemoryMappedFileManager.DEFAULT_REGION_LENGTH);
return MemoryMappedFileAppender.<B>newBuilder()
.setAdvertise(isAdvertise)
.setAdvertiseURI(advertiseURI)
.setAppend(isAppend)
.setConfiguration(config)
.setFileName(fileName).setFilter(filter).setIgnoreExceptions(ignoreExceptions)
.withImmediateFlush(isImmediateFlush).setLayout(layout).setName(name)
.setRegionLength(regionLength)
.build();
}
@PluginBuilderFactory
public static <B extends Builder<B>> B newBuilder() {
return new Builder<B>().asBuilder();
}
private static int determineValidRegionLength(final String name, final int regionLength) {
if (regionLength > MAX_REGION_LENGTH) {
LOGGER.info("MemoryMappedAppender[{}] Reduced region length from {} to max length: {}", name, regionLength,
MAX_REGION_LENGTH);
return MAX_REGION_LENGTH;
}
if (regionLength < MIN_REGION_LENGTH) {
LOGGER.info("MemoryMappedAppender[{}] Expanded region length from {} to min length: {}", name, regionLength,
MIN_REGION_LENGTH);
return MIN_REGION_LENGTH;
}
final int result = Integers.ceilingNextPowerOfTwo(regionLength);
if (regionLength != result) {
LOGGER.info("MemoryMappedAppender[{}] Rounded up region length from {} to next power of two: {}", name,
regionLength, result);
}
return result;
}
}