Skip to content

Commit

Permalink
Merge pull request #211 from jamezp/LOGMGR-213
Browse files Browse the repository at this point in the history
[LOGMGR-213] Add a System.LoggerFinder which will set the java.util.l…
  • Loading branch information
jamezp authored Dec 10, 2018
2 parents 39083b7 + 4e2c57d commit 443244d
Show file tree
Hide file tree
Showing 9 changed files with 607 additions and 0 deletions.
54 changes: 54 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<scope>provided</scope>
<optional>true</optional>
</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>
Expand Down Expand Up @@ -175,4 +182,51 @@
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>java9-tests</id>
<activation>
<jdk>[9,</jdk>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test-compile-java9</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<release>9</release>
<buildDirectory>${project.build.directory}</buildDirectory>
<compileSourceRoots>${project.basedir}/src/test/java9</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
29 changes: 29 additions & 0 deletions core/src/main/java/org/jboss/logmanager/JBossLoggerFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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;

/**
* For Java 8 this is just an empty type. For Java 9 or greater this implements the {@code System.LoggerFinder}. It
* will make an attempt to set the {@code java.util.logging.manager} system property before a logger is accessed.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class JBossLoggerFinder {
}
111 changes: 111 additions & 0 deletions core/src/main/java9/org/jboss/logmanager/JBossLoggerFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.EnumMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class JBossLoggerFinder extends System.LoggerFinder {
private static final Map<System.Logger.Level, java.util.logging.Level> LEVELS = new EnumMap<>(System.Logger.Level.class);
private static final AtomicBoolean LOGGED = new AtomicBoolean(false);
private static volatile boolean PROPERTY_SET = false;

static {
LEVELS.put(System.Logger.Level.ALL, Level.ALL);
LEVELS.put(System.Logger.Level.TRACE, Level.TRACE);
LEVELS.put(System.Logger.Level.DEBUG, Level.DEBUG);
LEVELS.put(System.Logger.Level.INFO, Level.INFO);
LEVELS.put(System.Logger.Level.WARNING, Level.WARN);
LEVELS.put(System.Logger.Level.ERROR, Level.ERROR);
LEVELS.put(System.Logger.Level.OFF, Level.OFF);
}

@Override
public System.Logger getLogger(final String name, final Module module) {
if (!PROPERTY_SET) {
synchronized (this) {
if (!PROPERTY_SET) {
if (System.getSecurityManager() == null) {
if (System.getProperty("java.util.logging.manager") == null) {
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
}
} else {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
if (System.getProperty("java.util.logging.manager") == null) {
System.setProperty("java.util.logging.manager", "org.jboss.logmanager.LogManager");
}
return null;
});
}
}
PROPERTY_SET = true;
}
}
final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(name);
if (!(logger instanceof org.jboss.logmanager.Logger)) {
if (LOGGED.compareAndSet(false, true)) {
logger.log(Level.ERROR, "The LogManager accessed before the \"java.util.logging.manager\" system property was set to \"org.jboss.logmanager.LogManager\". Results may be unexpected.");
}
}
return new JBossSystemLogger(logger);
}

private static class JBossSystemLogger implements System.Logger {
private static final String LOGGER_CLASS_NAME = JBossSystemLogger.class.getName();
private final java.util.logging.Logger delegate;

private JBossSystemLogger(final java.util.logging.Logger delegate) {
this.delegate = delegate;
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public boolean isLoggable(final Level level) {
return delegate.isLoggable(LEVELS.getOrDefault(level, java.util.logging.Level.INFO));
}

@Override
public void log(final Level level, final ResourceBundle bundle, final String msg, final Throwable thrown) {
final ExtLogRecord record = new ExtLogRecord(LEVELS.getOrDefault(level, java.util.logging.Level.INFO), msg, LOGGER_CLASS_NAME);
record.setThrown(thrown);
record.setResourceBundle(bundle);
delegate.log(record);
}

@Override
public void log(final Level level, final ResourceBundle bundle, final String format, final Object... params) {
final ExtLogRecord record = new ExtLogRecord(LEVELS.getOrDefault(level, java.util.logging.Level.INFO), format, ExtLogRecord.FormatStyle.MESSAGE_FORMAT, LOGGER_CLASS_NAME);
record.setParameters(params);
record.setResourceBundle(bundle);
delegate.log(record);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# 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.
#

org.jboss.logmanager.JBossLoggerFinder
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

import org.kohsuke.MetaInfServices;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@MetaInfServices
public class TestConfiguratorFactory implements ConfiguratorFactory {
@Override
public LogContextConfigurator create() {
return new TestLogContextConfigurator(false);
}

@Override
public int priority() {
return 50;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.logmanager.formatters.JsonFormatter;
import org.jboss.logmanager.handlers.FileHandler;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class TestLogContextConfigurator implements LogContextConfigurator {

public TestLogContextConfigurator() {
this(true);
}

TestLogContextConfigurator(final boolean assumedJul) {
if (assumedJul) {
configure(null);
}
}

@Override
public void configure(final LogContext logContext, final InputStream inputStream) {
configure(logContext);
}

private static void configure(final LogContext logContext) {
if (Boolean.getBoolean("org.jboss.logmanager.test.configure")) {
final Logger rootLogger;
if (logContext == null) {
rootLogger = Logger.getLogger("");
} else {
rootLogger = logContext.getLogger("");
}
try {
final String fileName = System.getProperty("test.log.file.name");
final FileHandler handler = new FileHandler(fileName, false);
handler.setAutoFlush(true);
handler.setFormatter(new JsonFormatter());
rootLogger.addHandler(handler);
rootLogger.setLevel(Level.INFO);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
}
Loading

0 comments on commit 443244d

Please sign in to comment.