aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/helper
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r--src/org/traccar/helper/Log.java189
-rw-r--r--src/org/traccar/helper/LogAction.java19
-rw-r--r--src/org/traccar/helper/PatternUtil.java7
3 files changed, 117 insertions, 98 deletions
diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java
index a22e4bde2..42cd07cef 100644
--- a/src/org/traccar/helper/Log.java
+++ b/src/org/traccar/helper/Log.java
@@ -15,126 +15,137 @@
*/
package org.traccar.helper;
-import org.apache.log4j.Appender;
-import org.apache.log4j.DailyRollingFileAppender;
-import org.apache.log4j.Layout;
-import org.apache.log4j.Level;
-import org.apache.log4j.LogManager;
-import org.apache.log4j.Logger;
-import org.apache.log4j.PatternLayout;
-import org.apache.log4j.varia.NullAppender;
import org.traccar.Config;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
import java.io.IOException;
-import java.lang.management.ManagementFactory;
-import java.lang.management.MemoryMXBean;
-import java.lang.management.OperatingSystemMXBean;
-import java.lang.management.RuntimeMXBean;
-import java.nio.charset.Charset;
+import java.io.Writer;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Formatter;
+import java.util.logging.Handler;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
public final class Log {
private Log() {
}
- public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
-
- private static final String LOGGER_NAME = "traccar";
-
private static final String STACK_PACKAGE = "org.traccar";
private static final int STACK_LIMIT = 3;
- private static Logger logger = null;
-
- public static String getAppVersion() {
- return Log.class.getPackage().getImplementationVersion();
- }
+ private static class RollingFileHandler extends Handler {
- public static void setupLogger(Config config) throws IOException {
+ private String name;
+ private String suffix;
+ private Writer writer;
- Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n");
+ RollingFileHandler(String name) {
+ this.name = name;
+ }
- Appender appender = new DailyRollingFileAppender(
- layout, config.getString("logger.file"), "'.'yyyyMMdd");
+ @Override
+ public synchronized void publish(LogRecord record) {
+ if (isLoggable(record)) {
+ try {
+ String suffix = new SimpleDateFormat("yyyyMMdd").format(new Date(record.getMillis()));
+ if (writer != null && !suffix.equals(this.suffix)) {
+ writer.close();
+ writer = null;
+ new File(name).renameTo(new File(name + "." + this.suffix));
+ }
+ if (writer == null) {
+ this.suffix = suffix;
+ writer = new BufferedWriter(new FileWriter(name));
+ }
+ writer.write(getFormatter().format(record));
+ writer.flush();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
- LogManager.resetConfiguration();
- LogManager.getRootLogger().addAppender(new NullAppender());
+ @Override
+ public void flush() {
+ if (writer != null) {
+ try {
+ writer.flush();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
- logger = Logger.getLogger(LOGGER_NAME);
- logger.addAppender(appender);
- logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL));
+ @Override
+ public void close() throws SecurityException {
+ if (writer != null) {
+ try {
+ writer.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
- Log.logSystemInfo();
- Log.info("Version: " + getAppVersion());
}
- public static Logger getLogger() {
- if (logger == null) {
- logger = Logger.getLogger(LOGGER_NAME);
- logger.setLevel(Level.OFF);
+ public static class LogFormatter extends Formatter {
+
+ private static String formatLevel(Level level) {
+ switch (level.getName()) {
+ case "FINEST":
+ return "TRACE";
+ case "FINER":
+ case "FINE":
+ return "DEBUG";
+ case "INFO":
+ return "INFO";
+ case "WARNING":
+ return "WARN";
+ case "SEVERE":
+ default:
+ return "ERROR";
+ }
}
- return logger;
- }
- public static void logSystemInfo() {
- try {
- OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();
- Log.info("Operating system"
- + " name: " + operatingSystemBean.getName()
- + " version: " + operatingSystemBean.getVersion()
- + " architecture: " + operatingSystemBean.getArch());
-
- RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
- Log.info("Java runtime"
- + " name: " + runtimeBean.getVmName()
- + " vendor: " + runtimeBean.getVmVendor()
- + " version: " + runtimeBean.getVmVersion());
-
- MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
- Log.info("Memory limit"
- + " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb"
- + " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb");
-
- Log.info("Character encoding: "
- + System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset());
-
- } catch (Exception error) {
- Log.warning("Failed to get system info");
- }
- }
+ @Override
+ public String format(LogRecord record) {
+ StringBuilder message = new StringBuilder(record.getMessage());
- public static void error(String msg) {
- getLogger().error(msg);
- }
+ if (record.getThrown() != null) {
+ message.append(" - ").append(exceptionStack(record.getThrown()));
+ }
- public static void warning(String msg) {
- getLogger().warn(msg);
- }
+ return String.format("%1$tF %1$tT %2$5s: %3$s%n",
+ new Date(record.getMillis()), formatLevel(record.getLevel()), message.toString());
+ }
- public static void warning(Throwable exception) {
- warning(null, exception);
}
- public static void warning(String msg, Throwable exception) {
- StringBuilder s = new StringBuilder();
- if (msg != null) {
- s.append(msg);
+ public static void setupLogger(Config config) throws IOException {
+
+ Logger rootLogger = Logger.getLogger("");
+ for (Handler handler : rootLogger.getHandlers()) {
+ rootLogger.removeHandler(handler);
}
- if (exception != null) {
- if (msg != null) {
- s.append(" - ");
- }
- s.append(exceptionStack(exception));
+
+ Handler handler;
+ if (config.getBoolean("logger.console")) {
+ handler = new ConsoleHandler();
+ } else {
+ handler = new RollingFileHandler(config.getString("logger.file"));
}
- getLogger().warn(s.toString());
- }
- public static void info(String msg) {
- getLogger().info(msg);
- }
+ handler.setFormatter(new LogFormatter());
+ handler.setLevel(Level.parse(config.getString("logger.level").toUpperCase()));
- public static void debug(String msg) {
- getLogger().debug(msg);
+ rootLogger.addHandler(handler);
}
public static String exceptionStack(Throwable exception) {
diff --git a/src/org/traccar/helper/LogAction.java b/src/org/traccar/helper/LogAction.java
index 2f575537d..db13337b8 100644
--- a/src/org/traccar/helper/LogAction.java
+++ b/src/org/traccar/helper/LogAction.java
@@ -18,10 +18,14 @@ package org.traccar.helper;
import java.beans.Introspector;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.traccar.model.BaseModel;
public final class LogAction {
+ private static final Logger LOGGER = LoggerFactory.getLogger(LogAction.class);
+
private LogAction() {
}
@@ -71,26 +75,25 @@ public final class LogAction {
}
public static void resetDeviceAccumulators(long userId, long deviceId) {
- log(String.format(PATTERN_DEVICE_ACCUMULATORS, userId, ACTION_DEVICE_ACCUMULATORS, deviceId));
+ LOGGER.info(String.format(
+ PATTERN_DEVICE_ACCUMULATORS, userId, ACTION_DEVICE_ACCUMULATORS, deviceId));
}
private static void logObjectAction(String action, long userId, Class<?> clazz, long objectId) {
- log(String.format(PATTERN_OBJECT, userId, action, Introspector.decapitalize(clazz.getSimpleName()), objectId));
+ LOGGER.info(String.format(
+ PATTERN_OBJECT, userId, action, Introspector.decapitalize(clazz.getSimpleName()), objectId));
}
private static void logLinkAction(String action, long userId,
Class<?> owner, long ownerId, Class<?> property, long propertyId) {
- log(String.format(PATTERN_LINK, userId, action,
+ LOGGER.info(String.format(
+ PATTERN_LINK, userId, action,
Introspector.decapitalize(owner.getSimpleName()), ownerId,
Introspector.decapitalize(property.getSimpleName()), propertyId));
}
private static void logLoginAction(String action, long userId) {
- log(String.format(PATTERN_LOGIN, userId, action));
- }
-
- private static void log(String msg) {
- Log.info(msg);
+ LOGGER.info(String.format(PATTERN_LOGIN, userId, action));
}
}
diff --git a/src/org/traccar/helper/PatternUtil.java b/src/org/traccar/helper/PatternUtil.java
index 1bbb166a6..53eeb0b24 100644
--- a/src/org/traccar/helper/PatternUtil.java
+++ b/src/org/traccar/helper/PatternUtil.java
@@ -15,6 +15,9 @@
*/
package org.traccar.helper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.lang.management.ManagementFactory;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -22,6 +25,8 @@ import java.util.regex.PatternSyntaxException;
public final class PatternUtil {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PatternUtil.class);
+
private PatternUtil() {
}
@@ -66,7 +71,7 @@ public final class PatternUtil {
result.stringTail = input.substring(matcher.group(1).length());
}
} catch (PatternSyntaxException error) {
- Log.warning(error);
+ LOGGER.warn(null, error);
}
}