To log or not to log !
Logging is so important to diagnose UI bugs.

Logging

My first idea about logging for JavaFX apps was to build myself a small lightweight and powerful module to add log capability to JRebirth Framework.

But after some hundred of lines of code written, I realized that using a real logging library could be interesting and not too heavy to embed.

Logging Facade

I choose to add dependency to slf4j-api : Simple Logging Facade 4 Java

This API is lightweight because the jar is near to 10 kb.

Add the SLF4J API to your pom.xml:

<dependency>
	<groupid>org.slf4j</groupid>
	<artifactid>slf4j-api</artifactid>
	<version>1.7.5</version>
</dependency>

The slf4j api allow to declare logger for JRebirth Framework but doesn't provide any logger implementation. So you need to add another dependency to choose the implementation you want to use.

By default all logs are rejected and you will have this message into the Java console at startup:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See https://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

There are 3 implementations available:

  • NOP
  • LOGback
  • SimpleLogger

NOP

To avoid this error message to appear you can add the No-OPeration dependency, it will log nothing but avoid the previous error message:

<dependency>
	<groupid>org.slf4j</groupid>
	<artifactid>slf4j-nop</artifactid>
	<version>1.7.5</version>
</dependency>

LOGback

Personally I choose LOGback implementation because it represents a good compromise between performances and customization. It was finely integrated because it was written by the same team as slf4j.

LOGBack jars are quite heavy and weight more than 800 kb so it could be a problem for tiny applications. To use it you must add this dependency into your pom.xml :

<dependency>
	<groupid>ch.qos.logback</groupid>
	<artifactid>logback-classic</artifactid>
	<version>1.0.13</version>
</dependency>

Then you must add a configuration file like this one into the application classpath (from the JRebirth Analyzer showcase):

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

	<appender name="FILE" class="ch.qos.logback.core.FileAppender">
		<file>JRebirthAnalyzer.log</file>
		<encoder>
			<pattern>%date %level [%thread{6}] %logger{10} [%file:%line] %msg%n</pattern>
		</encoder>
	</appender>

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
		<!-- %level %msg%n -->
			<pattern>%level [%thread] %file:%line - %msg%n</pattern>
		</encoder>
	</appender>


	<root level="trace">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
	</root>

</configuration>

SimpleLogger

If you don't want to embed 800 kb of jar to provide logging feature you can use the simple logger provided by slf4j, it's only a 10 kb jar file.

You just have to add this dependency to your pom.xml:

<dependency>
	<groupid>org.slf4j</groupid>
	<artifactid>slf4j-simple</artifactid>
	<version>1.7.5</version>
</dependency>

JRebirth Logs

All JRebirth logs use an internationalized engine allowing us to provide extensible log messaging.

It could be seen as an overkill feature but it has some advantages:

  • Provide localized log message
  • Manage log level activation automatically
  • Parameterize log level, to increase or decrease it by customization
  • Disable Message resolution for high performance

Note that you can still use your logger basic features.

Short UML Diagram:

Logging Class Diagram

JRebirth provides its own LoggerFactory that you can initialize like this (from JRebirthThread, which also implements a message container interface):

public final class JRebirthThread extends Thread implements ConcurrentMessages {

    /** The class logger. */
    private static final JRLogger LOGGER = JRLoggerFactory.getLogger(JRebirthThread.class);

Then you can log your message like this, using a MessageItem constant declared on the message container interface:

public void runIntoJTP(final JRebirthRunnable runnable) {

    if (getFacade().executorService().checkAvailability(runnable.priority())) {
        getFacade().executorService().execute(runnable);
        LOGGER.log(JTP_QUEUED, runnable.toString());
    } else {
        getFacade().highPriorityExecutorService().execute(runnable);
        LOGGER.log(HPTP_QUEUED, runnable.toString());
    }

}

All these log messages are store into an interface implemented by the class to let them accessible

package org.jrebirth.af.core.concurrent;

import static org.jrebirth.af.core.resource.Resources.create;

import org.jrebirth.af.api.resource.i18n.JRLevel;
import org.jrebirth.af.api.resource.i18n.MessageItem;
import org.jrebirth.af.core.log.JRebirthMarkers;
import org.jrebirth.af.core.resource.i18n.LogMessage;
import org.jrebirth.af.core.resource.i18n.MessageContainer;

/**
 * The class <strong>ConcurrentMessages</strong>.
 *
 * Messages used by the Concurrent package.
 *
 * @author Sébastien Bordes
 */
public interface ConcurrentMessages extends MessageContainer {

    /** JRebirthThread. */

    /** "Runnable submitted to JTP with hashCode={0}" . */
    MessageItem JTP_QUEUED = create(new LogMessage("jrebirth.concurrent.jtpQueued", JRLevel.Trace, JRebirthMarkers.CONCURRENT));

    /** "Runnable submitted to HPTP with hashCode={0}" . */
    MessageItem HPTP_QUEUED = create(new LogMessage("jrebirth.concurrent.hptpQueued", JRLevel.Trace, JRebirthMarkers.CONCURRENT));

    /** "An exception occurred during JRebirth BootUp" . */
    MessageItem BOOT_UP_ERROR = create(new LogMessage("jrebirth.concurrent.bootUpError", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** "An exception occurred into the JRebirth Thread". */
    MessageItem JIT_ERROR = create(new LogMessage("jrebirth.concurrent.jitError", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** "An error occurred while shutting down the application ". */
    MessageItem SHUTDOWN_ERROR = create(new LogMessage("jrebirth.concurrent.shutdownError", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** AbstractJrbRunnable. */

    /** "Run> {0}". */
    MessageItem RUN_IT = create(new LogMessage("jrebirth.concurrent.runIt", JRLevel.Trace, JRebirthMarkers.CONCURRENT));

    /** "Thread error : {0} ". */
    MessageItem THREAD_ERROR = create(new LogMessage("jrebirth.concurrent.threadError", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** JRebirthThreadPoolExecutor. */

    /** "JTP returned an error" . */
    MessageItem JTP_ERROR = create(new LogMessage("jrebirth.concurrent.jtpError", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** "JTP returned an error with rootCause =>". */
    MessageItem JTP_ERROR_EXPLANATION = create(new LogMessage("jrebirth.concurrent.jtpErrorExplanation", JRLevel.Error, JRebirthMarkers.CONCURRENT));

    /** "Future (hashcode={}) returned object : {0}". */
    MessageItem FUTURE_DONE = create(new LogMessage("jrebirth.concurrent.futureDone", JRLevel.Trace, JRebirthMarkers.CONCURRENT));

}

Event Tracking

JRebirth can log component lifecycle events (creation, access, destruction) and wave flows for analysis by the JRebirth Analyzer tool.

To enable, configure these loggers in your logback.xml:

<logger name="org.jrebirth.af.core.facade" level="TRACE"/>
<logger name="org.jrebirth.af.core.link" level="TRACE"/>

The output format matches the Analyzer's EDT file parser. Redirect to a file appender for post-mortem analysis.