I added the following implementation of System.LoggerFinder and System.Logger:
import java.lang.System.Logger;
import org.osgi.service.component.annotations.Component;
@Component(service = System.LoggerFinder.class)
public class EclipseLoggerFinder extends System.LoggerFinder {
@Override
public Logger getLogger(String name, Module module) {
return new EclipseLogger(name);
}
}
import java.lang.System.Logger;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
public class EclipseLogger implements Logger {
private final String name;
public EclipseLogger(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public boolean isLoggable(Level level) {
return level.getSeverity() >= Level.INFO.getSeverity();
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
if (isLoggable(level)) {
try {
Class<?> cls = Class.forName(name);
ILog logger = Platform.getLog(cls);
logger.log(new Status(toIStatus(level), cls, msg, thrown));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
if (isLoggable(level)) {
try {
Class<?> cls = Class.forName(name);
ILog logger = Platform.getLog(cls);
logger.log(new Status(toIStatus(level), cls, MessageFormat.format(format, params)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
private int toIStatus(Level level) {
switch (level) {
case TRACE:
case DEBUG:
return IStatus.OK;
case INFO:
return IStatus.INFO;
case WARNING:
return IStatus.WARNING;
case ERROR:
return IStatus.ERROR;
default:
throw new UnsupportedOperationException();
}
}
}
Created OSGI-INF/com.example.EclipseLoggerFinder.xml:
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.example.EclipseLoggerFinder">
<service>
<provide interface="java.lang.System$LoggerFinder"/>
</service>
<implementation class="com.example.EclipseLoggerFinder"/>
</scr:component>
Created also META-INF/services/java.lang.System$LoggerFinder:
com.example.EclipseLoggerFinder
But the EclipseLoggerFinder is ignored, and default System$LoggerFinder used instead.
How to register services for java.util.ServiceLoader in an Eclipse-based application?
[Updated on: Mon, 28 November 2022 12:17]
Report message to a moderator