Thanks for the pointer Neil. No I haven’t seen that section yet.

Just for clarification. That is introduced with DS 1.4 right? But as of now 
there is no default implementation for DS 1.4. At least I do not see a new 
Apache Felix SCR version for DS 1.4. And with DS 1.3 that automatic mapping 
from LoggerFactory to Logger or FormattedLogger would not work. At least in PDE 
I get a compile error because field type and service property do not match. And 
at runtime of course a NPE comes up, not sure if this is caused by PDE because 
of an incorrect component XML (although it looks correct for me) or if it is 
because it does not work with DS 1.3.

Since I looked for a solution for the Eclipse Platform I haven’t tested 
Bndtools yet, but without a DS 1.4 implementation I suppose my tests will fail.

Mit freundlichen Grüßen / Best regards

Dirk Fauth

Automotive Service Solutions, ESI application (AA-AS/EIS2-EU)
Robert Bosch GmbH | Postfach 11 29 | 73201 Plochingen | GERMANY | 
www.bosch.com<http://www.bosch.com>
Tel. +49 7153 666-1155 | dirk.fa...@de.bosch.com<mailto:dirk.fa...@de.bosch.com>

Sitz: Stuttgart, Registergericht: Amtsgericht Stuttgart, HRB 14000;
Aufsichtsratsvorsitzender: Franz Fehrenbach; Geschäftsführung: Dr. Volkmar 
Denner,
Prof. Dr. Stefan Asenkerschbaumer, Dr. Rolf Bulander, Dr. Stefan Hartung, Dr. 
Markus Heyn, Dr. Dirk Hoheisel,
Christoph Kübel, Uwe Raschke, Peter Tyroller


Von: Neil Bartlett [mailto:njbartl...@gmail.com]
Gesendet: Montag, 9. April 2018 13:03
An: Fauth Dirk (AA-AS/EIS2-EU) <dirk.fa...@de.bosch.com>; OSGi Developer Mail 
List <osgi-dev@mail.osgi.org>
Betreff: Re: [osgi-dev] How to use LoggerFactory with DS?

Hi Dirk, have you read the section on Logger support in R7 Declarative 
Services? See 
https://osgi.org/specification/osgi.cmpn/7.0.0/service.component.html#service.component-logger.support
 (or section 112.3.12 in the PDF).
This section shows the recommended approach to obtaining a Logger instance in a 
component.
Regards,
Neil

On Mon, Apr 9, 2018 at 11:56 AM, Fauth Dirk (AA-AS/EIS2-EU) via osgi-dev 
<osgi-dev@mail.osgi.org<mailto:osgi-dev@mail.osgi.org>> wrote:
Hi,

as with R7 the LogService is deprecated and instead the LoggerFactory should be 
used, I wanted to start integrating the LoggerFactory in the Eclipse Platform. 
But there is not much information about how the usage should be.

From what I understand so far, the idea is similar to SLF4J that a Logger 
instance is created via LoggerFactory. The Logger provides API for logging 
similar to SLF4J, even with placeholder parameters and exception logging (I 
like that pretty much).

But there is one thing I am not sure if I understand everything correctly. With 
SLF4J the LoggerFactory is static and the best practice is to create one Logger 
instance per class by using the LoggerFactory on a static field. This is done 
to reduce the needed amount of memory for cases where a lot of instances are 
created of a type. With OSGi R7 the LoggerFactory is a service and not a static 
helper class. So for DS I need to specify a reference. But actually I don’t 
want the LoggerFactory, I want a Logger. And I don’t want to create a Logger 
instance per log statement. I therefore thought of the following pattern for 
getting an OSGi Logger in my component:

@Component
public class StringInverterImpl implements StringInverter {

       private Logger logger;

       @Override
       public String invert(String input) {
             logger.info("received {} to invert", input);
             return new StringBuilder(input).reverse().toString();
       }

       @Reference
       void setLoggerFactory(LoggerFactory factory) {
             this.logger = factory.getLogger(getClass());
       }
}

So I have a mandatory reference to LoggerFactory, and once the reference is 
set, I use it to get the Logger instance. Since the reference is static and 
mandatory I don’t need to take care if the LoggerFactory comes and goes at 
runtime. And as the service instance typically only exists once, there is also 
no memory overhead with this. For multi-instance components created by a 
factory, this could even have the advantage to create a Logger with a name that 
corresponds to the instance. Which would in turn make the log outputs more 
speaking in terms of knowing which component instance created the log output.

Would this be the recommended way of using logging in R7? Are my observations 
correct or do I misunderstand something?

I would be happy to even write a small blog post about that topic if nobody 
else is currently writing something with regards to logging in R7.

Mit freundlichen Grüßen / Best regards

Dirk Fauth

Automotive Service Solutions, ESI application (AA-AS/EIS2-EU)
Robert Bosch GmbH | Postfach 11 29 | 73201 Plochingen | GERMANY | 
www.bosch.com<http://www.bosch.com>
Tel. +49 7153 666-1155 | dirk.fa...@de.bosch.com<mailto:dirk.fa...@de.bosch.com>

Sitz: Stuttgart, Registergericht: Amtsgericht Stuttgart, HRB 14000;
Aufsichtsratsvorsitzender: Franz Fehrenbach; Geschäftsführung: Dr. Volkmar 
Denner,
Prof. Dr. Stefan Asenkerschbaumer, Dr. Rolf Bulander, Dr. Stefan Hartung, Dr. 
Markus Heyn, Dr. Dirk Hoheisel,
Christoph Kübel, Uwe Raschke, Peter Tyroller


_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org<mailto:osgi-dev@mail.osgi.org>
https://mail.osgi.org/mailman/listinfo/osgi-dev

_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev

Reply via email to