Skip to content

Commit

Permalink
Merge pull request #219 from jamezp/LOGMGR-227
Browse files Browse the repository at this point in the history
Add new ext module and reintroduce a property configurator
  • Loading branch information
jamezp authored Dec 7, 2018
2 parents 8631ca1 + 2bfaa1b commit 871bd38
Show file tree
Hide file tree
Showing 17 changed files with 1,764 additions and 79 deletions.
46 changes: 46 additions & 0 deletions core/src/main/java/org/jboss/logmanager/ConfiguratorFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2018 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.logmanager;

/**
* Used to create a {@link LogContextConfigurator}. The {@linkplain #priority() priority} is used to determine which
* factory should be used. The lowest priority factory is used. If two factories have the same priority the second
* factory will not be used. The order of loading the factories for determining priority is done via the
* {@link java.util.ServiceLoader#load(Class, ClassLoader)}.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public interface ConfiguratorFactory {

/**
* Creates the {@link LogContextConfigurator}.
*
* @return the log context configurator
*/
LogContextConfigurator create();

/**
* The priority for the factory which is used to determine which factory should be used to create a
* {@link LogContextConfigurator}. The lowest priority factory will be used.
*
* @return the priority for this factory
*/
int priority();
}
14 changes: 10 additions & 4 deletions core/src/main/java/org/jboss/logmanager/LogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,23 @@ private void doConfigure(InputStream inputStream) {
synchronized (configuratorRef) {
configurator = configuratorRef.get();
if (configurator == null) {
final ServiceLoader<LogContextConfigurator> serviceLoader = ServiceLoader.load(LogContextConfigurator.class);
final Iterator<LogContextConfigurator> iterator = serviceLoader.iterator();
int best = Integer.MAX_VALUE;
ConfiguratorFactory factory = null;
final ServiceLoader<ConfiguratorFactory> serviceLoader = ServiceLoader.load(ConfiguratorFactory.class);
final Iterator<ConfiguratorFactory> iterator = serviceLoader.iterator();
List<Throwable> problems = null;
for (;;) try {
if (! iterator.hasNext()) break;
configurator = iterator.next();
break;
final ConfiguratorFactory f = iterator.next();
if (f.priority() < best || factory == null) {
best = f.priority();
factory = f;
}
} catch (Throwable t) {
if (problems == null) problems = new ArrayList<>(4);
problems.add(t);
}
configurator = factory == null ? null : factory.create();
if (configurator == null) {
if (problems == null) {
configuratorRef.set(configurator = LogContextConfigurator.EMPTY);
Expand Down
50 changes: 0 additions & 50 deletions core/src/test/java/org/jboss/logmanager/TestFileHandler.java

This file was deleted.

80 changes: 80 additions & 0 deletions ext/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source.
~
~ Copyright 2018 Red Hat, Inc., and individual contributors
~ as indicated by the @author tags.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jboss-logmanager-parent</artifactId>
<groupId>org.jboss.logmanager</groupId>
<version>3.0.0.Final-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jboss-logmanager-ext</artifactId>

<dependencies>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logmanager</groupId>
<artifactId>jboss-logmanager</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.modules</groupId>
<artifactId>jboss-modules</artifactId>
</dependency>
<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<!-- Optional and provided as this is only a compile-time dependency -->
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<config.dir>${project.build.testOutputDirectory}${file.separator}configs</config.dir>
<log.dir>${project.build.directory}${file.separator}logs</log.dir>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2018 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.logmanager.ext;

import org.jboss.logmanager.ConfiguratorFactory;
import org.jboss.logmanager.LogContextConfigurator;
import org.kohsuke.MetaInfServices;

/**
* The default configuration factory which has a priority of 100.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@MetaInfServices
public class DefaultConfiguratorFactory implements ConfiguratorFactory {

@Override
public LogContextConfigurator create() {
return new DefaultLogContextConfigurator();
}

@Override
public int priority() {
return 100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2018 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.logmanager.ext;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.logging.Logger;

import org.jboss.logmanager.Level;
import org.jboss.logmanager.LogContext;
import org.jboss.logmanager.LogContextConfigurator;
import org.jboss.logmanager.StandardOutputStreams;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.jboss.logmanager.handlers.ConsoleHandler;

/**
* A default log context configuration.
* <p>
* If the {@linkplain #configure(LogContext, InputStream) input stream} is {@code null} an attempt is made to find a
* {@code logging.properties} file. If the file is not found, a {@linkplain java.util.ServiceLoader service loader} is
* used to find the first implementation of a {@link LogContextConfigurator}. If that fails a default
* {@link ConsoleHandler} is configured with the pattern {@code %d{yyyy-MM-dd'T'HH:mm:ssXXX} %-5p [%c] (%t) %s%e%n}.
* </p>
*
* <p>
* Locating the {@code logging.properties} happens in the following order:
* <ul>
* <li>The {@code logging.configuration} system property is checked</li>
* <li>The current threads {@linkplain ClassLoader#getResourceAsStream(String)} class loader} for a {@code logging.properties}</li>
* <li>Finally {@link Class#getResourceAsStream(String)} is used to locate a {@code logging.properties}</li>
* </ul>
* </p>
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class DefaultLogContextConfigurator implements LogContextConfigurator {

@Override
public void configure(final LogContext logContext, final InputStream inputStream) {
final InputStream configIn = inputStream != null ? inputStream : findConfiguration();

// Configure the log context based on a property file
if (configIn != null) {
final Properties properties = new Properties();
try (Reader reader = new InputStreamReader(configIn, StandardCharsets.UTF_8)) {
properties.load(reader);
} catch (IOException e) {
throw new RuntimeException("Failed to configure log manager with configuration file.", e);
}
PropertyConfigurator.configure(logContext, properties);
} else {
// Next check the service loader
final Iterator<LogContextConfigurator> serviceLoader = ServiceLoader.load(LogContextConfigurator.class).iterator();
if (serviceLoader.hasNext()) {
serviceLoader.next().configure(logContext, inputStream);
} else {
// Configure a default console handler, pattern formatter and associated with the root logger
final ConsoleHandler handler = new ConsoleHandler(new PatternFormatter("%d{yyyy-MM-dd'T'HH:mm:ssXXX} %-5p [%c] (%t) %s%e%n"));
handler.setLevel(Level.INFO);
handler.setAutoFlush(true);
final Logger rootLogger = logContext.getLogger("");
rootLogger.setLevel(Level.INFO);
rootLogger.addHandler(handler);
}
}
}

private static InputStream findConfiguration() {
final String propLoc = System.getProperty("logging.configuration");
if (propLoc != null) try {
return new URL(propLoc).openStream();
} catch (IOException e) {
StandardOutputStreams.printError("Unable to read the logging configuration from '%s' (%s)%n", propLoc, e);
}
final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
if (tccl != null) try {
final InputStream stream = tccl.getResourceAsStream("logging.properties");
if (stream != null) return stream;
} catch (Exception ignore) {
}
return DefaultLogContextConfigurator.class.getResourceAsStream("logging.properties");
}
}
Loading

0 comments on commit 871bd38

Please sign in to comment.