Table of Contents
CSS application code might need to log messages about warnings,
fatal errors, but also informational messages.
The suggestion is to use java.util.logging
, the logging
package that is included with Java.
To write log messages from application code, no additional CSS plugin is need. Simply invoke the logging API like this:
// Import logger from JRE import java.util.logging.Logger; // Fetch Logger, for example using current class name or plugin ID. Logger logger = Logger.getLogger(getClass().getName()); // Log a messages logger.warning("Something terrible happened"); logger.info("FYI, I just did something"); // Can use a formatter for lazy message generation logger.log(Level.DEBUG, "Value is {0}", value); // ... or to include detail of an exception catch (Exception ex) { logger.log(Level.WARNNIG, "Operation failed", ex); }
There are several ways to configure java.util.logging
,
for example via *.ini files in the JRE.
CSS includes a plugin org.csstudio.logging
that supports logging in
several ways:
To use java.util.logging
, your product needs to invoke
LogConfigurator.configureFromPreferences()
from within its startup code, usually just before entering the Workbench run loop.
The LogConfigurator registers a PluginLogListener
to add RCP log messages to
java.util.logging
. Then it reads Eclipse preferences to configure logging,
allowing to log to the console, files and JMS.
For details on how the logging to the console, files and JMS can be configured,
refer to the file org.csstudio.logging/preferences.ini
A related plugin org.csstudio.logging.ui
allows adjustments of the log preferences
from the preferences GUI.
There are several other logging systems for Java:
Log4j, Apache Commons Logging, SLF4J, ...
The point for shared CSS code should be to
not force the use of any particular external logging library
into a CSS product.
Shared CSS code should be content
with the java.util.logging
package
provided by the standard Java library.
When creating a site-specific product, you are of course free to include for example SLF4J, and use its “bridge” as a root logger for java.util.logging, so all CSS log messages will then be piped through SLF4J.