Skip to content

Commit

Permalink
Disambiguated logginbg resource loading
Browse files Browse the repository at this point in the history
  • Loading branch information
spoto committed Feb 28, 2024
1 parent fb3e5ec commit 3790475
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ private MokamintApplication() {}
public static void main(String[] args) {
main(MokamintApplication::new, args);
}

static {
loadLoggingConfig(() -> MokamintApplication.class.getModule().getResourceAsStream("logging.properties"));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,8 @@ private MokamintMiner() {}
public static void main(String[] args) {
main(MokamintMiner::new, args);
}

static {
loadLoggingConfig(() -> MokamintMiner.class.getModule().getResourceAsStream("logging.properties"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ private MokamintNode() {}
public static void main(String[] args) {
main(MokamintNode::new, args);
}

static {
loadLoggingConfig(() -> MokamintNode.class.getModule().getResourceAsStream("logging.properties"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ private MokamintPlotter() {}
public static void main(String[] args) {
main(MokamintPlotter::new, args);
}

static {
loadLoggingConfig(() -> MokamintPlotter.class.getModule().getResourceAsStream("logging.properties"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.mokamint.tools;

import java.io.IOException;
import java.io.InputStream;

import io.mokamint.tools.internal.AbstractToolImpl;

/**
Expand All @@ -27,4 +30,18 @@ public abstract class AbstractTool extends AbstractToolImpl {
* Builds the tool.
*/
protected AbstractTool() {}

/**
* A function that opens a resource.
*/
public interface ResourceOpener {

/**
* Opens the resource.
*
* @return the stream of the resource
* @throws IOException if the resource cannot be opened
*/
InputStream open() throws IOException;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package io.mokamint.tools.internal;

import java.io.IOException;
import java.net.URL;
import java.util.function.Supplier;
import java.util.logging.LogManager;

import io.mokamint.tools.AbstractTool;
import io.mokamint.tools.AbstractTool.ResourceOpener;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.HelpCommand;
Expand Down Expand Up @@ -70,18 +70,15 @@ public int run(String[] args) {
.execute(args);
}

static {
protected static void loadLoggingConfig(ResourceOpener opener) {
String current = System.getProperty("java.util.logging.config.file");
if (current == null) {
// if the property is not set, we provide a default (if it exists)
URL resource = AbstractToolImpl.class.getClassLoader().getResource("logging.properties");
if (resource != null) {
try (var is = resource.openStream()) {
LogManager.getLogManager().readConfiguration(is);
}
catch (SecurityException | IOException e) {
throw new RuntimeException("Cannot load the logging.properties file", e);
}
try (var is = opener.open()) {
LogManager.getLogManager().readConfiguration(is);
}
catch (SecurityException | IOException e) {
throw new RuntimeException("Cannot load the logging properties file", e);
}
}
}
Expand Down

0 comments on commit 3790475

Please sign in to comment.