/** * Creates an instance of {@link BatchEmitter} using one of available {@link BatchEmitterFactory} services. A check * for compatibility of given {@link ClientObjectFactory} with available services is performed. * <p> * NOTE: Currently the first found and compatible {@link BatchEmitterFactory} is selected as the {@link * BatchEmitter} provider. This is subject to change after new config features are added in future releases ( * priority-based selection will be available to provide more flexible extension capabilities). * * @param batchSize number of elements in a current batch that should trigger a delivery, regardless of * the delivery interval value * @param deliveryInterval number of millis between two time-triggered deliveries, regardless of the batch size * value * @param clientObjectFactory client-specific objects provider * @param failoverPolicy sink for failed batch items * @return T configured {@link BatchEmitter} */ public BatchEmitter createInstance(int batchSize, int deliveryInterval, ClientObjectFactory clientObjectFactory, FailoverPolicy failoverPolicy) { ServiceLoader<BatchEmitterFactory> loader = ServiceLoader.load(BatchEmitterFactory.class); Iterator<BatchEmitterFactory> it = loader.iterator(); while (it.hasNext()) { BatchEmitterFactory factory = it.next(); LOG.info("BatchEmitterFactory class found {}", factory); if (factory.accepts(clientObjectFactory.getClass())) { LOG.info("Using {} as BatchEmitterFactoryProvider", factory); return factory.createInstance(batchSize, deliveryInterval, clientObjectFactory, failoverPolicy); } } throw new ConfigurationException(String.format("No compatible BatchEmitter implementations for %s found", clientObjectFactory.getClass())); }
private String loadClasspathResource() { try { BufferedReader br = new BufferedReader(new InputStreamReader( ClassLoader.getSystemClassLoader().getResourceAsStream( path.replace(CLASSPATH_PREFIX, "")), "UTF-8")); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append('\n'); } return sb.toString(); } catch (Exception e) { throw new ConfigurationException(e.getMessage(), e); } }
@Test(expected = ConfigurationException.class) public void throwsExceptionOnUnresolvedAppender() { // given Appender appender = mock(Appender.class); when(appender.isStarted()).thenReturn(true); Configuration configuration = mock(Configuration.class); String testAppenderRef = "testAppenderRef"; when(configuration.getAppender(testAppenderRef)).thenReturn(null); FailoverPolicy<String> failoverPolicy = createTestFailoverPolicy(testAppenderRef, configuration); String failedMessage = "test failed message"; // when failoverPolicy.deliver(failedMessage); }
private AppenderComponentBuilder createAppender(final String key, final Properties properties) { final String name = (String) properties.remove(CONFIG_NAME); if (Strings.isEmpty(name)) { throw new ConfigurationException("No name attribute provided for Appender " + key); } final String type = (String) properties.remove(CONFIG_TYPE); if (Strings.isEmpty(type)) { throw new ConfigurationException("No type attribute provided for Appender " + key); } final AppenderComponentBuilder appenderBuilder = builder.newAppender(name, type); addFiltersToComponent(appenderBuilder, properties); final Properties layoutProps = PropertiesUtil.extractSubset(properties, "layout"); if (layoutProps.size() > 0) { appenderBuilder.add(createLayout(name, layoutProps)); } return processRemainingProperties(appenderBuilder, properties); }
public HttpURLConnectionManager(final Configuration configuration, final LoggerContext loggerContext, final String name, final URL url, final String method, final int connectTimeoutMillis, final int readTimeoutMillis, final Property[] headers, final SslConfiguration sslConfiguration, final boolean verifyHostname) { super(configuration, loggerContext, name); this.url = url; if (!(url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https"))) { throw new ConfigurationException("URL must have scheme http or https"); } this.isHttps = this.url.getProtocol().equalsIgnoreCase("https"); this.method = Objects.requireNonNull(method, "method"); this.connectTimeoutMillis = connectTimeoutMillis; this.readTimeoutMillis = readTimeoutMillis; this.headers = headers != null ? headers : new Property[0]; this.sslConfiguration = sslConfiguration; if (this.sslConfiguration != null && !isHttps) { throw new ConfigurationException("SSL configuration can only be specified with URL scheme https"); } this.verifyHostname = verifyHostname; }
@Override public BulkProcessorObjectFactory build() { if (serverUris == null) { throw new ConfigurationException("No serverUris provided for JestClientConfig"); } return new BulkProcessorObjectFactory(Arrays.asList(serverUris.split(";"))); }
@Test(expected = ConfigurationException.class) public void builderFailsIfServerUrisStringIsNull() { // given Builder builder = createTestObjectFactoryBuilder(); String serverUris = null; // when builder.withServerUris(serverUris); builder.build(); }
@Override public JestHttpObjectFactory build() { if (serverUris == null) { throw new ConfigurationException("No serverUris provided for JestClientConfig"); } return new JestHttpObjectFactory(Arrays.asList(serverUris.split(";")), connTimeout, readTimeout, maxTotalConnection, defaultMaxTotalConnectionPerRoute, discoveryEnabled); }
/** * Transforms given items to client-specific model and adds them to provided {@link BatchEmitter} * * @param log batch item source * * @deprecated will use configured indexName for backwards compatibility */ @Override public void add(String log) { if (indexName == null) { throw new ConfigurationException("No indexName provided for AsyncBatchDelivery"); } add(this.indexName, log); }
@Override public AsyncBatchDelivery build() { if (indexName != null) { LOG.warn("AsyncBatchDelivery.indexName attribute has been deprecated and will be removed in 1.3. " + "It will NOT be used in AsyncBatchDelivery.add(String indexName, T logObject) calls. " + "Please use IndexName instead."); } if (clientObjectFactory == null) { throw new ConfigurationException("No Elasticsearch client factory [JestHttp|ElasticsearchBulkProcessor] provided for AsyncBatchDelivery"); } return new AsyncBatchDelivery(indexName, batchSize, deliveryInterval, clientObjectFactory, failoverPolicy, indexTemplate); }
@Override public ElasticsearchAppender build() { if (name == null) { throw new ConfigurationException("No name provided for Elasticsearch appender"); } if (batchDelivery == null) { throw new ConfigurationException("No batchDelivery [AsyncBatchDelivery] provided for Elasticsearch appender"); } if (layout == null) { layout = JsonLayout.newBuilder().setCompact(true).build(); } return new ElasticsearchAppender(name, filter, layout, ignoreExceptions, batchDelivery, messageOnly, indexNameFormatter); }
@Override public RollingIndexNameFormatter build() { if (indexName == null) { throw new ConfigurationException("No indexName provided for RollingIndexName"); } if (pattern == null) { throw new ConfigurationException("No pattern provided for RollingIndexName"); } return new RollingIndexNameFormatter(indexName, pattern, getInitTimeInMillis(), TimeZone.getTimeZone(timeZone)); }
@Override public IndexTemplate build() { if (name == null) { throw new ConfigurationException("No name provided for IndexTemplate"); } if ((path == null && source == null) || (path != null && source != null)) { throw new ConfigurationException("Either path or source have to be provided for IndexTemplate"); } return new IndexTemplate(name, loadSource()); }
private String loadFileSystemResource() { try { return new String(Files.readAllBytes(Paths.get(path))); } catch (IOException e){ throw new ConfigurationException(e.getMessage(), e); } }
@Override public NoopIndexNameFormatter build() { if (indexName == null) { throw new ConfigurationException("No indexName provided for IndexName"); } return new NoopIndexNameFormatter(indexName); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionWhenNameIsNotSet() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withName(null); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionWhenNeitherPathOrSourceIsSet() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withPath(null) .withSource(null); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionWhenBothPathAndSourceAreSet() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withPath(TEST_PATH) .withSource(TEST_SOURCE); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionWhenClasspathResourceDoesntExist() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withPath("classpath:nonExistentFile"); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionWhenFileDoesntExist() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withPath("nonExistentFile"); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptionOnInvalidProtocol() { // given IndexTemplate.Builder builder = createTestIndexTemplateBuilder(); builder.withPath("~/nonExistentFile"); // when builder.build(); }
@Override public TestHttpObjectFactory build() { if (serverUris == null) { throw new ConfigurationException("No serverUris provided for JestClientConfig"); } return new TestHttpObjectFactory(Arrays.asList(serverUris.split(";")), connTimeout, readTimeout, maxTotalConnection, defaultMaxTotalConnectionPerRoute, discoveryEnabled); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptioWhenIndexNameIsNull() { // when RollingIndexNameFormatter.Builder builder = createRollingIndexNameFormatterBuilder(); builder.withIndexName(null); // then builder.build(); }
@Test(expected = ConfigurationException.class) public void builderThrowsExceptioWhenPatternIsNull() { // when RollingIndexNameFormatter.Builder builder = createRollingIndexNameFormatterBuilder(); builder.withPattern(null); // then builder.build(); }
@Test(expected = ConfigurationException.class) public void builderFailsWhenClientObjectFactoryIsNull() { // given Builder batchDeliveryBuilder = createTestBatchDeliveryBuilder(); batchDeliveryBuilder.withClientObjectFactory(null); // when batchDeliveryBuilder.build(); }
@Test(expected = ConfigurationException.class) public void deprecatedDeliveryFailsWhenIndexNameIsNull() { // given Builder batchDeliveryBuilder = createTestBatchDeliveryBuilder(); batchDeliveryBuilder.withIndexName(null); BatchDelivery<String> delivery = batchDeliveryBuilder.build(); // when delivery.add("testLog"); }
@Test(expected = ConfigurationException.class) public void builderFailsWhenAppenderNameIsNull() { // given ElasticsearchAppender.Builder builder = createTestElasticsearchAppenderBuilder(); builder.withName(null); // when builder.build(); }
@Test(expected = ConfigurationException.class) public void builderFailsWhenBatchDeliveryIsNull() { // given ElasticsearchAppender.Builder builder = createTestElasticsearchAppenderBuilder(); builder.withBatchDelivery(null); // when builder.build(); }
@Override public void start() { final Map<String, Appender> map = config.getAppenders(); final List<AppenderControl> appenders = new ArrayList<AppenderControl>(); for (final AppenderRef appenderRef : appenderRefs) { if (map.containsKey(appenderRef.getRef())) { appenders.add(new AppenderControl(map.get(appenderRef.getRef()), appenderRef.getLevel(), appenderRef.getFilter())); } else { LOGGER.error("No appender named {} was configured", appenderRef); } } if (errorRef != null) { if (map.containsKey(errorRef)) { errorAppender = new AppenderControl(map.get(errorRef), null, null); } else { LOGGER.error("Unable to set up error Appender. No appender named {} was configured", errorRef); } } if (appenders.size() > 0) { thread = new AsyncThread(appenders, queue); thread.setName("AsyncAppender-" + getName()); } else if (errorRef == null) { throw new ConfigurationException("No appenders are available for AsyncAppender " + getName()); } thread.start(); super.start(); }
/** * 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); } }
@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(); }
private FilterComponentBuilder createFilter(final String key, final Properties properties) { final String type = (String) properties.remove(CONFIG_TYPE); if (Strings.isEmpty(type)) { throw new ConfigurationException("No type attribute provided for Appender " + key); } final String onMatch = (String) properties.remove(AbstractFilterBuilder.ATTR_ON_MATCH); final String onMismatch = (String) properties.remove(AbstractFilterBuilder.ATTR_ON_MISMATCH); final FilterComponentBuilder filterBuilder = builder.newFilter(type, onMatch, onMismatch); return processRemainingProperties(filterBuilder, properties); }
private AppenderRefComponentBuilder createAppenderRef(final String key, final Properties properties) { final String ref = (String) properties.remove("ref"); if (Strings.isEmpty(ref)) { throw new ConfigurationException("No ref attribute provided for AppenderRef " + key); } final AppenderRefComponentBuilder appenderRefBuilder = builder.newAppenderRef(ref); final String level = Strings.trimToNull((String) properties.remove("level")); if (!Strings.isEmpty(level)) { appenderRefBuilder.addAttribute("level", level); } return addFiltersToComponent(appenderRefBuilder, properties); }
private LoggerComponentBuilder createLogger(final String key, final Properties properties) { final String name = (String) properties.remove(CONFIG_NAME); final String location = (String) properties.remove("includeLocation"); if (Strings.isEmpty(name)) { throw new ConfigurationException("No name attribute provided for Logger " + key); } final String level = Strings.trimToNull((String) properties.remove("level")); final String type = (String) properties.remove(CONFIG_TYPE); final LoggerComponentBuilder loggerBuilder; boolean includeLocation; if (type != null) { if (type.equalsIgnoreCase("asyncLogger")) { if (location != null) { includeLocation = Boolean.parseBoolean(location); loggerBuilder = builder.newAsyncLogger(name, level, includeLocation); } else { loggerBuilder = builder.newAsyncLogger(name, level); } } else { throw new ConfigurationException("Unknown Logger type " + type + " for Logger " + name); } } else { if (location != null) { includeLocation = Boolean.parseBoolean(location); loggerBuilder = builder.newLogger(name, level, includeLocation); } else { loggerBuilder = builder.newLogger(name, level); } } addLoggersToComponent(loggerBuilder, properties); addFiltersToComponent(loggerBuilder, properties); final String additivity = (String) properties.remove("additivity"); if (!Strings.isEmpty(additivity)) { loggerBuilder.addAttribute("additivity", additivity); } return loggerBuilder; }
private RootLoggerComponentBuilder createRootLogger(final Properties properties) { final String level = Strings.trimToNull((String) properties.remove("level")); final String type = (String) properties.remove(CONFIG_TYPE); final String location = (String) properties.remove("includeLocation"); final boolean includeLocation; final RootLoggerComponentBuilder loggerBuilder; if (type != null) { if (type.equalsIgnoreCase("asyncRoot")) { if (location != null) { includeLocation = Boolean.parseBoolean(location); loggerBuilder = builder.newAsyncRootLogger(level, includeLocation); } else { loggerBuilder = builder.newAsyncRootLogger(level); } } else { throw new ConfigurationException("Unknown Logger type for root logger" + type); } } else { if (location != null) { includeLocation = Boolean.parseBoolean(location); loggerBuilder = builder.newRootLogger(level, includeLocation); } else { loggerBuilder = builder.newRootLogger(level); } } addLoggersToComponent(loggerBuilder, properties); return addFiltersToComponent(loggerBuilder, properties); }
private LayoutComponentBuilder createLayout(final String appenderName, final Properties properties) { final String type = (String) properties.remove(CONFIG_TYPE); if (Strings.isEmpty(type)) { throw new ConfigurationException("No type attribute provided for Layout on Appender " + appenderName); } final LayoutComponentBuilder layoutBuilder = builder.newLayout(type); return processRemainingProperties(layoutBuilder, properties); }
private static <B extends ComponentBuilder<B>> ComponentBuilder<B> createComponent(final ComponentBuilder<?> parent, final String key, final Properties properties) { final String name = (String) properties.remove(CONFIG_NAME); final String type = (String) properties.remove(CONFIG_TYPE); if (Strings.isEmpty(type)) { throw new ConfigurationException("No type attribute provided for component " + key); } final ComponentBuilder<B> componentBuilder = parent.getBuilder().newComponent(name, type); return processRemainingProperties(componentBuilder, properties); }