From 110c49504f87c025423c923500bd52ea4615b33b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 30 Jul 2013 20:42:02 +1200 Subject: Improve logging system (fix #278) --- pom.xml | 5 ++ setup/package.sh | 3 +- setup/windows/traccar.iss | 1 + src/org/traccar/BasePipelineFactory.java | 4 +- src/org/traccar/ServerManager.java | 58 +------------- src/org/traccar/TrackerEventHandler.java | 22 ++---- src/org/traccar/geocode/GoogleReverseGeocoder.java | 2 +- src/org/traccar/helper/Log.java | 90 +++++++++++++++------- .../traccar/helper/NamedParameterStatement.java | 2 +- src/org/traccar/http/WebServer.java | 6 +- src/org/traccar/protocol/St210ProtocolDecoder.java | 4 +- 11 files changed, 88 insertions(+), 109 deletions(-) diff --git a/pom.xml b/pom.xml index 061b89a75..1563f300f 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,11 @@ junit 4.11 + + log4j + log4j + 1.2.17 + com.h2database h2 diff --git a/setup/package.sh b/setup/package.sh index c79c5d710..8d3c87fc0 100755 --- a/setup/package.sh +++ b/setup/package.sh @@ -107,6 +107,7 @@ sed -i 's//org.traccar.Main/g' out/conf/wrapper.conf sed -i 's/@app.name@/traccar/g' out/conf/wrapper.conf sed -i 's/@app.long.name@/traccar/g' out/conf/wrapper.conf sed -i 's/@app.description@/traccar/g' out/conf/wrapper.conf +sed -i 's/wrapper.logfile=..\/logs\/wrapper.log/wrapper.logfile=..\/logs\/wrapper.log.YYYYMMDD\nwrapper.logfile.rollmode=DATE/g' out/conf/wrapper.conf # linux 32 @@ -152,7 +153,7 @@ makeself out traccar.run "traccar" "mkdir -p $app; cp -rf * $app; $app/bin/tracc zip -j traccar-macosx-64.zip traccar.run macosx/README.txt rm traccar.run -rm -rf out +#rm -rf out # GENERAL CLEANUP diff --git a/setup/windows/traccar.iss b/setup/windows/traccar.iss index bd69352db..262a8e51e 100644 --- a/setup/windows/traccar.iss +++ b/setup/windows/traccar.iss @@ -56,6 +56,7 @@ begin StringChangeEx(S, '@app.name@', 'Traccar', true); StringChangeEx(S, '@app.long.name@', 'Traccar', true); StringChangeEx(S, '@app.description@', 'Traccar', true); + StringChangeEx(S, 'wrapper.logfile=../logs/wrapper.log', 'wrapper.logfile=../logs/wrapper.log.YYYYMMDD' + #13#10 + 'wrapper.logfile.rollmode=DATE', true); SaveStringToFile(ExpandConstant(CurrentFileName), S, false); end; diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index e44f67205..e5aa5f76b 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -75,10 +75,10 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { msg.append(")"); } - Log.fine(msg.toString()); + Log.debug(msg.toString()); } else if (e instanceof ExceptionEvent) { ExceptionEvent event = (ExceptionEvent) e; - Log.warning(event.getCause().toString()); + Log.warning(event.getCause()); } } diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java index 61bf55616..a32a49572 100644 --- a/src/org/traccar/ServerManager.java +++ b/src/org/traccar/ServerManager.java @@ -89,9 +89,6 @@ public class ServerManager { return properties; } - /** - * Initialize - */ public void init(String[] arguments) throws IOException, ClassNotFoundException, SQLException { @@ -103,7 +100,7 @@ public class ServerManager { dataManager = new DatabaseDataManager(properties); - initLogger(properties); + Log.setupLogger(properties); initGeocoder(properties); initXexunServer("xexun"); @@ -159,9 +156,6 @@ public class ServerManager { } } - /** - * Start - */ public void start() { if (webServer != null) { webServer.start(); @@ -171,9 +165,6 @@ public class ServerManager { } } - /** - * Stop - */ public void stop() { for (Object server: serverList) { ((TrackerServer) server).stop(); @@ -188,57 +179,10 @@ public class ServerManager { } } - /** - * Destroy - */ public void destroy() { serverList.clear(); } - /** - * Initialize logger - */ - private void initLogger(Properties properties) throws IOException { - - loggerEnabled = Boolean.valueOf(properties.getProperty("logger.enable")); - - if (loggerEnabled) { - - String fileName = properties.getProperty("logger.file"); - if (fileName != null) { - - FileHandler file = new FileHandler(fileName, true); - - // Simple formatter - file.setFormatter(new Formatter() { - private final String LINE_SEPARATOR = - System.getProperty("line.separator", "\n"); - - private final DateFormat dateFormat = - new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - @Override - public String format(LogRecord record) { - StringBuffer line = new StringBuffer(); - dateFormat.format(new Date(record.getMillis()), line, new FieldPosition(0)); - line.append(" "); - line.append(record.getSourceClassName()); - line.append("."); - line.append(record.getSourceMethodName()); - line.append(" "); - line.append(record.getLevel().getName()); - line.append(": "); - line.append(formatMessage(record)); - line.append(LINE_SEPARATOR); - return line.toString(); - } - }); - - Log.getLogger().addHandler(file); - } - } - } - private void initGeocoder(Properties properties) throws IOException { if (Boolean.parseBoolean(properties.getProperty("geocoder.enable"))) { reverseGeocoder = new GoogleReverseGeocoder(); diff --git a/src/org/traccar/TrackerEventHandler.java b/src/org/traccar/TrackerEventHandler.java index 538e71cdf..6deeaad9b 100644 --- a/src/org/traccar/TrackerEventHandler.java +++ b/src/org/traccar/TrackerEventHandler.java @@ -41,19 +41,14 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler { private void processSinglePosition(Position position) { if (position == null) { - Log.info("null message"); + Log.info("processSinglePosition null message"); } else { - Log.info( - "id: " + position.getId() + - ", deviceId: " + position.getDeviceId() + - ", valid: " + position.getValid() + - ", time: " + position.getTime() + - ", latitude: " + position.getLatitude() + - ", longitude: " + position.getLongitude() + - ", altitude: " + position.getAltitude() + - ", speed: " + position.getSpeed() + - ", course: " + position.getCourse() + - ", power: " + position.getPower()); + StringBuilder s = new StringBuilder(); + s.append("device: ").append(position.getDeviceId()).append(", "); + s.append("time: ").append(position.getTime()).append(", "); + s.append("lat: ").append(position.getLatitude()).append(", "); + s.append("lon: ").append(position.getLongitude()); + Log.info(s.toString()); } // Write position to database @@ -63,8 +58,7 @@ public class TrackerEventHandler extends IdleStateAwareChannelHandler { dataManager.updateLatestPosition(position.getDeviceId(), id); } } catch (Exception error) { - Log.info("Exception during query execution"); - Log.warning(error.getMessage()); + Log.warning(error); } } diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java index a25f60d66..1e61eb6ee 100644 --- a/src/org/traccar/geocode/GoogleReverseGeocoder.java +++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java @@ -55,7 +55,7 @@ public class GoogleReverseGeocoder implements ReverseGeocoder { reader.close(); } catch(Exception error) { - Log.warning(error.getMessage()); + Log.warning(error); } return null; diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java index 983171b72..4cd61b933 100644 --- a/src/org/traccar/helper/Log.java +++ b/src/org/traccar/helper/Log.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,53 +15,87 @@ */ package org.traccar.helper; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.io.IOException; +import java.util.Properties; +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; -/** - * Logger - */ public class Log { - private static final String TRACCAR_LOGGER_NAME = "traccar"; - private static Logger logger = null; + + public static void setupLogger(Properties properties) throws IOException { + if (Boolean.valueOf(properties.getProperty("logger.enable"))) { + + Layout layout = new PatternLayout( + "%d{yyyy-MM-dd HH:mm:ss} %5p: %m%n"); + + Appender appender = new DailyRollingFileAppender( + layout, properties.getProperty("logger.file"), "'.'yyyyMMdd"); + + LogManager.resetConfiguration(); + logger = Logger.getRootLogger(); + logger.addAppender(appender); + logger.setLevel(Level.DEBUG); + } + } - /** - * Return global logger - */ public static Logger getLogger() { if (logger == null) { - logger = Logger.getLogger(TRACCAR_LOGGER_NAME); - logger.setUseParentHandlers(false); - logger.setLevel(Level.ALL); + logger = Logger.getRootLogger(); + logger.setLevel(Level.OFF); } return logger; } - private static void write(Level level, String msg) { - StackTraceElement[] stack = Thread.currentThread().getStackTrace(); - if (stack != null && stack.length > 3) { - getLogger().logp(level, stack[3].getClassName(), stack[3].getMethodName(), msg); - } else { - getLogger().log(level, msg); - } + public static void error(String msg) { + getLogger().error(msg); } - public static void severe(String msg) { - write(Level.SEVERE, msg); + public static void warning(String msg) { + getLogger().warn(msg); } - public static void warning(String msg) { - write(Level.WARNING, msg); + 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); + s.append(" - "); + } + if (exception != null) { + String exceptionMsg = exception.getMessage(); + if (exceptionMsg != null) { + s.append(exceptionMsg); + s.append(" - "); + } + s.append(exception.getClass().getName()); + StackTraceElement[] stack = exception.getStackTrace(); + if (stack.length > 0) { + s.append(" ("); + s.append(stack[0].getFileName()); + s.append(":"); + s.append(stack[0].getLineNumber()); + s.append(")"); + } + } + getLogger().warn(s.toString()); } public static void info(String msg) { - write(Level.INFO, msg); + getLogger().info(msg); } - public static void fine(String msg) { - write(Level.FINE, msg); + public static void debug(String msg) { + getLogger().debug(msg); } } diff --git a/src/org/traccar/helper/NamedParameterStatement.java b/src/org/traccar/helper/NamedParameterStatement.java index eb5c411f2..833f4aea3 100644 --- a/src/org/traccar/helper/NamedParameterStatement.java +++ b/src/org/traccar/helper/NamedParameterStatement.java @@ -132,7 +132,7 @@ public class NamedParameterStatement { try { reset(true); } catch (SQLException secondError) { - Log.warning("Exception during query preparation: " + secondError.getMessage()); + Log.warning(secondError); throw secondError; } } diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java index 59c212d01..4afd4ed7a 100644 --- a/src/org/traccar/http/WebServer.java +++ b/src/org/traccar/http/WebServer.java @@ -61,7 +61,7 @@ public class WebServer { context.bind("java:/DefaultDS", ds); } catch (Exception error) { - Log.warning(error.getMessage()); + Log.warning(error); } } @@ -86,7 +86,7 @@ public class WebServer { try { server.start(); } catch (Exception error) { - Log.warning(error.getMessage()); + Log.warning(error); } } @@ -94,7 +94,7 @@ public class WebServer { try { server.stop(); } catch (Exception error) { - Log.warning(error.getMessage()); + Log.warning(error); } } diff --git a/src/org/traccar/protocol/St210ProtocolDecoder.java b/src/org/traccar/protocol/St210ProtocolDecoder.java index 432c9c1fa..797ed9eb0 100644 --- a/src/org/traccar/protocol/St210ProtocolDecoder.java +++ b/src/org/traccar/protocol/St210ProtocolDecoder.java @@ -615,10 +615,10 @@ public class St210ProtocolDecoder extends BaseProtocolDecoder { try{ position = decodeMsg(sentence); - Log.info("MESSAGE DECODED WITH SUCCESS!"); + Log.info("Message decoded with success"); } catch(Exception e){ - Log.severe("ERROR WHILE DECODING MESSAGE: " + e.getMessage()); + Log.warning(e); } return position; -- cgit v1.2.3