Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Efxclipse/Runtime/Recipes"
(→Logging) |
(→Usage) |
||
Line 20: | Line 20: | ||
<source lang="java"> | <source lang="java"> | ||
− | + | import org.eclipse.fx.core.log.Logger; | |
− | + | import org.eclipse.fx.osgi.util.LoggerCreator; | |
public class MyClass { | public class MyClass { | ||
Line 28: | Line 28: | ||
// .... | // .... | ||
} | } | ||
+ | </source> | ||
+ | |||
+ | ==== LoggerFactory Service ==== | ||
+ | |||
+ | The different logger bundles contribute their LoggerFactory implementation into the OSGi-Registry. In case you are e.g. contributing a service via DS you can get simple add a service reference and you'll get the LoggerFactory with the highest rank injected. | ||
+ | |||
+ | <source lang="java"> | ||
+ | import org.eclipse.fx.core.log.LoggerFactory; | ||
+ | import org.eclipse.fx.core.log.Logger; | ||
+ | |||
+ | public class MyServiceImpl implements MyService { | ||
+ | private Logger logger; | ||
+ | |||
+ | public void setLoggerFactory(LoggerFactory factory) { | ||
+ | this.logger = factory.createLogger(MyService.class.getName()); | ||
+ | } | ||
+ | |||
+ | public void unsetLoggerFactory(LoggerFactory factory) { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="myservice"> | ||
+ | <implementation class="impl.MyServiceImpl"/> | ||
+ | <service> | ||
+ | <provide interface="service.MyService"/> | ||
+ | </service> | ||
+ | <reference bind="setLoggerFactory" cardinality="1..1" interface="org.eclipse.fx.core.log.LoggerFactory" name="LoggerFactory" policy="static" unbind="unsetLoggerFactory"/> | ||
+ | </scr:component> | ||
</source> | </source> | ||
== DI == | == DI == |
Revision as of 17:22, 6 December 2013
This page holds best practice recipes when writing JavaFX application using e(fx)clipse
Core
Logging
e(fx)clipse has its own logging facade org.eclipse.fx.core.log.Logger
which allows to plug-in different log frameworks.
Currently available are:
-
java.util.Logging
(default) - log4j by adding
org.eclipse.fx.core.log4j
bundle to your OSGi-Runtime
Usage
There are different ways to use get a logger.
LoggerCreator
If you are running on OSGi you can add the org.eclipse.fx.osgi.util
bundle which provides access to the LoggerCreator
factory class
import org.eclipse.fx.core.log.Logger; import org.eclipse.fx.osgi.util.LoggerCreator; public class MyClass { private static Logger LOGGER = LoggerCreator.createLogger(MyClass.class); // .... }
LoggerFactory Service
The different logger bundles contribute their LoggerFactory implementation into the OSGi-Registry. In case you are e.g. contributing a service via DS you can get simple add a service reference and you'll get the LoggerFactory with the highest rank injected.
import org.eclipse.fx.core.log.LoggerFactory; import org.eclipse.fx.core.log.Logger; public class MyServiceImpl implements MyService { private Logger logger; public void setLoggerFactory(LoggerFactory factory) { this.logger = factory.createLogger(MyService.class.getName()); } public void unsetLoggerFactory(LoggerFactory factory) { } }
<?xml version="1.0" encoding="UTF-8"?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="myservice"> <implementation class="impl.MyServiceImpl"/> <service> <provide interface="service.MyService"/> </service> <reference bind="setLoggerFactory" cardinality="1..1" interface="org.eclipse.fx.core.log.LoggerFactory" name="LoggerFactory" policy="static" unbind="unsetLoggerFactory"/> </scr:component>