Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Is it possible to use System.Logger instead of org.eclipse.core.runtime.ILog?
Is it possible to use System.Logger instead of org.eclipse.core.runtime.ILog? [message #1856215] Mon, 28 November 2022 09:40 Go to next message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 346
Registered: August 2013
Senior Member
Hi

Some of our bundles are shared between an Eclipse-based application and a Spring-based application.

These bundles creates a logger as follows:
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Platform;
// ...
private static final ILog LOGGER = Platform.getLog(NumberHelpers.class);


Platform is not available in Spring, so we would like to create a logger as follows:
import java.lang.System.Logger.Level;
// ...
private static final System.Logger LOGGER = System.getLogger(NumberHelpers.class.toString());


It works. But the problem is that in the Eclipse application the log messages are written to console only. One can't view it in the Eclipse Error Log View. Is it possible to redirect messages from System.Logger to Eclipse ILog?
Re: Is it possible to use System.Logger instead of org.eclipse.core.runtime.ILog? [message #1856219 is a reply to message #1856215] Mon, 28 November 2022 12:06 Go to previous message
Denis Nikiforov is currently offline Denis NikiforovFriend
Messages: 346
Registered: August 2013
Senior Member
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

Previous Topic:Locale change and JFaceResources
Next Topic:Eclipse not open
Goto Forum:
  


Current Time: Wed May 22 05:18:02 GMT 2024

Powered by FUDForum. Page generated in 0.02998 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top