-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from jamezp/LOGMGR-227
Add new ext module and reintroduce a property configurator
- Loading branch information
Showing
17 changed files
with
1,764 additions
and
79 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
core/src/main/java/org/jboss/logmanager/ConfiguratorFactory.java
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 |
---|---|---|
@@ -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(); | ||
} |
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
50 changes: 0 additions & 50 deletions
50
core/src/test/java/org/jboss/logmanager/TestFileHandler.java
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -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> |
43 changes: 43 additions & 0 deletions
43
ext/src/main/java/org/jboss/logmanager/ext/DefaultConfiguratorFactory.java
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
ext/src/main/java/org/jboss/logmanager/ext/DefaultLogContextConfigurator.java
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 |
---|---|---|
@@ -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"); | ||
} | ||
} |
Oops, something went wrong.