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

Allow configuration of DEBUG logging using Eclipse bundle tracing #1366

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion org.eclipse.m2e.logback/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>OFF</level> <!-- change to DEBUG to mimic '-consolelog' behaviour -->
<level>${org.eclipse.m2e.log.console.threshold:-OFF}</level> <!-- change to DEBUG to mimic '-consolelog' behaviour -->
</filter>
</appender>

Expand All @@ -31,7 +31,7 @@

<appender name="MavenConsoleLog" class="org.eclipse.m2e.logback.appender.MavenConsoleAppender">
</appender>

<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,14 +29,18 @@
import java.util.function.BooleanSupplier;

import org.osgi.framework.Bundle;
import org.osgi.util.tracker.ServiceTracker;
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 org.eclipse.osgi.service.environment.EnvironmentInfo;

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;
Expand All @@ -51,9 +57,12 @@ 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,
// 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()) {
Expand Down Expand Up @@ -84,9 +93,14 @@ 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.
//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)) {
Expand All @@ -100,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<EnvironmentInfo, Object> 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();
}
}

Expand All @@ -113,9 +137,37 @@ 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) {
ServiceTracker<DebugOptions, Object> tracker = openServiceTracker(DebugOptions.class);
try {
DebugOptions debugOptions = (DebugOptions) tracker.getService();
if(debugOptions != null) {
Map<String, String> options = debugOptions.getOptions();
for(Entry<String, String> 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 {
tracker.close();
}
}

private static <T> ServiceTracker<T, Object> openServiceTracker(Class<T> serviceClass) {
Bundle bundle = Platform.getBundle("org.eclipse.m2e.core"); // fragments don't have a BundleContext
ServiceTracker<T, Object> tracker = new ServiceTracker<>(bundle.getBundleContext(), serviceClass, null);
tracker.open();
return tracker;
}

// --- utility methods ---

private static boolean isStateLocationInitialized() {
Expand Down