-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Observed in downstream projects that other Log4j2 configurations were "winning" over our manual configuration, so we choose a very high priority (1e6) to force logging to match Barista's desired configuration. Unfortunately the approaach we're taking now won't allow any kind of tweaking or customization, so we might have to revisit down the road.
- Loading branch information
1 parent
e2ae6ad
commit 16be304
Showing
3 changed files
with
42 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,63 @@ | ||
package barista; | ||
|
||
import java.net.URI; | ||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.core.Filter.Result; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.core.appender.ConsoleAppender; | ||
import org.apache.logging.log4j.core.config.Configurator; | ||
import org.apache.logging.log4j.core.config.Configuration; | ||
import org.apache.logging.log4j.core.config.ConfigurationFactory; | ||
import org.apache.logging.log4j.core.config.ConfigurationSource; | ||
import org.apache.logging.log4j.core.config.Order; | ||
import org.apache.logging.log4j.core.config.builder.api.AppenderComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; | ||
import org.apache.logging.log4j.core.config.builder.api.FilterComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.api.LoggerComponentBuilder; | ||
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
|
||
@Plugin(name = "BaristaLogging", category = ConfigurationFactory.CATEGORY) | ||
@Order(1_000_000) | ||
final class Logging extends ConfigurationFactory { | ||
|
||
final class Logging { | ||
private static final String STDOUT = "stdout"; | ||
public static final String[] SUPPORTED_TYPES = {"*"}; | ||
|
||
public static void configure() { | ||
ConfigurationBuilder<BuiltConfiguration> builder = | ||
ConfigurationBuilderFactory.newConfigurationBuilder() | ||
.setStatusLevel(Level.ERROR) | ||
.setConfigurationName("barista"); | ||
static BuiltConfiguration createConfiguration( | ||
String name, ConfigurationBuilder<BuiltConfiguration> builder) { | ||
builder.setStatusLevel(Level.ERROR); | ||
builder.setConfigurationName(name); | ||
|
||
LayoutComponentBuilder layout = | ||
builder.newLayout("PatternLayout") | ||
.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable"); | ||
FilterComponentBuilder filter = | ||
builder.newFilter("MarkerFilter", Result.DENY, Result.NEUTRAL) | ||
.addAttribute("marker", "FLOW"); | ||
|
||
AppenderComponentBuilder appenderBuilder = | ||
builder.newAppender(STDOUT, "CONSOLE") | ||
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT) | ||
.add(layout) | ||
.add(filter); | ||
.add(layout); | ||
|
||
builder.add(appenderBuilder); | ||
builder.add( | ||
builder.newRootLogger(Level.INFO) | ||
.add(builder.newAppenderRef(appenderBuilder.getName()))); | ||
|
||
return builder.build(); | ||
} | ||
|
||
LoggerComponentBuilder logger = | ||
builder.newLogger("org.apache.logging.log4j", Level.DEBUG) | ||
.add(builder.newAppenderRef(STDOUT)) | ||
.addAttribute("additivity", false); | ||
@Override | ||
public Configuration getConfiguration( | ||
final LoggerContext loggerContext, final ConfigurationSource source) { | ||
return getConfiguration(loggerContext, source.toString(), null); | ||
} | ||
|
||
builder.add(appenderBuilder) | ||
.add(logger) | ||
.add(builder.newRootLogger(Level.INFO).add(builder.newAppenderRef(STDOUT))); | ||
@Override | ||
public Configuration getConfiguration( | ||
final LoggerContext loggerContext, final String name, final URI configLocation) { | ||
ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder(); | ||
return createConfiguration(name, builder); | ||
} | ||
|
||
Configurator.initialize(builder.build()); | ||
// TODO(markelliot): we may want to stop the logging context on server shutdown | ||
@Override | ||
protected String[] getSupportedTypes() { | ||
return SUPPORTED_TYPES; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters