Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to redirect Sun JDK Logger to a Runtime Workbench Console (repost) ?
How to redirect Sun JDK Logger to a Runtime Workbench Console (repost) ? [message #178463] Mon, 12 January 2004 09:20
Frederic Fays is currently offline Frederic FaysFriend
Messages: 7
Registered: July 2009
Junior Member
note: This message has already been posted,
http://dev.eclipse.org/newslists/news.eclipse.platform/msg12 149.html
but due the messy justification, we decided to repost the message.

Introduction:

We are developping on Mac OS X 10.2.8, with Eclipse 3.0 M6.

We have a system with several Eclipse plugins.
Most of them generate log output messages.
A Perspective has also been developped.

We have been using the Logger adding the following
to the VM parameters of the Runtime workbench:
-Djava.util.logging.config.file=/Applications/eclipse/loggin g.properties

Objective:

To have the Logger messages written in a customized Console
of our plugin using different colors for each Level type.

We also want to customize the default behavior using the file
"logging.properties", that is, we want to use our own "ConsoleHandler"
class.

e.g. Changes from the default "logging.properties" file:

# Our custom console handler (instead of
"java.util.logging.ConsoleHandler")
handlers = custom.eclipse.util.console.ConsoleHandler

Question:

Is there a way to have the LogManager instanciate our 'CustomHandler'
via the "org.eclipse.core.internal.plugins.PluginClassLoader" ClassLoader
instance ?

Our tests:

We have realized that since Eclipse 3.0 some new classes have been
incorporated
(MessageConsole, MessageConsoleStream among others in
"org.eclipse.ui.console")
to be able to interact with the Console view, and we decided to use them
for the logging purposes.

At the begining, we created a simple plugin to test if the properties
defined
in "logging.properties" were effectively read by the LogManager.
We only redefined "java.util.logging.ConsoleHandler.formatter" property
inside "logging.properties", pointing it to our CustomFormatter.
It did not work (ClassNotFoundException).

Then, we put the class "CustomFormatter" inside a Jar, afterward we placed
the Jar file in a directory visible for the main Eclipse application
classpath (in "~/Library/Java/Extension/").

We restarted Eclipse, Launched the Runtime Workbench and finally it worked
(we were able to see the formatted messages (printed on System.out)).
At that point, we were half-way, and we wanted to have the Log messages
printed inside our instance of org.eclipse.ui.console.MessageConsole
(instanciated in CustomPlugin).

Then we redefined the "handlers" property inside "logging.properties",
pointing it to our "CustomConsoleHandler".
We put "CustomConsoleHandler" class in a Jar, moved this Jar into a
location visible from the main Eclipse application classpath, restarted
Eclipse, relaunched the Runtime Workbench.

Nevertheless, we were not able to bind the "consoleStream" member
from the instance of "CustomConsoleHandler" to the desired
"MessageConsoleStream" taken from our CustomPlugin (see sniplet below).

After some research, we discovered that the "CustomConsoleHandler"
class was loaded by two different ClassLoader instances
("org.eclipse.core.internal.plugins.PluginClassLoader" and
"sun.misc.Launcher$ExtClassLoader").

And the "CustomConsoleHandler" instance made by the "LogManager" was
instanciated with the class definition loaded by the
"sun.misc.Launcher$ExtClassLoader" ClassLoader instance.
And from the CustomPlugin side we only can see the class definition
loaded by "org.eclipse.core.internal.plugins.PluginClassLoader"
ClassLoader instance.


--- CustomConsoleHandler.java (Minimalist custom ConsoleHandler) ----

package custom.eclipse.util.console;

import java.util.logging.LogRecord;
import org.eclipse.ui.console.MessageConsoleStream;

public class CustomConsoleHandler extends java.util.logging.ConsoleHandler{

public MessageConsoleStream consoleStream = null;

/**
* Constructor.
*/
public ConsoleHandler() {}

/**
* @see java.util.logging.Handler#publish(java.util.logging.LogRecor d)
*/
public void publish (LogRecord record) {
if(consoleStream != null)
consoleStream.print (this.getFormatter().format (record));
}
/**
* @see java.util.logging.Handler#flush()
*/
public void flush ()
{ /* Not implemented */ }
/**
* @see java.util.logging.Handler#close()
*/
public void close () throws SecurityException
{ /* Not implemented */ }
}

--- CustomPlugin.java (snippet) ----

package custom.eclipse.plugin.core;

import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import custom.eclipse.util.console.CustomConsoleHandler;
import org.eclipse.core.runtime.IPluginDescriptor;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.eclipse.ui.plugin.AbstractUIPlugin;

public class CustomPlugin extends AbstractUIPlugin {

public static final Logger LOGGER
= Logger.getLogger (CustomPlugin.class.getName ());

/**
* Constructor
*/
public SnowPlugin (IPluginDescriptor descriptor) {
super (descriptor);

MessageConsole console = new MessageConsole ("Custom console", null);
ConsolePlugin.getDefault ()
.getConsoleManager ()
.addConsoles (new IConsole []{ console });

Logger logger = LOGGER;
while (logger != null) {
Handler[] handlers = logger.getHandlers();

for (int i = 0; i < handlers.length; i++) {
Handler handler = handlers[i];

// never true (see next test for the explanation)
if(handlers[i] instanceof CustomConsoleHandler) {
((CustomConsoleHandler) handler).consoleStream
= console.newMessageStream();
}

if(handler
.getClass()
.getName()
.equals(CustomConsoleHandler.class.getName())) {

// prints "org.eclipse.core.internal.plugins.PluginClassLoader"
System.out.println(
CustomConsoleHandler
.class
.getClassLoader()
.getClass()
.getName());

// prints "sun.misc.Launcher$ExtClassLoader"
System.out.println(
handler
.getClass()
.getClassLoader()
.getClass()
.getName());
}

}
}
logger = logger.getParent();
}
LOGGER.log (Level.INFO, "test logging #INFO");
}

/* ... -snip- ... */
}
Previous Topic:Problems view only updates on a rebuild all
Next Topic:Eclipse - adding folders to CVS not visible using pserver
Goto Forum:
  


Current Time: Tue Jul 16 10:13:53 GMT 2024

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

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

Back to the top