LogstashEncoderBuilder.java

package io.jstach.rainbowgum.json.encoder;

import io.jstach.rainbowgum.LogProperties;
import io.jstach.rainbowgum.LogProperty;

/**
 * Builder to create {@link io.jstach.rainbowgum.json.encoder.LogstashEncoder }.
 * Creates a Logstash encoder.
 * <table class="table">
 * <caption><strong>Properties retrieved from LogProperties</strong></caption>
 * <tr>
 * <th>Property Pattern</th>
 * <th>Type</th>
 * <th>Required</th>
 * <th>Default</th>
 * <th>Description</th>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_zoneId }</code></td>
 * <td><code>String (converted)</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td>Zone used for <code>@timestamp</code>, defaults to the system default zone.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_prettyPrint }</code></td>
 * <td><code>Boolean</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td><code>true</code> will pretty print the JSON, default is false.</td>
 * </tr>
 * <tr>
 * <td><code>{@value PROPERTY_maxBufferSize }</code></td>
 * <td><code>Integer</code></td>
 * <td><code>false</code></td>
 * <td><code>null</code></td>
 * <td>Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.</td>
 * </tr>
 * </table>
 */
public final class LogstashEncoderBuilder implements io.jstach.rainbowgum.LogBuilder<LogstashEncoderBuilder,io.jstach.rainbowgum.json.encoder.LogstashEncoder> {

	/**
	 * The properties to be retrieved from config will have
	 * this prefix.
	 */
	static final String PROPERTY_PREFIX = "logging.appender.{name}.encoder.logstash.";
	
	/**
	 * <code>{@value #PROPERTY_zoneId } = java.time.ZoneId</code> Zone used for <code>@timestamp</code>, defaults to the system default zone.
	 */
	static final String PROPERTY_zoneId = PROPERTY_PREFIX + "zoneId";
	/**
	 * <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 */
	static final String PROPERTY_prettyPrint = PROPERTY_PREFIX + "prettyPrint";
	/**
	 * <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 */
	static final String PROPERTY_maxBufferSize = PROPERTY_PREFIX + "maxBufferSize";
	
	private final String propertyPrefix;
	final String property_zoneId;
	final String property_prettyPrint;
	final String property_maxBufferSize;
	
	private final java.lang.String name;
	private java.time.@org.jspecify.annotations.Nullable ZoneId zoneId = null;
	private java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint = null;
	private java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize = null;
	
	/**
	 * Create a builder for {@code io.jstach.rainbowgum.json.encoder.LogstashEncoder }.
	 * @param name will fill <code>{name}</code> in <code>logging.appender.{name}.encoder.logstash.</code>. Property name prefix.
	 */
	public LogstashEncoderBuilder(
			java.lang.String name
			) {
		try {
			java.util.Map<String,String> prefixParameters = java.util.Map.of(
					"name", name
				);
			this.propertyPrefix = LogProperties.interpolateKey(PROPERTY_PREFIX, prefixParameters);
			this.name = name;
			property_zoneId = LogProperties.interpolateKey(PROPERTY_zoneId, prefixParameters);
			property_prettyPrint = LogProperties.interpolateKey(PROPERTY_prettyPrint, prefixParameters);
			property_maxBufferSize = LogProperties.interpolateKey(PROPERTY_maxBufferSize, prefixParameters);
		}
		catch (IllegalArgumentException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}


	
	/**
	 * Sets zoneId.
	 * Zone used for <code>@timestamp</code>, defaults to the system default zone.
	 * Default is <code>null</code>.
	 * @param zoneId <code>{@value #PROPERTY_zoneId } = java.time.ZoneId</code> Zone used for <code>@timestamp</code>, defaults to the system default zone.
	 * @return this builder.
	 */
	public LogstashEncoderBuilder zoneId(java.time.@org.jspecify.annotations.Nullable ZoneId zoneId) {
		this.zoneId = zoneId;
		return this;
	}
	
	/**
	 * Sets prettyPrint.
	 * <code>true</code> will pretty print the JSON, default is false.
	 * Default is <code>null</code>.
	 * @param prettyPrint <code>{@value #PROPERTY_prettyPrint } = java.lang.Boolean</code> <code>true</code> will pretty print the JSON, default is false.
	 * @return this builder.
	 */
	public LogstashEncoderBuilder prettyPrint(java.lang.@org.jspecify.annotations.Nullable Boolean prettyPrint) {
		this.prettyPrint = prettyPrint;
		return this;
	}
	
	/**
	 * Sets maxBufferSize.
	 * Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * Default is <code>null</code>.
	 * @param maxBufferSize <code>{@value #PROPERTY_maxBufferSize } = java.lang.Integer</code> Maximum buffer size - a soft ceiling checked between events, not a hard cap enforced on any single event (see {@link io.jstach.rainbowgum.LogEncoder.Buffer#isOversized()}). A negative value (the default) disables this entirely.
	 * @return this builder.
	 */
	public LogstashEncoderBuilder maxBufferSize(java.lang.@org.jspecify.annotations.Nullable Integer maxBufferSize) {
		this.maxBufferSize = maxBufferSize;
		return this;
	}
	
	/**
	 * Creates {@code io.jstach.rainbowgum.json.encoder.LogstashEncoder } from this builder.
	 * @return {@code io.jstach.rainbowgum.json.encoder.LogstashEncoder }.
	 */
	public io.jstach.rainbowgum.json.encoder.LogstashEncoder build() {
		try {
			return io.jstach.rainbowgum.json.encoder.LogstashEncoder.of(
					this.name
					, this.zoneId
					, this.prettyPrint
					, this.maxBufferSize
					);
		}
		catch (IllegalArgumentException | LogProperty.PropertyMissingException e) {
			throw LogProperty.ValidationException.of(this.getClass(), e);
		}
	}
	
	@Override
	public LogstashEncoderBuilder fromProperties(LogProperties properties) {
		var __v = LogProperty.Validator.of(this.getClass());
		var _zoneId = properties.forKey(property_zoneId).ofString().map(_v -> io.jstach.rainbowgum.json.encoder.LogstashEncoder.convertZoneId(_v)).or(this.zoneId).validateIfError(__v);
		var _prettyPrint = properties.forKey(property_prettyPrint).ofBoolean().or(this.prettyPrint).validateIfError(__v);
		var _maxBufferSize = properties.forKey(property_maxBufferSize).ofInt().or(this.maxBufferSize).validateIfError(__v);
		__v.validate();
		this.zoneId = _zoneId.valueOrNull();
		this.prettyPrint = _prettyPrint.valueOrNull();
		this.maxBufferSize = _maxBufferSize.valueOrNull();
		return this;
	}
	
	/**
	 * The interpolated property prefix: {@value #PROPERTY_PREFIX}.
	 * @return resolved prefix which should end with a "<code>.</code>".
	 */
	@Override
	public String propertyPrefix() {
		return this.propertyPrefix;
	}
	
}