Skip to content
kasemir edited this page Apr 6, 2016 · 4 revisions

Logging

Log messages should be based on java.util.logging.

Earlier use of SLF4J, Log4J, Apache.commons.logging and others is deprecated. While they each may have some slight advantage, we do want to limit a spread of logging library dependencies.

Obtaining a Logger

Use a logger that reflects the plugin name, which typically is possible by using the package name of a class.

In most cases, creating just one logger for each plugin with the name of the plugin is most effective:

class MyPluginActivator
{
    final private static String ID = "org.csstudio.my.plugin"; // Plugin ID
    final private static Logger logger = Logger.getLogger(ID);

In some cases, having more detailed loggers associated with a class is useful:

class MyClass
{
    final private static Logger logger = Logger.getLogger(getClass().getName());

As for using class.getName(), this will provide the full package name. For nested (inner) classes, it will return a name that is harder to read like org.csstudio.my.plugin.MyClass$1, but this information can be used by developers to locate the inner class.

class.getSimpleName() will not include the package name and should NOT be used to obtain a logger.

class.getCanonicalName() will be NULL for anonymous inner classes and it therefore less useful.

http://stackoverflow.com/questions/15202997/what-is-the-difference-between-canonical-name-simple-name-and-class-name-in-jav

Log Patterns

When encountering an exception, log the exception information which will result in a complete stack trace, including nested 'Caused by' exceptions:

catch (Exception ex)
{
    logger.log(Level.WARNING, "Something terrible happened", ex);

Log Levels

Clone this wiki locally