Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Logger-Layout?
Logger-Layout? [message #928693] Sun, 30 September 2012 22:26 Go to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
Hi,
I've just started using juno, and I'm trying to get used to die model (and of course with DI). For the beginning I injected a Logger (org.eclipse.e4.core.services.log.Logger) and it's working fine! Now I'm wondering how I could change the layout of the log-messages.? How can I do this? Is there a way like the properties-file in log4j?

Thanks!
Re: Logger-Layout? [message #929110 is a reply to message #928693] Mon, 01 October 2012 08:28 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi David

You can register your own ILoggerProvider as an OSGi service. This way you can choose your own logging framework (log4j, slf4j, ...).

Greetings
Christoph
Re: Logger-Layout? [message #955149 is a reply to message #929110] Tue, 23 October 2012 14:58 Go to previous messageGo to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
thanks for the answer...maybe I'm justed little bit confoused but how can register a Provider as an OSGi service?
Re: Logger-Layout? [message #955154 is a reply to message #955149] Tue, 23 October 2012 15:00 Go to previous messageGo to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
<<nonsense>>

[Updated on: Tue, 23 October 2012 15:09]

Report message to a moderator

Re: Logger-Layout? [message #956075 is a reply to message #955149] Wed, 24 October 2012 08:14 Go to previous messageGo to next message
Christoph Keimel is currently offline Christoph KeimelFriend
Messages: 482
Registered: December 2010
Location: Germany
Senior Member
Hi David,

this is how I register an ILoggerProvider which uses slf4j as logging framework.

public class MyPlugin implements BundleActivator {
	private ServiceRegistration<?> registration;
	
	@Override
	public void start(BundleContext bundleContext) throws Exception {
		 
	    // Use SLF4J as LoggerProvider for e4
	    ILoggerProvider logService = new ILoggerProvider() {
	    	@Override
	    	public org.eclipse.e4.core.services.log.Logger getClassLogger(Class<?> clazz) {
	    		return new Slf4jLoggerWrapper(LoggerFactory.getLogger(clazz));
	    	}
	    };
	    registration = bundleContext.registerService(ILoggerProvider.class, logService, null);
	}

	@Override
	public void stop(BundleContext bundleContext) throws Exception {
		registration.unregister();
		registration = null;
	} 
}


Greetings
Christoph

[Updated on: Thu, 25 October 2012 09:14]

Report message to a moderator

Re: Logger-Layout? [message #956293 is a reply to message #956075] Wed, 24 October 2012 11:46 Go to previous messageGo to next message
Eclipse UserFriend
Note: if you do this via a declarative service, then OSGi will activate your bundle on demand.
Re: Logger-Layout? [message #956896 is a reply to message #956293] Wed, 24 October 2012 22:08 Go to previous messageGo to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
thanks for your help! i know i'm a greenhorn but i'm trying to get use to eclipse...

just to be sure: the important thing is the "return new Slf4jLoggerWrapper(LoggerFactory.getLogger(clazz));".

so when i get this right i could create a LogFactory which returns a self-made Log4JLogger which extends org.eclipse.e4.core.services.log.Logger and where i have my "private org.apache.log4j.Logger log". in this Log4JLogger i implement for example the method "info" like this:
@Override
	public void info(Throwable t, String message) {
		if (this.isInfoEnabled()) {
			this.log.info(buildLogMessage(message, null), t);
		}
	}


could that work?


[Updated on: Wed, 24 October 2012 22:39]

Report message to a moderator

Re: Logger-Layout? [message #956950 is a reply to message #956896] Wed, 24 October 2012 23:09 Go to previous messageGo to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
it works!
thank you for the help!

[Updated on: Wed, 24 October 2012 23:09]

Report message to a moderator

Re: Logger-Layout? [message #959601 is a reply to message #956950] Fri, 26 October 2012 21:11 Go to previous messageGo to next message
David Reichl is currently offline David ReichlFriend
Messages: 9
Registered: September 2012
Junior Member
hi,

I just changed my Logger so I can use a log4j-Logger and to use simple "@Inject Logger log" without define the class by my self.

My first thought was:

@Creatable
public class Logger extends org.eclipse.e4.core.services.log.Logger{
	private org.apache.log4j.Logger log = null;
	protected static char sign = '#';

	@Inject
	@SuppressWarnings("unchecked")
	public Logger(@Active MContribution contribution) {	
		String uri = contribution.getContributionURI();
		String[] uriArray = uri.split("\\.");
		String uriClass = uriArray[uriArray.length-1];
		this.log = org.apache.log4j.Logger.getLogger(uriClass);

	}
        .... 


But is this well written? Is there anotherway to get the class?
Re: Logger-Layout? [message #960629 is a reply to message #959601] Sat, 27 October 2012 17:08 Go to previous messageGo to next message
Eclipse UserFriend
A few comments:



  • Injection will fail if there's no MContribution available. You can use @Optional to avoid that so that the injector will inject "null" if not found.
  • But remember that the injector will normally traverse up the context hierarchy to resolve values. So your MContribution may in fact come from the injection-requestor's ancestor instead. You may choose to be injected with the IEclipseContext instead: you can then request local resolution of different values within that context.
  • I don't remember the scoping of @Creatable: IIRC the Creatable instance is remembered for that context and its descendants. So it you request a Logger from the MApplication, then that single logger instance will be used for all windows & parts. If you instead provide a context function (either declaratively through an OSGi Declarative Services, as an OSGi Service, or by registering a ContextFunction object with a context), you can compute and return a result on each injection: the value returned isn't placed into the context unless your context function explicitly places it into the context.




Re: Logger-Layout? [message #1691095 is a reply to message #959601] Thu, 02 April 2015 10:00 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

@David,
thanks for sharing all this, and thanks to all helpers.

Would you please share your Log4j org.eclipse.e4.core.services.log.Logger here? Might be useful to more than one.

@All
Btw: what if I don't want to use external logging libraries (Log4j, Slf4j, etc.) ? What is the default logger provider and how can I fetch it?

Thank you very much,
-Piero

[Updated on: Thu, 02 April 2015 10:01]

Report message to a moderator

Re: Logger-Layout? [message #1691104 is a reply to message #956075] Thu, 02 April 2015 10:41 Go to previous message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 114
Registered: January 2015
Senior Member

Hi Christoph,
it's good to see an example: thanks for sharing.

@All
I tried registering this declarative logging service too, but how could I tell the IEventBroker to use such a service?
The thing is, I am trying to use the broker on a custom class I inject in the bundle context via Activator, but I always get no actual value was found for the argument "Logger".

@see https://www.eclipse.org/forums/index.php?t=msg&th=370090&goto=904549&#msg_904549
Previous Topic:Problem with IEventBroker
Next Topic:Help My Rcp application suddenly looks weird I dont know what I messed
Goto Forum:
  


Current Time: Tue Mar 19 11:06:01 GMT 2024

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

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

Back to the top