org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder Java Examples
The following examples show how to use
org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RollingFileAppenderUpdateDataTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final String filePattern) { final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.setConfigurationName("LOG4J2-1964 demo"); builder.setStatusLevel(Level.ERROR); // @formatter:off builder.add(builder.newAppender("consoleLog", "Console") .addAttribute("target", ConsoleAppender.Target.SYSTEM_ERR)); builder.add(builder.newAppender("fooAppender", "RollingFile") .addAttribute("fileName", "target/rolling-update-date/foo.log") .addAttribute("filePattern", filePattern) .addComponent(builder.newComponent("SizeBasedTriggeringPolicy") .addAttribute("size", "10MB"))); builder.add(builder.newRootLogger(Level.INFO) .add(builder.newAppenderRef("consoleLog")) .add(builder.newAppenderRef("fooAppender"))); // @formatter:on return builder; }
Example #2
Source File: Log4j2ConfigurationFactory.java From apm-agent-java with Apache License 2.0 | 6 votes |
private AppenderComponentBuilder createFileAppender(ConfigurationBuilder<BuiltConfiguration> builder, String logFile, LayoutComponentBuilder layout) { ByteValue size = ByteValue.of(getValue("log_file_size", sources, LoggingConfiguration.DEFAULT_MAX_SIZE)); return builder.newAppender("rolling", "RollingFile") .addAttribute("fileName", logFile) .addAttribute("filePattern", logFile + ".%i") .add(layout) .addComponent(builder.newComponent("Policies") .addComponent(builder.newComponent("SizeBasedTriggeringPolicy").addAttribute("size", size.getBytes() + "B"))) // Always keep exactly one history file. // This is needed to ensure that the rest of the file can be sent when its rotated. // Storing multiple history files would give the false impression that, for example, // when currently reading from apm.log2, the reading would continue from apm.log1. // This is not the case, when apm.log2 is fully read, the reading will continue from apm.log. // That is because we don't want to require the reader having to know the file name pattern of the rotated file. .addComponent(builder.newComponent("DefaultRolloverStrategy").addAttribute("max", 1)); }
Example #3
Source File: Log4j2ConfigurationFactory.java From apm-agent-java with Apache License 2.0 | 6 votes |
private List<AppenderComponentBuilder> createAppenders(ConfigurationBuilder<BuiltConfiguration> builder) { List<AppenderComponentBuilder> appenders = new ArrayList<>(); String logFile = getActualLogFile(ElasticApmAgent.getAgentHome(), getValue(LOG_FILE_KEY, sources, getValue(DEPRECATED_LOG_FILE_KEY, sources, DEFAULT_LOG_FILE))); if (logFile.equals(SYSTEM_OUT)) { appenders.add(createConsoleAppender(builder)); if (Boolean.parseBoolean(getValue(SHIP_AGENT_LOGS, sources, Boolean.TRUE.toString()))) { File tempLog = getTempLogFile(ephemeralId); tempLog.deleteOnExit(); File rotatedTempLog = new File(tempLog + ".1"); rotatedTempLog.deleteOnExit(); appenders.add(createFileAppender(builder, tempLog.getAbsolutePath(), createLayout(builder, LogFormat.JSON))); } } else { appenders.add(createFileAppender(builder, logFile, createLayout(builder, getFileLogFormat()))); } for (AppenderComponentBuilder appender : appenders) { builder.add(appender); } return appenders; }
Example #4
Source File: LoggerFactory.java From lucene-solr with Apache License 2.0 | 6 votes |
public static void initGuiLogging(String logFile) { ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.add(builder.newRootLogger(Level.INFO)); LoggerContext context = Configurator.initialize(builder.build()); PatternLayout layout = PatternLayout.newBuilder() .withPattern("[%d{ISO8601}] %5p (%F:%L) - %m%n") .withCharset(StandardCharsets.UTF_8) .build(); Appender fileAppender = FileAppender.newBuilder() .setName("File") .setLayout(layout) .withFileName(logFile) .withAppend(false) .build(); fileAppender.start(); Appender textAreaAppender = TextAreaAppender.newBuilder() .setName("TextArea") .setLayout(layout) .build(); textAreaAppender.start(); context.getRootLogger().addAppender(fileAppender); context.getRootLogger().addAppender(textAreaAppender); context.updateLoggers(); }
Example #5
Source File: ConfigurationAssemblerTest.java From pulsar with Apache License 2.0 | 6 votes |
@Test public void testBuildConfiguration() { try { System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"); final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory .newConfigurationBuilder(); CustomConfigurationFactory.addTestFixtures("config name", builder); final Configuration configuration = builder.build(); try (LoggerContext ctx = Configurator.initialize(configuration)) { validate(configuration); } } finally { System.getProperties().remove(Constants.LOG4J_CONTEXT_SELECTOR); } }
Example #6
Source File: LoggingConfigurationFactory.java From luna with MIT License | 6 votes |
/** * Returns the pattern layout defined by {@link LoggingSettings}. * * @param builder The builder. * @return The pattern layout. */ private LayoutComponentBuilder getPatternLayout(ConfigurationBuilder<BuiltConfiguration> builder) { var pattern = ""; var formatType = Luna.loggingSettings().formatType(); switch (formatType) { case BASIC: pattern = "[%d{dd MMM yyyy, h:mm:ss a}] [%level] %msg%n"; break; case VERBOSE: pattern = "%d{[dd MMM yyyy HH:mm:ss]} [%logger{36}] [%t]%n%level: %msg%n"; break; } if (pattern.isEmpty()) { throw new IllegalStateException("No pattern layout for " + formatType + "!"); } return builder.newLayout("PatternLayout").addAttribute("pattern", pattern); }
Example #7
Source File: SimpleConfiguratorIntegrationTest.java From tutorials with MIT License | 6 votes |
@Test public void givenDefaultLog4j2Environment_whenProgrammaticallyConfigured_thenLogsCorrectly() { ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); AppenderComponentBuilder console = builder.newAppender("Stdout", "CONSOLE") .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); console.add(builder.newLayout("PatternLayout") .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); builder.add(console); builder.add(builder.newLogger("com", Level.DEBUG) .add(builder.newAppenderRef("Stdout")) .addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR) .add(builder.newAppenderRef("Stdout"))); Configurator.initialize(builder.build()); LogPrinter logPrinter = new LogPrinter(); logPrinter.printlog(); }
Example #8
Source File: ConfigurationAssemblerTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testBuildConfiguration() throws Exception { try { System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector"); final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory .newConfigurationBuilder(); CustomConfigurationFactory.addTestFixtures("config name", builder); final Configuration configuration = builder.build(); try (LoggerContext ctx = Configurator.initialize(configuration)) { validate(configuration); } } finally { System.getProperties().remove(Constants.LOG4J_CONTEXT_SELECTOR); } }
Example #9
Source File: TestConfigurator.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void testBuilder() throws Exception { final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.setStatusLevel(Level.ERROR); builder.setConfigurationName("BuilderTest"); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); ctx = Configurator.initialize(builder.build()); final Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Unexpected Configuration", "BuilderTest", config.getName()); assertThat(config.getAppenders(), hasSize(equalTo(1))); }
Example #10
Source File: ConfigurationBuilderTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private void addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.setShutdownTimeout(5000, TimeUnit.MILLISECONDS); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newLogger("org.apache.logging.log4j.core"). add(builder.newAppenderRef("Stdout"))); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.addProperty("MyKey", "MyValue"); builder.add(builder.newCustomLevel("Panic", 17)); builder.setPackages("foo,bar"); }
Example #11
Source File: CustomConfigurationFactory.java From logging-log4j2 with Apache License 2.0 | 6 votes |
public static Configuration addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder. newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder. newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL). addAttribute("marker", "FLOW")); builder.add(appenderBuilder); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.add(builder.newCustomLevel("Panic", 17)); return builder.build(); }
Example #12
Source File: ConfigurationBuilderTest.java From pulsar with Apache License 2.0 | 5 votes |
@Test public void testXmlConstructing() { //assumeTrue(System.lineSeparator().length() == 1); // Only run test on platforms with single character line endings (such as Linux), not on Windows final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); addTestFixtures("config name", builder); final String xmlConfiguration = builder.toXmlConfiguration(); assertEquals(expectedXml, xmlConfiguration); }
Example #13
Source File: ConfigurationBuilderTest.java From pulsar with Apache License 2.0 | 5 votes |
private void addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.setShutdownTimeout(5000, TimeUnit.MILLISECONDS); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); final AppenderComponentBuilder appenderBuilder2 = builder.newAppender("Pulsar", "Pulsar") .addAttribute("serviceUrl", "pulsar://localhost:6650") .addAttribute("topic", "my-topic"); appenderBuilder2.add(builder.newLayout("GelfLayout"). addAttribute("host", "my-host"). addComponent(builder.newKeyValuePair("extraField", "extraValue"))); builder.add(appenderBuilder2); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newLogger("org.apache.logging.log4j.core", Level.DEBUG). add(builder.newAppenderRef("Stdout"))); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.addProperty("MyKey", "MyValue"); builder.add(builder.newCustomLevel("Panic", 17)); builder.setPackages("foo,bar"); }
Example #14
Source File: LoggingMixin.java From picocli with Apache License 2.0 | 5 votes |
public static LoggerContext initializeLog4j() { ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.setStatusLevel(Level.ERROR); // show internal log4j2 errors builder.setConfigurationName("QuickAndDirtySetup"); AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE") .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout") .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); builder.add(appenderBuilder); //builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG) // .add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout").addAttribute("level", Level.WARN))); return Configurator.initialize(builder.build()); }
Example #15
Source File: LogUtil.java From fix-orchestra with Apache License 2.0 | 5 votes |
public static Logger initializeFileLogger(String fileName, Level level, Class<?> clazz) { final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); final AppenderComponentBuilder appenderBuilder = builder.newAppender("file", "File").addAttribute("fileName", fileName); builder.add(appenderBuilder); builder.add( builder.newLogger(clazz.getCanonicalName(), level).add(builder.newAppenderRef("file"))); builder.add(builder.newRootLogger(level).add(builder.newAppenderRef("file"))); final LoggerContext ctx = Configurator.initialize(builder.build()); return LogManager.getLogger(clazz); }
Example #16
Source File: LoggingMixin.java From picocli with Apache License 2.0 | 5 votes |
public static LoggerContext initializeLog4j() { ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.setStatusLevel(Level.ERROR); // show internal log4j2 errors builder.setConfigurationName("QuickAndDirtySetup"); AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE") .addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout") .addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); builder.add(appenderBuilder); //builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG) // .add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout").addAttribute("level", Level.WARN))); return Configurator.initialize(builder.build()); }
Example #17
Source File: CustomConfigurationFactory.java From pulsar with Apache License 2.0 | 5 votes |
static Configuration addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); final AppenderComponentBuilder appenderBuilder2 = builder.newAppender("Pulsar", "Pulsar") .addAttribute("serviceUrl", "pulsar://localhost:6650") .addAttribute("topic", "my-topic"); appenderBuilder2.add(builder.newLayout("GelfLayout"). addAttribute("host", "my-host"). addComponent(builder.newKeyValuePair("extraField", "extraValue"))); builder.add(appenderBuilder2); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.add(builder.newCustomLevel("Panic", 17)); return builder.build(); }
Example #18
Source File: ConfigurationBuilderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testXmlConstructing() throws Exception { assumeTrue(System.lineSeparator().length() == 1); // Only run test on platforms with single character line endings (such as Linux), not on Windows final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); addTestFixtures("config name", builder); final String xmlConfiguration = builder.toXmlConfiguration(); assertEquals(expectedXml, xmlConfiguration); }
Example #19
Source File: Log4j2ConfigurationFactory.java From apm-agent-java with Apache License 2.0 | 5 votes |
private LayoutComponentBuilder createLayout(ConfigurationBuilder<BuiltConfiguration> builder, LogFormat logFormat) { if (logFormat == LogFormat.PLAIN_TEXT) { return builder .newLayout("PatternLayout") .addAttribute("pattern", "%d [%thread] %-5level %logger{36} - %msg%n"); } else { String serviceName = getValue(CoreConfiguration.SERVICE_NAME, sources, ServiceNameUtil.getDefaultServiceName()); return builder.newLayout("EcsLayout") .addAttribute("eventDataset", serviceName + ".apm"); } }
Example #20
Source File: Log4j1ConfigurationParser.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Parses a Log4j 1.2 properties configuration file in ISO 8859-1 encoding into a ConfigurationBuilder. * * @param input * InputStream to read from is assumed to be ISO 8859-1, and will not be closed. * @return the populated ConfigurationBuilder, never {@literal null} * @throws IOException * if unable to read the input * @throws ConfigurationException * if the input does not contain a valid configuration */ public ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final InputStream input) throws IOException { try { properties.load(input); strSubstitutorProperties = new StrSubstitutor(properties); strSubstitutorSystem = new StrSubstitutor(System.getProperties()); final String rootCategoryValue = getLog4jValue(ROOTCATEGORY); final String rootLoggerValue = getLog4jValue(ROOTLOGGER); if (rootCategoryValue == null && rootLoggerValue == null) { // This is not a Log4j 1 properties configuration file. warn("Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input); // throw new ConfigurationException( // "Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input); } builder.setConfigurationName("Log4j1"); // DEBUG final String debugValue = getLog4jValue("debug"); if (Boolean.valueOf(debugValue)) { builder.setStatusLevel(Level.DEBUG); } // Root buildRootLogger(getLog4jValue(ROOTCATEGORY)); buildRootLogger(getLog4jValue(ROOTLOGGER)); // Appenders final Map<String, String> appenderNameToClassName = buildClassToPropertyPrefixMap(); for (final Map.Entry<String, String> entry : appenderNameToClassName.entrySet()) { final String appenderName = entry.getKey(); final String appenderClass = entry.getValue(); buildAppender(appenderName, appenderClass); } // Loggers buildLoggers("log4j.category."); buildLoggers("log4j.logger."); buildProperties(); return builder; } catch (final IllegalArgumentException e) { throw new ConfigurationException(e); } }
Example #21
Source File: Log4j1ConfigurationFactory.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) { final ConfigurationBuilder<BuiltConfiguration> builder; try (final InputStream configStream = source.getInputStream()) { builder = new Log4j1ConfigurationParser().buildConfigurationBuilder(configStream); } catch (final IOException e) { throw new ConfigurationException("Unable to load " + source, e); } return builder.build(); }
Example #22
Source File: TestConfigurator.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testBuilderWithScripts() throws Exception { final String script = "if (logEvent.getLoggerName().equals(\"NoLocation\")) {\n" + " return \"NoLocation\";\n" + " } else if (logEvent.getMarker() != null && logEvent.getMarker().isInstanceOf(\"FLOW\")) {\n" + " return \"Flow\";\n" + " } else {\n" + " return null;\n" + " }"; final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); builder.setStatusLevel(Level.ERROR); builder.setConfigurationName("BuilderTest"); builder.add(builder.newScriptFile("filter.groovy", "target/test-classes/scripts/filter.groovy").addIsWatched(true)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addComponent(builder.newComponent("ScriptPatternSelector") .addAttribute("defaultPattern", "[%-5level] %c{1.} %C{1.}.%M.%L %msg%n") .addComponent(builder.newComponent("PatternMatch").addAttribute("key", "NoLocation") .addAttribute("pattern", "[%-5level] %c{1.} %msg%n")) .addComponent(builder.newComponent("PatternMatch").addAttribute("key", "FLOW") .addAttribute("pattern", "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n")) .addComponent(builder.newComponent("selectorScript", "Script", script).addAttribute("language", "beanshell")))); appenderBuilder.add(builder.newFilter("ScriptFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addComponent(builder.newComponent("ScriptRef").addAttribute("ref", "filter.groovy"))); builder.add(appenderBuilder); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); ctx = Configurator.initialize(builder.build()); final Configuration config = ctx.getConfiguration(); assertNotNull("No configuration", config); assertEquals("Unexpected Configuration", "BuilderTest", config.getName()); assertThat(config.getAppenders(), hasSize(equalTo(1))); assertNotNull("Filter script not found", config.getScriptManager().getScript("filter.groovy")); assertNotNull("pattern selector script not found", config.getScriptManager().getScript("selectorScript")); }
Example #23
Source File: Log4j1ConfigurationParser.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Parses a Log4j 1.2 properties configuration file in ISO 8859-1 encoding into a ConfigurationBuilder. * * @param input * InputStream to read from is assumed to be ISO 8859-1, and will not be closed. * @return the populated ConfigurationBuilder, never {@literal null} * @throws IOException * if unable to read the input * @throws ConfigurationException * if the input does not contain a valid configuration */ public ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final InputStream input) throws IOException { try { properties.load(input); strSubstitutorProperties = new StrSubstitutor(properties); strSubstitutorSystem = new StrSubstitutor(System.getProperties()); final String rootCategoryValue = getLog4jValue(ROOTCATEGORY); final String rootLoggerValue = getLog4jValue(ROOTLOGGER); if (rootCategoryValue == null && rootLoggerValue == null) { // This is not a Log4j 1 properties configuration file. warn("Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input); // throw new ConfigurationException( // "Missing " + ROOTCATEGORY + " or " + ROOTLOGGER + " in " + input); } builder.setConfigurationName("Log4j1"); // DEBUG final String debugValue = getLog4jValue("debug"); if (Boolean.valueOf(debugValue)) { builder.setStatusLevel(Level.DEBUG); } // Root buildRootLogger(getLog4jValue(ROOTCATEGORY)); buildRootLogger(getLog4jValue(ROOTLOGGER)); // Appenders final Map<String, String> appenderNameToClassName = buildClassToPropertyPrefixMap(); for (final Map.Entry<String, String> entry : appenderNameToClassName.entrySet()) { final String appenderName = entry.getKey(); final String appenderClass = entry.getValue(); buildAppender(appenderName, appenderClass); } // Loggers buildLoggers("log4j.category."); buildLoggers("log4j.logger."); buildProperties(); return builder; } catch (final IllegalArgumentException e) { throw new ConfigurationException(e); } }
Example #24
Source File: Log4j1ConfigurationFactory.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) { final ConfigurationBuilder<BuiltConfiguration> builder; try (final InputStream configStream = source.getInputStream()) { builder = new Log4j1ConfigurationParser().buildConfigurationBuilder(configStream); } catch (final IOException e) { throw new ConfigurationException("Unable to load " + source, e); } return builder.build(); }
Example #25
Source File: ConfigurationBuilderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.setShutdownTimeout(5000, TimeUnit.MILLISECONDS); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); final AppenderComponentBuilder appenderBuilder2 = builder.newAppender("Kafka", "Kafka").addAttribute("topic", "my-topic"); appenderBuilder2.addComponent(builder.newProperty("bootstrap.servers", "localhost:9092")); appenderBuilder2.add(builder.newLayout("GelfLayout"). addAttribute("host", "my-host"). addComponent(builder.newKeyValuePair("extraField", "extraValue"))); builder.add(appenderBuilder2); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newLogger("org.apache.logging.log4j.core"). add(builder.newAppenderRef("Stdout"))); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.addProperty("MyKey", "MyValue"); builder.add(builder.newCustomLevel("Panic", 17)); builder.setPackages("foo,bar"); }
Example #26
Source File: ConfigurationBuilderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testXmlConstructing() throws Exception { //assumeTrue(System.lineSeparator().length() == 1); // Only run test on platforms with single character line endings (such as Linux), not on Windows final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); addTestFixtures("config name", builder); final String xmlConfiguration = builder.toXmlConfiguration(); assertEquals(expectedXml, xmlConfiguration); }
Example #27
Source File: CustomConfigurationFactory.java From logging-log4j2 with Apache License 2.0 | 5 votes |
static Configuration addTestFixtures(final String name, final ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.add(builder.newScriptFile("target/test-classes/scripts/filter.groovy").addIsWatched(true)); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL) .addAttribute("level", Level.DEBUG)); final AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout"). addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); final AppenderComponentBuilder appenderBuilder2 = builder.newAppender("Kafka", "Kafka").addAttribute("topic", "my-topic"); appenderBuilder2.addComponent(builder.newProperty("bootstrap.servers", "localhost:9092")); appenderBuilder2.add(builder.newLayout("GelfLayout"). addAttribute("host", "my-host"). addComponent(builder.newKeyValuePair("extraField", "extraValue"))); builder.add(appenderBuilder2); builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG, true). add(builder.newAppenderRef("Stdout")). addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); builder.add(builder.newCustomLevel("Panic", 17)); return builder.build(); }
Example #28
Source File: AbstractCustomConfigurationFactoryTemplate.java From xian with Apache License 2.0 | 5 votes |
private Configuration createConfiguration(final String name, ConfigurationBuilder<BuiltConfiguration> builder) { builder.setConfigurationName(name); builder.setStatusLevel(Level.ERROR); builder.add(builder.newFilter("ThresholdFilter", Filter.Result.ACCEPT, Filter.Result.NEUTRAL).addAttribute("level", level())); if (isConsoleAppenderEnabled()) { AppenderComponentBuilder appenderBuilder = builder.newAppender("Stdout", "CONSOLE").addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT); appenderBuilder.add(builder.newLayout("PatternLayout").addAttribute("pattern", "%d [%t] %-5level: %msg [%c][%X{msgId}]%n%throwable")); appenderBuilder.add(builder.newFilter("MarkerFilter", Filter.Result.DENY, Filter.Result.NEUTRAL).addAttribute("marker", "FLOW")); builder.add(appenderBuilder); } builder.add(builder.newLogger("org.apache.logging.log4j", Level.DEBUG).add(builder.newAppenderRef("Stdout")).addAttribute("additivity", false)); builder.add(builder.newRootLogger(Level.ERROR).add(builder.newAppenderRef("Stdout"))); return builder.build(); }
Example #29
Source File: DefaultConfigurationBuilder.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public ConfigurationBuilder<T> add(final RootLoggerComponentBuilder builder) { for (final Component c : loggers.getComponents()) { if (c.getPluginType().equals(LoggerConfig.ROOT)) { throw new ConfigurationException("Root Logger was previously defined"); } } return add(loggers, builder); }
Example #30
Source File: Log4j2ConfigurationFactory.java From apm-agent-java with Apache License 2.0 | 5 votes |
public Configuration getConfiguration() { ConfigurationBuilder<BuiltConfiguration> builder = newConfigurationBuilder(); builder.setStatusLevel(Level.ERROR) .setConfigurationName("ElasticAPM"); Level level = Level.valueOf(getValue(LOG_LEVEL_KEY, sources, getValue(DEPRECATED_LOG_LEVEL_KEY, sources, Level.INFO.toString()))); RootLoggerComponentBuilder rootLogger = builder.newRootLogger(level); List<AppenderComponentBuilder> appenders = createAppenders(builder); for (AppenderComponentBuilder appender : appenders) { rootLogger.add(builder.newAppenderRef(appender.getName())); } builder.add(rootLogger); return builder.build(); }