Skip to content

Commit

Permalink
Enable console logging when '-consolelog' is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed May 2, 2023
1 parent a083b3e commit 6d11e7b
Showing 1 changed file with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
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;

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;
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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<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 @@ -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<DebugOptions> debugOptionsRef = bundle.getBundleContext().getServiceReference(DebugOptions.class);
if(debugOptionsRef != null) {
DebugOptions debugOptions = bundle.getBundleContext().getService(debugOptionsRef);
ServiceTracker<DebugOptions, Object> tracker = openServiceTracker(DebugOptions.class);
try {
DebugOptions debugOptions = (DebugOptions) tracker.getService();
if(debugOptions != null) {
try {
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);
}
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 {
bundle.getBundleContext().ungetService(debugOptionsRef);
}
}
} 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

0 comments on commit 6d11e7b

Please sign in to comment.