From 73443a6193393165f5c59c298be20b288bf6b8c9 Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Mon, 1 May 2023 15:12:33 -0300 Subject: [PATCH 1/4] Set debug log level when debug option for debugLog is true Eclipse uses bundle tracing in the launch dialog to configure OSGi DebugOptions. This enhancements adds support for configuring debug logging using an key ending with '/debugLog'. --- .../configuration/M2ELogbackConfigurator.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java b/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java index d2dd5b4e16..3d372af7c0 100644 --- a/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java +++ b/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java @@ -19,6 +19,8 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.SortedMap; import java.util.Timer; @@ -27,14 +29,17 @@ import java.util.function.BooleanSupplier; import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; +import org.eclipse.osgi.service.debug.DebugOptions; import ch.qos.logback.classic.BasicConfigurator; +import ch.qos.logback.classic.Level; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.classic.spi.Configurator; @@ -53,7 +58,7 @@ public class M2ELogbackConfigurator extends BasicConfigurator implements Configu @Override public void configure(LoggerContext lc) { - // Bug 337167: Configuring Logback requires the state-location. If not yet initialized it will be initialized to the default value, + // Bug 337167: Configuring Logback requires the state-location. If not yet initialized it will be initialized to the default value, // but this prevents the workspace-chooser dialog to show up in a stand-alone Eclipse-product. Therefore we have to wait until the resources plug-in has started. // This happens if a Plug-in that uses SLF4J is started before the workspace has been selected. if(!isStateLocationInitialized()) { @@ -86,7 +91,7 @@ private synchronized void configureLogback(LoggerContext lc) { } loadConfiguration(lc, configFile.toUri().toURL()); - //Delete old logs in legacy logback plug-in's state location. Can sum up to 1GB of disk-space. + //Delete old logs in legacy logback plug-in's state location. Can sum up to 1GB of disk-space. // TODO: can be removed when some time has passed and it is unlikely old workspaces that need clean-up are used. Path legacyLogbackState = stateDir.resolveSibling("org.eclipse.m2e.logback.configuration"); if(Files.isDirectory(legacyLogbackState)) { @@ -113,9 +118,33 @@ private static void loadConfiguration(LoggerContext lc, URL configFile) throws J StatusPrinter.printInCaseOfErrorsOrWarnings(lc); + applyDebugLogLevels(lc); + logJavaProperties(LoggerFactory.getLogger(M2ELogbackConfigurator.class)); } + private static void applyDebugLogLevels(LoggerContext lc) { + Bundle bundle = Platform.getBundle("org.eclipse.m2e.core"); // fragments don't have a BundleContext + ServiceReference debugOptionsRef = bundle.getBundleContext().getServiceReference(DebugOptions.class); + if(debugOptionsRef != null) { + DebugOptions debugOptions = bundle.getBundleContext().getService(debugOptionsRef); + if(debugOptions != null) { + try { + Map options = debugOptions.getOptions(); + for(Entry entry : options.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if(key.endsWith("/debugLog") && "true".equals(value)) { + lc.getLogger(key.replace("/debugLog", "")).setLevel(Level.DEBUG); + } + } + } finally { + bundle.getBundleContext().ungetService(debugOptionsRef); + } + } + } + } + // --- utility methods --- private static boolean isStateLocationInitialized() { From a083b3e962c827b50b41cad499bc65100b52f321 Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Mon, 1 May 2023 15:17:07 -0300 Subject: [PATCH 2/4] Allow configuring the console filter threshold with system property. --- .../defaultLogbackConfiguration/logback.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/org.eclipse.m2e.logback/defaultLogbackConfiguration/logback.xml b/org.eclipse.m2e.logback/defaultLogbackConfiguration/logback.xml index abdf1c7373..9effde76cb 100644 --- a/org.eclipse.m2e.logback/defaultLogbackConfiguration/logback.xml +++ b/org.eclipse.m2e.logback/defaultLogbackConfiguration/logback.xml @@ -4,7 +4,7 @@ %date [%thread] %-5level %logger{35} - %msg%n - OFF + ${org.eclipse.m2e.log.console.threshold:-OFF} @@ -31,7 +31,7 @@ - + From 6d11e7bcdfad58abf15fd975f70ee7855f54023a Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Mon, 1 May 2023 15:17:33 -0300 Subject: [PATCH 3/4] Enable console logging when '-consolelog' is specified --- .../configuration/M2ELogbackConfigurator.java | 55 +++++++++++++------ 1 file changed, 39 insertions(+), 16 deletions(-) diff --git a/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java b/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java index 3d372af7c0..91ac2b864c 100644 --- a/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java +++ b/org.eclipse.m2e.logback/src/org/eclipse/m2e/logback/configuration/M2ELogbackConfigurator.java @@ -29,7 +29,7 @@ import java.util.function.BooleanSupplier; import org.osgi.framework.Bundle; -import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +37,7 @@ import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Status; import org.eclipse.osgi.service.debug.DebugOptions; +import org.eclipse.osgi.service.environment.EnvironmentInfo; import ch.qos.logback.classic.BasicConfigurator; import ch.qos.logback.classic.Level; @@ -56,6 +57,9 @@ public class M2ELogbackConfigurator extends BasicConfigurator implements Configu // This has to match the log directory in defaultLogbackConfiguration/logback.xml private static final String PROPERTY_LOG_DIRECTORY = "org.eclipse.m2e.log.dir"; //$NON-NLS-1$ + // This has to match the log directory in defaultLogbackConfiguration/logback.xml + private static final String PROPERTY_LOG_CONSOLE_THRESHOLD = "org.eclipse.m2e.log.console.threshold"; //$NON-NLS-1$ + @Override public void configure(LoggerContext lc) { // Bug 337167: Configuring Logback requires the state-location. If not yet initialized it will be initialized to the default value, @@ -89,6 +93,11 @@ private synchronized void configureLogback(LoggerContext lc) { if(System.getProperty(PROPERTY_LOG_DIRECTORY, "").length() <= 0) { //$NON-NLS-1$ System.setProperty(PROPERTY_LOG_DIRECTORY, stateDir.toAbsolutePath().toString()); } + if(System.getProperty(PROPERTY_LOG_CONSOLE_THRESHOLD, "").length() <= 0) { //$NON-NLS-1$ + if(isConsoleLogEnable()) { + System.setProperty(PROPERTY_LOG_CONSOLE_THRESHOLD, Level.DEBUG.levelStr); + } + } loadConfiguration(lc, configFile.toUri().toURL()); //Delete old logs in legacy logback plug-in's state location. Can sum up to 1GB of disk-space. @@ -105,7 +114,17 @@ private synchronized void configureLogback(LoggerContext lc) { Files.delete(legacyLogbackState); } } catch(Exception e) { - LOG.log(Status.warning("Exception while setting up logging:" + e.getMessage(), e)); //$NON-NLS-1$ + LOG.log(Status.warning("Exception while setting up logging:" + e.getMessage(), e)); + } + } + + private boolean isConsoleLogEnable() { + ServiceTracker tracker = openServiceTracker(EnvironmentInfo.class); + try { + EnvironmentInfo environmentInfo = (EnvironmentInfo) tracker.getService(); + return environmentInfo != null && "true".equals(environmentInfo.getProperty("eclipse.consoleLog")); //$NON-NLS-1$ + } finally { + tracker.close(); } } @@ -124,27 +143,31 @@ private static void loadConfiguration(LoggerContext lc, URL configFile) throws J } private static void applyDebugLogLevels(LoggerContext lc) { - Bundle bundle = Platform.getBundle("org.eclipse.m2e.core"); // fragments don't have a BundleContext - ServiceReference debugOptionsRef = bundle.getBundleContext().getServiceReference(DebugOptions.class); - if(debugOptionsRef != null) { - DebugOptions debugOptions = bundle.getBundleContext().getService(debugOptionsRef); + ServiceTracker tracker = openServiceTracker(DebugOptions.class); + try { + DebugOptions debugOptions = (DebugOptions) tracker.getService(); if(debugOptions != null) { - try { - Map options = debugOptions.getOptions(); - for(Entry entry : options.entrySet()) { - String key = entry.getKey(); - String value = entry.getValue(); - if(key.endsWith("/debugLog") && "true".equals(value)) { - lc.getLogger(key.replace("/debugLog", "")).setLevel(Level.DEBUG); - } + Map options = debugOptions.getOptions(); + for(Entry entry : options.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + if(key.endsWith("/debugLog") && "true".equals(value)) { + lc.getLogger(key.replace("/debugLog", "")).setLevel(Level.DEBUG); } - } finally { - bundle.getBundleContext().ungetService(debugOptionsRef); } } + } finally { + tracker.close(); } } + private static ServiceTracker openServiceTracker(Class serviceClass) { + Bundle bundle = Platform.getBundle("org.eclipse.m2e.core"); // fragments don't have a BundleContext + ServiceTracker tracker = new ServiceTracker<>(bundle.getBundleContext(), serviceClass, null); + tracker.open(); + return tracker; + } + // --- utility methods --- private static boolean isStateLocationInitialized() { From e9a3355b570226a94353fc5146138370335e4092 Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Thu, 4 May 2023 12:30:45 +0200 Subject: [PATCH 4/4] Bump version --- org.eclipse.m2e.logback/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org.eclipse.m2e.logback/META-INF/MANIFEST.MF b/org.eclipse.m2e.logback/META-INF/MANIFEST.MF index 0dc4aa025a..e97d75b2bd 100644 --- a/org.eclipse.m2e.logback/META-INF/MANIFEST.MF +++ b/org.eclipse.m2e.logback/META-INF/MANIFEST.MF @@ -1,7 +1,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.m2e.logback;singleton:=true -Bundle-Version: 2.1.100.qualifier +Bundle-Version: 2.1.101.qualifier Bundle-Name: M2E Logback Appender and Configuration Bundle-Vendor: Eclipse.org - m2e Bundle-RequiredExecutionEnvironment: JavaSE-17