-
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 #211 from jamezp/LOGMGR-213
[LOGMGR-213] Add a System.LoggerFinder which will set the java.util.l…
- Loading branch information
Showing
9 changed files
with
607 additions
and
0 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
core/src/main/java/org/jboss/logmanager/JBossLoggerFinder.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,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
111
core/src/main/java9/org/jboss/logmanager/JBossLoggerFinder.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,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); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
core/src/main/resources/META-INF/services/java.lang.System$LoggerFinder
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,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 |
38 changes: 38 additions & 0 deletions
38
core/src/test/java/org/jboss/logmanager/TestConfiguratorFactory.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,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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
core/src/test/java/org/jboss/logmanager/TestLogContextConfigurator.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,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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.