From 49d57451db0b9860f40b2ba53bc96069bb86261f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 24 Oct 2018 08:50:43 +0400 Subject: Default logging on config errors --- src/org/traccar/Context.java | 9 +++++++-- src/org/traccar/helper/Log.java | 25 +++++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 51c67bd0a..5b22120d7 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -359,8 +359,13 @@ public final class Context { public static void init(String configFile) throws Exception { - config = new Config(); - config.load(configFile); + try { + config = new Config(); + config.load(configFile); + } catch (Exception e) { + Log.setupDefaultLogger(); + throw e; + } if (config.getBoolean("logger.enable")) { Log.setupLogger(config); diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java index 4db2ec6f0..a26da6b30 100644 --- a/src/org/traccar/helper/Log.java +++ b/src/org/traccar/helper/Log.java @@ -151,7 +151,24 @@ public final class Log { } + public static void setupDefaultLogger() { + File jarPath = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath()); + File logsPath = new File(jarPath, "logs"); + if (!logsPath.exists() || !logsPath.isDirectory()) { + logsPath = jarPath; + } + setupLogger(false, new File(logsPath, "tracker-server.log").getPath(), Level.WARNING.getName(), false); + } + public static void setupLogger(Config config) { + setupLogger( + config.getBoolean("logger.console"), + config.getString("logger.file"), + config.getString("logger.level"), + config.getBoolean("logger.fullStackTraces")); + } + + private static void setupLogger(boolean console, String file, String levelString, boolean fullStackTraces) { Logger rootLogger = Logger.getLogger(""); for (Handler handler : rootLogger.getHandlers()) { @@ -159,15 +176,15 @@ public final class Log { } Handler handler; - if (config.getBoolean("logger.console")) { + if (console) { handler = new ConsoleHandler(); } else { - handler = new RollingFileHandler(config.getString("logger.file")); + handler = new RollingFileHandler(file); } - handler.setFormatter(new LogFormatter(config.getBoolean("logger.fullStackTraces"))); + handler.setFormatter(new LogFormatter(fullStackTraces)); - Level level = Level.parse(config.getString("logger.level").toUpperCase()); + Level level = Level.parse(levelString.toUpperCase()); rootLogger.setLevel(level); handler.setLevel(level); handler.setFilter(record -> record != null && !record.getLoggerName().startsWith("sun")); -- cgit v1.2.3