Package io.jstach.rainbowgum.pattern.format


@NonNullByDefault package io.jstach.rainbowgum.pattern.format
Provides Logback style pattern formatters. The URI scheme of pattern encoders is "pattern".

The supported builtin keywords are in the follow enum types:

Rainbow Gum does not currently support all of the builtin keywords that Logback does! But most of them are available.

Adding a custom keyword

Extend PatternKeywordProvider and register a PatternFormatterFactory.KeywordFactory (or PatternFormatterFactory.CompositeFactory if the keyword should accept a child pattern like %keyword(child)) with the PatternRegistry. This example adds %hostname, resolved once rather than on every event:
public class CustomPatternKeywordExample extends PatternKeywordProvider {

	@Override
	protected void register(PatternRegistry patternRegistry) {
		// Adds "%hostname" as a usable keyword in patterns.
		patternRegistry.keyword(PatternKey.of("hostname"), (config, node) -> HostnameFormatter.INSTANCE);
	}

	enum HostnameFormatter implements EventFormatter {

		INSTANCE;

		// Resolved once instead of on every event.
		private static final String HOSTNAME = hostname();

		private static String hostname() {
			try {
				return InetAddress.getLocalHost().getHostName();
			}
			catch (UnknownHostException e) {
				return "unknown";
			}
		}

		@Override
		public void format(StringBuilder output, LogEvent event) {
			output.append(HOSTNAME);
		}

	}

}
Register it like any other RainbowGumServiceProvider. If your application is modularized:

provides io.jstach.rainbowgum.spi.RainbowGumServiceProvider with com.mycompany.CustomPatternKeywordExample;

Configuring PatternConfig

PatternConfig carries platform specific settings (time zone, line separator, whether ANSI is disabled, the %r start time) that keywords need. Prefer property configuration (see the user guide's Pattern Module section) since it is resolved per encoder name and needs no code. To instead set a programmatic default used by every encoder that has no more specific property configuration, register a PatternConfig - which is itself a RainbowGumServiceProvider.Configurator - with LogConfig.Builder.configurator(io.jstach.rainbowgum.spi.RainbowGumServiceProvider.Configurator):
LogConfig.Builder configure(LogConfig.Builder builder) {
	return builder.configurator(PatternConfig.builder() //
		.zoneId(ZoneOffset.UTC) //
		.ansiDisabled(true) //
		.build());
}