Skip to content

Commit

Permalink
Create logging sample (#7)
Browse files Browse the repository at this point in the history
* Add `logging` sample by demonstrating how to configure logging via `jpro.onJVMStartup` and `jpro.onFStartup` configuration options

* Update JPro to version `2023.2.0-M1` for the usage of latest introduced changes needed for this sample

* Retrieve JavaFX version from `gradle.properties`

* Reconfigure logging on `JVM` startup

* Upgrade JPro version to version `2023.2.2`

* Define dependencies version in the `gradle.properties`

* Define dependencies versions in `gradle.properties`

* Use version strings from `gradle.properties` in `build.gradle` files foe each sample

* Apply API changes regarding the update of the dependencies

* Update the routing API to the latest version

* Use the new routing API in the `website` sample

* Apply some cleanup

* Fix `link` on the header label

* Remove the exception from the `start` method in the `PWAApp` class

* Fix missing dependencies for the `logging-sample`

* Add `logging` sample description in the `README.md` file

* Leave only the Linux CI build with Java 17

* Update the `logging` sample application and include the `jpro.onJVMShutdown` configuration

* Fix resource paths in `jpro.conf` configuration file

* Fix logback configuration resource filename
  • Loading branch information
besidev committed Aug 18, 2023
1 parent 224f270 commit 591b32a
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
java-version: '17'
- name: compile
run: |
./gradlew clean multifilehandler:assemble filehandler:assemble html-jpro:assemble popups:jar native-scrolling:assemble
./gradlew clean jar
17 changes: 0 additions & 17 deletions .github/workflows/linux17.yml

This file was deleted.

27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ This project contains multiple samples for [jpro, which enables javafx in the we
Samples:


Sample | description | Demo
-------|---------------------------------------------------------------------------------------------------------------------------------------------|-------
popups | Shows how to use popups with JPro. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/PopupsApp)
multifilehandler | Shows how to upload and download mltiple files. | ---
filehandler | Shows how to upload and download files. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/FileHandlerApp)
html-jpro | A small sample, which shows how to use the WebAPI to communicate between JPro and a html-site. | ---
native-scrolling | A small sample on how to use native scrolling. | ---
website | A minimalistic website with multiple pages. It uses our library [jpro-web](https://github.com/Sandec/jpro-web) | ---
auth0 | Login with [Auth0](https://auth0.com/). Read the [readme](https://github.com/JPro-one/JPro-Samples/tree/master/auth0) for more details. | ---
suneditor | Example on how to integrate a html richtext editor - [suneditor]() in this case. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/JPro_Suneditor)
gps | Shows how to retrieve gps location and use it to show a map. | [link](https://www.jfx-ensemble.co/sample/jpro-samples/JPro_GeoLocation)
pwa | Install your Website as a desktop/mobile app! Read the [readme](https://github.com/JPro-one/JPro-Samples/tree/master/pwa) for more details.


| Sample | description | Demo |
|------------------|---------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
| popups | Shows how to use popups with JPro. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/PopupsApp) |
| multifilehandler | Shows how to upload and download mltiple files. | --- |
| filehandler | Shows how to upload and download files. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/FileHandlerApp) |
| html-jpro | A small sample, which shows how to use the WebAPI to communicate between JPro and a html-site. | --- |
| native-scrolling | A small sample on how to use native scrolling. | --- |
| website | A minimalistic website with multiple pages. It uses our library [jpro-web](https://github.com/Sandec/jpro-web) | --- |
| auth0 | Login with [Auth0](https://auth0.com/). Read the [readme](https://github.com/JPro-one/JPro-Samples/tree/master/auth0) for more details. | --- |
| logging | Shows how to configure logging via `jpro.onJVMStartup`, `jpro.onFXStartup` and `jpro.onJVMShutdown` configuration options. | --- |
| suneditor | Example on how to integrate a html richtext editor - [suneditor]() in this case. | [link](https://www.jfx-ensemble.com/sample/jpro-samples/JPro_Suneditor) |
| gps | Shows how to retrieve gps location and use it to show a map. | [link](https://www.jfx-ensemble.co/sample/jpro-samples/JPro_GeoLocation) |
| pwa | Install your Website as a desktop/mobile app! Read the [readme](https://github.com/JPro-one/JPro-Samples/tree/master/pwa) for more details. | |

# How to start #

Expand Down
42 changes: 42 additions & 0 deletions logging/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
45 changes: 45 additions & 0 deletions logging/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
application
id("org.openjfx.javafxplugin")
id("jpro-gradle-plugin")
}

group = "one.jpro.samples"
version = "0.0.1"

repositories {
mavenCentral()
}

val JAVAFX_VERSION = properties["JAVAFX_VERSION"] as String
val SLF4J_VERSION = properties["SLF4J_VERSION"] as String
val LOGBACK_VERSION = properties["LOGBACK_VERSION"] as String
val JUNIT_VERSION = properties["JUNIT_VERSION"] as String

javafx {
version = JAVAFX_VERSION
modules = listOf("javafx.graphics", "javafx.controls", "javafx.fxml", "javafx.media", "javafx.web")
}

dependencies {
implementation("org.slf4j:slf4j-api:$SLF4J_VERSION")
implementation("org.slf4j:jul-to-slf4j:$SLF4J_VERSION")
implementation("ch.qos.logback:logback-classic:$LOGBACK_VERSION")

testImplementation(platform("org.junit:junit-bom:$JUNIT_VERSION"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

application {
mainModule.set("one.jpro.samples.logging")
// Define the main class for the application.
mainClass.set("one.jpro.samples.logging.SampleApp")
}

jpro {
port = 8080
}

tasks.test {
useJUnitPlatform()
}
15 changes: 15 additions & 0 deletions logging/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Module descriptor.
*
* @author Besmir Beqiri
*/
module one.jpro.samples.logging {
requires javafx.controls;
requires java.logging;
requires org.slf4j;
requires jul.to.slf4j;
requires ch.qos.logback.core;
requires ch.qos.logback.classic;

exports one.jpro.samples.logging;
}
18 changes: 18 additions & 0 deletions logging/src/main/java/one/jpro/samples/logging/LogOnFXStartup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package one.jpro.samples.logging;

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

/**
* Log config on FX startup.
*
* @author Besmir Beqiri
*/
public class LogOnFXStartup {

private static final Logger logger = Logger.getLogger(LogOnFXStartup.class.getName());

public static void main(String[] args) {
logger.log(Level.INFO, "Some logging on JavaFX Application Startup!");
}
}
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
@@ -0,0 +1,99 @@
package one.jpro.samples.logging;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;

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
SLF4JBridgeHandler.removeHandlersForRootLogger();

String logbackFile = LogOnJVMStartup.class.getResource("/logback-sample.xml").getFile();
loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);

SLF4JBridgeHandler.install();
try {
configurator.doConfigure(logbackFile);
} catch (JoranException je) {
je.printStackTrace();
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);
}
}
}
34 changes: 34 additions & 0 deletions logging/src/main/java/one/jpro/samples/logging/SampleApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package one.jpro.samples.logging;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Stage;

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

@Override
public void start(Stage stage) {
Label label = new Label("Hello JPro!");
label.setFont(new Font(50));
label.setAlignment(Pos.CENTER);
stage.setScene(new Scene(label));
stage.show();
}

/**
* Application entry point.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
6 changes: 6 additions & 0 deletions logging/src/main/resources/jpro.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
jpro.logConsole=false
jpro.logToJUL=true
jpro.forceShutdown = false
jpro.onJVMStartup=one.jpro.samples.logging.LogOnJVMStartup
jpro.onJVMShutdown=one.jpro.samples.logging.LogOnJVMShutdown
jpro.onFXStartup=one.jpro.samples.logging.LogOnFXStartup
24 changes: 24 additions & 0 deletions logging/src/main/resources/logback-sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[onJVMStartup] %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/testFile.log</file>
<append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>[onJVMStartup] %-4relative [%thread] %-5level %logger{35} -%kvp- %msg%n</pattern>
</encoder>
</appender>

<logger name="Console" additivity="false"/>

<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ include "ensemble:site"
include "auth0"
include "suneditor"
include "gps"
include 'logging'

0 comments on commit 591b32a

Please sign in to comment.