Skip to content

Commit

Permalink
Update the logging sample application and include the `jpro.onJVMSh…
Browse files Browse the repository at this point in the history
…utdown` configuration
  • Loading branch information
besidev committed Aug 17, 2023
1 parent 40dbedf commit eef5e53
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 9 deletions.
5 changes: 5 additions & 0 deletions logging/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Module descriptor.
*
* @author Besmir Beqiri
*/
module one.jpro.samples.logging {
requires javafx.controls;
requires java.logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package one.jpro.samples.logging;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This is a simple class that will be executed on JVM startup.
*
* @author Besmir Beqiri
*/
public class LogOnJVMShutdown {

private static final Logger jullogger = Logger.getLogger(LogOnJVMShutdown.class.getName());

public static void main(String[] args) {

jullogger.log(Level.INFO, "JUL Logging on JVM Shutdown!");

for (java.util.logging.Handler handler : Logger.getLogger("").getHandlers()) {
handler.flush();
}
LogOnJVMStartup.julShutdownTrigger.complete(null);
LogOnJVMStartup.loggerContext.stop();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.*;

/**
* This is a simple class that will be executed on JVM startup.
*
* @author Besmir Beqiri
*/
public class LogOnJVMStartup {

public static LoggerContext loggerContext = null;
private static final Logger logger = Logger.getLogger(LogOnJVMStartup.class.getName());

public static void main(String[] args) {
// // redirect j.u.l. logging to SLF4J
// redirect j.u.l. logging to SLF4J
SLF4JBridgeHandler.removeHandlersForRootLogger();

String logbackFile = LogOnJVMStartup.class.getResource("/logback-sample.xml").getFile();
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
String logbackFile = LogOnJVMStartup.class.getResource("/logback-it.xml").getFile();
loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
Expand All @@ -36,6 +37,63 @@ public static void main(String[] args) {
throw new RuntimeException(je.getMessage());
}

synchronizeJULShutdown();

logger.log(Level.INFO, "Logging on JVM Startup!");
}

public static void synchronizeJULShutdown() {
// For reference: https://stackoverflow.com/questions/60687511/why-is-the-new-java-logger-failing-to-work-consistently-here
Logger root = Logger.getLogger("");
Handler[] handlers = root.getHandlers();

for(Handler h : handlers) {
root.removeHandler(h);
}

root.addHandler(new CleanerJoin());

for(Handler h : handlers) {
root.addHandler(h);
}
}


public static CompletableFuture<Void> julShutdownTrigger = new CompletableFuture<>();
static class CleanerJoin extends Handler {

CleanerJoin() {
}

@Override
public void close() {
boolean interrupted = false;
try {
for(;;) {
try { //Could use LogManager to lookup timeout values and use a timed join.
julShutdownTrigger.get();
break;
} catch (ExecutionException e) {
reportError("Shutdown hook failed.", e, ErrorManager.CLOSE_FAILURE);
break;
} catch (InterruptedException retry) {
interrupted = true;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}

@Override
public void flush() {
}

@Override
public void publish(LogRecord r) {
isLoggable(r);
}
}
}
5 changes: 5 additions & 0 deletions logging/src/main/java/one/jpro/samples/logging/SampleApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
* Example application for custom logging configuration.
*
* @author Besmir Beqiri
*/
public class SampleApp extends Application {

@Override
Expand Down
6 changes: 4 additions & 2 deletions logging/src/main/resources/jpro.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
jpro.logConsole=false
jpro.logToJUL=true
jpro.onJVMStartup=one.jpro.samples.logging.LogOnJVMStartup
jpro.onFXStartup=one.jpro.samples.logging.LogOnFXStartup
jpro.forceShutdown = false
jpro.onJVMStartup=one.jpro.maven.LogOnJVMStartup
jpro.onJVMShutdown=one.jpro.maven.LogOnJVMShutdown
jpro.onFXStartup=one.jpro.maven.LogOnFXStartup
2 changes: 1 addition & 1 deletion logging/src/main/resources/logback-sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>[configOnJVMStartup] %-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n</pattern>
<pattern>[onJVMStartup] %-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n</pattern>
</encoder>
</appender>

Expand Down

0 comments on commit eef5e53

Please sign in to comment.