Skip to content

Commit

Permalink
Configurable logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed Aug 3, 2023
1 parent 79e4d71 commit 806793e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
19 changes: 13 additions & 6 deletions src/qz/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,30 @@ public static Properties getTrayProperties() {
}

private static void setupFileLogging() {
//disable jetty logging
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "OFF");

Properties app = CertificateManager.loadProperties();
if(PrefsSearch.get(app, "log.disable", false)) {
return;
}

int logSize = PrefsSearch.get(app, "log.size", Constants.DEFAULT_LOG_SIZE);
int logRotations = PrefsSearch.get(app, "log.rotations", Constants.DEFAULT_LOG_ROTATIONS);
RollingFileAppender fileAppender = RollingFileAppender.newBuilder()
.setName("log-file")
.withAppend(true)
.setLayout(PatternLayout.newBuilder().withPattern("%d{ISO8601} [%p] %m%n").build())
.setFilter(ThresholdFilter.createFilter(Level.DEBUG, Filter.Result.ACCEPT, Filter.Result.DENY))
.withFileName(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".log")
.withFilePattern(FileUtilities.USER_DIR + File.separator + Constants.LOG_FILE + ".%i.log")
.withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(Constants.LOG_ROTATIONS)).build())
.withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(Constants.LOG_SIZE)))
.withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(logRotations)).build())
.withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(logSize)))
.withImmediateFlush(true)
.build();
fileAppender.start();

LoggerUtilities.getRootLogger().addAppender(fileAppender);

//disable jetty logging
System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
}
}
4 changes: 2 additions & 2 deletions src/qz/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class Constants {
public static final String[] PERSIST_PROPS = {"file.whitelist", "file.allow", "networking.hostname", "networking.port", STEAL_WEBSOCKET_PROPERTY };
public static final String AUTOSTART_FILE = ".autostart";
public static final String DATA_DIR = "qz";
public static final int LOG_SIZE = 524288;
public static final int LOG_ROTATIONS = 5;
public static final int DEFAULT_LOG_SIZE = 524288;
public static final int DEFAULT_LOG_ROTATIONS = 5;

public static final int BORDER_PADDING = 10;

Expand Down
11 changes: 11 additions & 0 deletions src/qz/utils/PrefsSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public static String get(Properties app, String name, String defaultVal, boolean
return get(null, app, name, defaultVal, searchSystemProperties);
}

public static int get(Properties app, String name, int defaultVal) {
try {
return Integer.parseInt(get(app, name, "", true));
} catch(NumberFormatException ignore) {}
return defaultVal;
}

public static boolean get(Properties app, String name, boolean defaultVal) {
return Boolean.parseBoolean(get(app, name, "" + defaultVal, true));
}

public static String get(Properties user, Properties app, String name, String defaultVal, String... altNames) {
return get(user, app, name, defaultVal, true, altNames);
}
Expand Down

0 comments on commit 806793e

Please sign in to comment.