Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GJULE - Easier default configuration #25173

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nucleus/glassfish-jul-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<properties>
<test.logManager>org.glassfish.main.jul.GlassFishLogManager</test.logManager>
<test.enableDefaultLogCfg>false</test.enableDefaultLogCfg>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@
import org.glassfish.main.jul.cfg.ConfigurationHelper;
import org.glassfish.main.jul.cfg.GlassFishLogManagerConfiguration;
import org.glassfish.main.jul.cfg.GlassFishLogManagerProperty;
import org.glassfish.main.jul.cfg.GlassFishLoggingConstants;
import org.glassfish.main.jul.cfg.LoggingProperties;
import org.glassfish.main.jul.env.LoggingSystemEnvironment;
import org.glassfish.main.jul.handler.BlockingExternallyManagedLogHandler;
import org.glassfish.main.jul.handler.ExternallyManagedLogHandler;
import org.glassfish.main.jul.handler.SimpleLogHandler;
import org.glassfish.main.jul.handler.SimpleLogHandler.SimpleLogHandlerProperty;
Expand Down Expand Up @@ -88,8 +90,10 @@
* @author David Matejcek
*/
public class GlassFishLogManager extends LogManager {

/** Empty string - standard root logger name */
public static final String ROOT_LOGGER_NAME = "";
private static final String LOGGING_PROPERTIES = "logging.properties";

private static final ReentrantLock LOCK = new ReentrantLock();

Expand All @@ -104,6 +108,10 @@ public class GlassFishLogManager extends LogManager {
private GlassFishLogManagerConfiguration configuration;


/**
* @deprecated Too late - if user enables ie GC logging, it initiates the JDK LogManager before this.
*/
@Deprecated
static boolean initialize(final Properties configuration) {
trace(GlassFishLogManager.class, "initialize(configuration)");
if (status.ordinal() > GlassFishLoggingStatus.UNINITIALIZED.ordinal()) {
Expand Down Expand Up @@ -697,21 +705,29 @@ private static LoggingProperties ensureSortedProperties(final Properties propert

private static LoggingProperties provideProperties() {
try {
final LoggingProperties propertiesFromJvmOption = toProperties(System.getProperty(JVM_OPT_LOGGING_CFG_FILE));
if (Boolean.getBoolean(GlassFishLoggingConstants.JVM_OPT_LOGGING_CFG_BLOCK)) {
final LoggingProperties cfg = new LoggingProperties();
cfg.setProperty(KEY_ROOT_HANDLERS.getPropertyName(),
BlockingExternallyManagedLogHandler.class.getName());
return cfg;
}
final String file = System.getProperty(JVM_OPT_LOGGING_CFG_FILE);
final LoggingProperties propertiesFromJvmOption = toProperties(file);
if (propertiesFromJvmOption != null) {
trace(GlassFishLogManager.class, () -> "Using file " + file);
return propertiesFromJvmOption;
}
if (Boolean.getBoolean(JVM_OPT_LOGGING_CFG_USE_DEFAULTS)) {
return createDefaultProperties();
}
final LoggingProperties propertiesFromClasspath = loadFromClasspath();
if (propertiesFromClasspath != null) {
return propertiesFromClasspath;
}
if (Boolean.getBoolean(JVM_OPT_LOGGING_CFG_USE_DEFAULTS)) {
return createDefaultProperties();
}
throw new IllegalStateException(
"Could not find any logging.properties configuration file neither from JVM option ("
+ JVM_OPT_LOGGING_CFG_FILE + ") nor from classpath and even " + JVM_OPT_LOGGING_CFG_USE_DEFAULTS
+ " wasn't set to true.");
"Could not find any configuration. The JVM option "
+ JVM_OPT_LOGGING_CFG_FILE + " was not set, the JVM option " + JVM_OPT_LOGGING_CFG_USE_DEFAULTS
+ " was disabled and no " + LOGGING_PROPERTIES + " were found on classpath.");
} catch (final IOException e) {
throw new IllegalStateException("Could not load logging configuration file.", e);
}
Expand All @@ -725,6 +741,7 @@ private static LoggingProperties createDefaultProperties() {
cfg.setProperty(KEY_USR_ROOT_LOGGER_LEVEL.getPropertyName(), level);
cfg.setProperty(KEY_ROOT_HANDLERS.getPropertyName(), SimpleLogHandler.class.getName());
cfg.setProperty(SimpleLogHandlerProperty.LEVEL.getPropertyFullName(), level);
trace(GlassFishLogManager.class, () -> "Using internal defaults: " + cfg);
return cfg;
}

Expand All @@ -744,10 +761,12 @@ private static LoggingProperties toProperties(final String absolutePath) throws
private static LoggingProperties loadFromClasspath() throws IOException {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
trace(GlassFishLogManager.class, () -> "loadFromClasspath(); classloader: " + classLoader);
try (InputStream input = classLoader.getResourceAsStream("logging.properties")) {
try (InputStream input = classLoader.getResourceAsStream(LOGGING_PROPERTIES)) {
if (input == null) {
return null;
}
trace(GlassFishLogManager.class,
() -> "Using file from classpath: " + classLoader.getResource(LOGGING_PROPERTIES));
return LoggingProperties.loadFrom(input);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.logging.LogManager;
import java.util.logging.Logger;

import org.glassfish.main.jul.cfg.GlassFishLoggingConstants;

import static org.glassfish.main.jul.cfg.GlassFishLoggingConstants.CLASS_LOG_MANAGER_GLASSFISH;
import static org.glassfish.main.jul.cfg.GlassFishLoggingConstants.JVM_OPT_LOGGING_MANAGER;
import static org.glassfish.main.jul.tracing.GlassFishLoggingTracer.stacktrace;
Expand All @@ -33,17 +35,22 @@
* in the JVM starts the initialization.
* <p>
* Simply said - this must be the first thing application must execute.
* <p>
* As an example, when you enable GC logging, it will be always faster than this class.
* That is why is this class deprecated and we recommend to use the
* {@link GlassFishLoggingConstants#JVM_OPT_LOGGING_MANAGER} and other related options
* which guarantee that the log manager will be set.
*
* @author David Matejcek
*/
@Deprecated
public final class GlassFishLogManagerInitializer {

private GlassFishLogManagerInitializer() {
// hidden
}



/**
* Tries to set the {@link GlassFishLogManager}as the JVM's {@link LogManager} implementation.
* This must be done before any JUL component is used and remains set until JVM shutdown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ public class GlassFishLoggingConstants {
* existence.
*/
public static final String JVM_OPT_LOGGING_MANAGER = "java.util.logging.manager";

/**
* System property name to ask the starting log manager to block until application finishes
* the configuration.
*/
public static final String JVM_OPT_LOGGING_CFG_BLOCK = "java.util.logging.config.block";

/**
* System property name defining property file which will be automatically loaded on startup.
* Usually it is named <code>logging.properties</code>
*/
public static final String JVM_OPT_LOGGING_CFG_FILE = "java.util.logging.config.file";
/**
* System property telling the GlassFishLogManager to use defaults if there would not be any
* logging.properties neither set by {@value #JVM_OPT_LOGGING_CFG_FILE} nor available on classpath.
* logging.properties set by {@value #JVM_OPT_LOGGING_CFG_FILE}.
* <p>
* Defaults use the SimpleLogHandler and level INFO or level set by
* {@value #JVM_OPT_LOGGING_CFG_DEFAULT_LEVEL}
Expand All @@ -76,6 +83,9 @@ public class GlassFishLoggingConstants {
* <p>
* If the property is not set, GJULE makes the decision based on the (<code>*.printSource</code>
* property) - if any formatter requires this feature, the feature is enabled.
* This applies just when the formatter is set from the supplied log manager configuration,
* not when you configure formatter from your code.
*
* <p>
* It is disabled otherwise.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -19,7 +19,9 @@
import java.util.Arrays;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import org.glassfish.main.jul.cfg.GlassFishLoggingConstants;
import org.glassfish.main.jul.cfg.LogProperty;
import org.glassfish.main.jul.record.GlassFishLogRecord;

Expand All @@ -31,6 +33,12 @@
/**
* Fast {@link Formatter} usable in tests or even in production if you need only simple logs with
* time, level and messages.
* <p>
* Note that if you configured the formatter from your code (and not a property file),
* if you want to source class and method automatically detected, you have to enable
* {@link GlassFishLoggingConstants#KEY_CLASS_AND_METHOD_DETECTION_ENABLED}. Without that the output
* will contain just class and method fields manually set to the LogRecord or using {@link Logger}
* methods which have these parameters to do that.
*
* @author David Matejcek
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -98,7 +98,6 @@ public static void resetConfiguration() throws Exception {
* {@link BlockingExternallyManagedLogHandler} to block logging system in
* {@link GlassFishLoggingStatus#CONFIGURING} state, so it is just collecting
* log records to the StartupQueue.
* <p>
*/
@Test
@Order(1)
Expand Down
5 changes: 3 additions & 2 deletions nucleus/parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<!-- GlassFishLogManager is a child of this module, so we cannot use it here yet. -->
<test.logManager>java.util.logging.LogManager</test.logManager>
<test.logLevel>INFO</test.logLevel>
<test.enableDefaultLogCfg>true</test.enableDefaultLogCfg>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -883,7 +884,7 @@
</excludes>
<systemPropertyVariables>
<java.util.logging.manager>${test.logManager}</java.util.logging.manager>
<java.util.logging.config.useDefaults>true</java.util.logging.config.useDefaults>
<java.util.logging.config.useDefaults>${test.enableDefaultLogCfg}</java.util.logging.config.useDefaults>
<java.util.logging.config.defaultLevel>${test.logLevel}</java.util.logging.config.defaultLevel>
<glassfish.suspend>${glassfish.suspend}</glassfish.suspend>
</systemPropertyVariables>
Expand All @@ -906,7 +907,7 @@
</includes>
<systemPropertyVariables>
<java.util.logging.manager>${test.logManager}</java.util.logging.manager>
<java.util.logging.config.useDefaults>true</java.util.logging.config.useDefaults>
<java.util.logging.config.useDefaults>${test.enableDefaultLogCfg}</java.util.logging.config.useDefaults>
<java.util.logging.config.defaultLevel>${test.logLevel}</java.util.logging.config.defaultLevel>
</systemPropertyVariables>
</configuration>
Expand Down