Skip to content

Commit

Permalink
Make logs configurable (#1163)
Browse files Browse the repository at this point in the history
* Configurable logs
* Consolidate preferences
  • Loading branch information
tresf authored Aug 4, 2023
1 parent 79e4d71 commit 4e3cbe7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 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, Constants.PREFS_LOG_DISABLE, false)) {
return;
}

int logSize = PrefsSearch.get(app, Constants.PREFS_LOG_SIZE, Constants.DEFAULT_LOG_SIZE);
int logRotate = PrefsSearch.get(app, Constants.PREFS_LOG_ROTATE, 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(logRotate)).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");
}
}
6 changes: 2 additions & 4 deletions src/qz/auth/Certificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class Certificate {

private static final Logger log = LogManager.getLogger(Certificate.class);
private static final String QUIETLY_FAIL = "quiet";
public static final String OVERRIDE_CA_FLAG = "trustedRootCert";
public static final String OVERRIDE_CA_PROPERTY = "authcert.override";

public enum Algorithm {
SHA1("SHA1withRSA"),
Expand Down Expand Up @@ -142,13 +140,13 @@ public enum Algorithm {
public static void scanAdditionalCAs() {
ArrayList<Map.Entry<Path, String>> certPaths = new ArrayList<>();
// First, look for "-DtrustedRootCert" command line property
certPaths.addAll(FileUtilities.parseDelimitedPaths(System.getProperty(OVERRIDE_CA_FLAG)));
certPaths.addAll(FileUtilities.parseDelimitedPaths(System.getProperty(Constants.PREFS_OVERRIDE_CA_CLI)));

// Second, look for "override.crt" within App directory
certPaths.add(new AbstractMap.SimpleEntry<>(SystemUtilities.getJarParentPath().resolve(Constants.OVERRIDE_CERT), QUIETLY_FAIL));

// Third, look for "authcert.override" property in qz-tray.properties
certPaths.addAll(FileUtilities.parseDelimitedPaths(App.getTrayProperties(), OVERRIDE_CA_PROPERTY));
certPaths.addAll(FileUtilities.parseDelimitedPaths(App.getTrayProperties(), Constants.PREFS_OVERRIDE_CA));

for(Map.Entry<Path, String> certPath : certPaths) {
if(certPath.getKey() != null) {
Expand Down
13 changes: 11 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 Expand Up @@ -69,6 +69,15 @@ public class Constants {
public static final String PREFS_FILEIO_ENABLED = "security.file.enabled";
public static final String PREFS_FILEIO_STRICT = "security.file.strict";

public static final String PREFS_LOG_DISABLE = "log.disable";
public static final String PREFS_LOG_ROTATE = "log.rotate";
public static final String PREFS_LOG_SIZE = "log.size";

public static final String PREFS_OVERRIDE_CA = "authcert.override";
public static final String PREFS_OVERRIDE_CA_CLI = "trustedRootCert";

public static final String PREFS_PRINTER_JOB_DATA = "printer.status.jobdata";

public static final String ALLOW_SITES_TEXT = "Permanently allowed \"%s\" to access local resources";
public static final String BLOCK_SITES_TEXT = "Permanently blocked \"%s\" from accessing local resources";

Expand Down
3 changes: 2 additions & 1 deletion src/qz/printer/status/StatusSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.websocket.api.Session;
import qz.App;
import qz.common.Constants;
import qz.printer.status.job.WmiJobStatusMap;
import qz.utils.*;
import qz.ws.PrintSocketClient;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void enableJobDataOnPrinter(String printer, int maxJobData, PrintingUtili
if (!SystemUtilities.isWindows()) {
throw new UnsupportedOperationException("Job data listeners are only supported on Windows");
}
String spoolFileMonitoring = PrefsSearch.get(App.getTrayProperties(), "printer.status.jobdata", "false", false );
String spoolFileMonitoring = PrefsSearch.get(App.getTrayProperties(), Constants.PREFS_PRINTER_JOB_DATA, "false", false );
if (!Boolean.parseBoolean(spoolFileMonitoring)) {
throw new UnsupportedOperationException("Job data listeners are currently disabled");
}
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 4e3cbe7

Please sign in to comment.