]> www.vanbest.org Git - tv_grab_nl_java/commitdiff
More logging work
authorJan-Pascal van Best <janpascal@vanbest.org>
Thu, 26 Apr 2012 20:51:19 +0000 (22:51 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Thu, 26 Apr 2012 20:51:19 +0000 (22:51 +0200)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Config.java
src/main/java/org/vanbest/xmltv/ProgrammeCache.java

index c92afb708c5478b0f45e34ad16cab462b011b742..26cf99eec8078f3d4b2987fc89f6b19e50601d21 100644 (file)
@@ -9,6 +9,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 
+import org.apache.log4j.Logger;
 import org.vanbest.xmltv.EPGSource.Stats;
 
 public abstract class AbstractEPGSource implements EPGSource {
@@ -17,6 +18,7 @@ public abstract class AbstractEPGSource implements EPGSource {
        protected Config config;
        protected ProgrammeCache cache;
        protected Stats stats = new Stats();
+       static Logger logger = Logger.getLogger(AbstractEPGSource.class);
        
        public static final int MAX_FETCH_TRIES=5;
 
@@ -63,7 +65,7 @@ public abstract class AbstractEPGSource implements EPGSource {
                                done = true;
                        } catch (IOException e) {
                                if (!config.quiet) {
-                                       System.out.println("Error fetching from url " + url + ", count="+count);
+                                       logger.warn("Error fetching from url " + url + ", count="+count);
                                }
                                if (count>=MAX_FETCH_TRIES) { 
                                        stats.fetchErrors++;
index 7ed397be233a2bb3036ba1eb898aaf8248c5115f..6890ba7f9f0654270d0275f03500e38d66597f84 100644 (file)
@@ -34,6 +34,7 @@ import java.util.Map;
 import java.util.Properties;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Logger;
 
 public class Config {
        //constants
@@ -62,6 +63,7 @@ public class Config {
        
        String project_version;
        String build_time;
+       static Logger logger = Logger.getLogger(Config.class);
        
        private Config() {
                Properties configProp = new Properties();
@@ -231,7 +233,7 @@ public class Config {
                                                } else if (value.equals("disabled")) {
                                                        c.setEnabled(false); 
                                                } else {
-                                                       System.out.println("Error in config file, unknown channel status \"" + parts.get(2) + "\", should be enabled or disabled");
+                                                       logger.error("Error in config file, unknown channel status \"" + parts.get(2) + "\", should be enabled or disabled");
                                                }
                                                break;
                                        case 2:
@@ -253,7 +255,7 @@ public class Config {
                                                } else if (value.equals("disabled")) {
                                                        c.setEnabled(false); 
                                                } else {
-                                                       System.out.println("Error in config file, unknown channel status \"" + parts.get(3) + "\", should be enabled or disabled");
+                                                       logger.error("Error in config file, unknown channel status \"" + parts.get(3) + "\", should be enabled or disabled");
                                                }
                                        }
                                        result.channels.add(c);
@@ -263,11 +265,11 @@ public class Config {
                                        try {
                                                fileformat = Integer.parseInt(parts.get(1));
                                        } catch (NumberFormatException e) {
-                                               System.out.println("Unknown config file format " + parts.get(1));
+                                               logger.error("Unknown config file format " + parts.get(1));
                                                fileformat = CURRENT_FILE_FORMAT; // may crash later
                                        }
                                        if (fileformat > CURRENT_FILE_FORMAT) {
-                                               System.out.println("Unknown config file format " + parts.get(1));
+                                               logger.error("Unknown config file format " + parts.get(1));
                                                fileformat = CURRENT_FILE_FORMAT;
                                        }
                                } else if (key.equals("cache-file")) {
@@ -277,7 +279,7 @@ public class Config {
                                                result.cacheDbUser = "SA";
                                                result.cacheDbPassword = "";
                                        } else {
-                                               System.out.println("Illegal key cache-file in config file!");
+                                               logger.warn("Illegal key cache-file in config file with fileformat "+fileformat+"!");
                                        }
                                } else if (key.equals("cache-db-handle")) {
                                        result.cacheDbHandle = parts.get(1);
@@ -288,12 +290,12 @@ public class Config {
                                } else if (key.equals("nice-time-milliseconds")) {
                                        result.niceMilliseconds = Integer.parseInt(parts.get(1));
                                } else {
-                                       System.out.println("Unknown key " + key + " in config file!");
+                                       logger.error("Unknown key " + key + " in config file!");
                                }
                        }
                } catch (IOException e) {
                        e.printStackTrace();
-                       System.out.println("Error reading configuration file, continuing with empty configuration");
+                       logger.warn("Error reading configuration file, continuing with empty configuration");
                        return getDefaultConfig();
                }
                return result;
index c48f16516fc9e0276938ae7aa612c03d22d85aec..33bd0c23e73916eafde2c60c954bdbb85f5014a5 100644 (file)
@@ -37,6 +37,7 @@ import java.util.Map;
 import java.util.Properties;
 
 import org.apache.commons.io.FileUtils;
+import org.apache.log4j.Logger;
 
 public class ProgrammeCache {
        private Connection db;
@@ -50,6 +51,8 @@ public class ProgrammeCache {
        private final static Integer SCHEMA_VERSION=1;
        private final static String SCHEMA_KEY="TV_GRAB_NL_JAVA_SCHEMA_VERSION";
        
+       static Logger logger = Logger.getLogger(ProgrammeCache.class);
+       
        public ProgrammeCache(Config config) {
                this.config = config;
         try {
@@ -64,8 +67,8 @@ public class ProgrammeCache {
                } catch (SQLException e) {
                        db = null;
                        if (!config.quiet) {
-                               System.out.println("Unable to open cache database, proceeding without cache");
-                               if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
+                               logger.warn("Unable to open cache database, proceeding without cache");
+                               logger.debug("Stack trace: ", e);
                        }
         }
         boolean recreateTable = false;
@@ -76,15 +79,15 @@ public class ProgrammeCache {
                                stat.setString(2, SCHEMA_KEY);
                                ResultSet result = stat.executeQuery();
                                if (!result.next()) {
-                                       if (!config.quiet) System.out.println("No schema version found in database");
+                                       logger.debug("No schema version found in database");
                                        recreateTable=true;
                                } else {
                                        Integer currentSchema = (Integer) result.getObject("programme");
                                        if (currentSchema<SCHEMA_VERSION) {
-                                               if (!config.quiet) System.out.println("Current cache database schema version " + currentSchema + " is lower than my version " + SCHEMA_VERSION);
+                                               logger.debug("Current cache database schema version " + currentSchema + " is lower than my version " + SCHEMA_VERSION);
                                                recreateTable = true;
                                        } else if (currentSchema>SCHEMA_VERSION) {
-                                               if (!config.quiet) System.out.println("Got a database schema from the future, since my version is " + SCHEMA_VERSION+ " and yours is " + currentSchema);
+                                               logger.warn("Got a database schema from the future, since my version is " + SCHEMA_VERSION+ " and yours is " + currentSchema);
                                                recreateTable = true;
                                        }
                                        
@@ -92,15 +95,13 @@ public class ProgrammeCache {
                                stat.close();
                        } catch (SQLException e) {
                                if (!config.quiet) {
-                                       System.out.println("Got SQL exception when trying to find current database schema");
-                                       System.out.flush();
-                                       if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
-                                       System.out.flush();
+                                       logger.warn("Got SQL exception when trying to find current database schema");
+                                       logger.debug("Stack trace", e);
                                }
                                recreateTable = true;
                        }
                if (recreateTable) {
-                       if (!config.quiet) System.out.println("Unknown cache schema, removing and recreating cache");
+                       logger.info("Unknown cache schema, removing and recreating cache");
                        try {
                                        Statement stat = db.createStatement();
                                        // System.out.println("Dropping old table");
@@ -120,10 +121,8 @@ public class ProgrammeCache {
                                        stat2.executeUpdate();
                                } catch (SQLException e) {
                                        if (!config.quiet) {
-                                               System.out.println("Unable to create cache database, proceeding without cache");
-                                               System.out.flush();
-                                               if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
-                                               System.out.flush();
+                                               logger.warn("Unable to create cache database, proceeding without cache");
+                                               logger.debug("stack trace: ", e);
                                        }
                                        db = null;
                                }
@@ -137,10 +136,8 @@ public class ProgrammeCache {
                                clearSourceStatement = db.prepareStatement("DELETE FROM cache WHERE source=?");
                        } catch (SQLException e) {
                                if (!config.quiet) {
-                                       System.out.println("Unable to prepare statements, proceeding without cache");
-                                       System.out.flush();
-                                       if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
-                                       System.out.flush();
+                                       logger.warn("Unable to prepare statements, proceeding without cache");
+                                       logger.debug("stack trace: ", e);
                                }
                                db = null;
                }
@@ -192,11 +189,11 @@ public class ProgrammeCache {
                        //System.out.println(putStatement.toString());
                        int count = putStatement.executeUpdate();
                        if (count!=1 && !config.quiet) {
-                               System.out.println("Weird, cache database update statement affected " + count + " rows");
+                               logger.warn("Weird, cache database update statement affected " + count + " rows");
                        }
                } catch (SQLException e) {
-                       System.out.println("Error writing programme ("+source+","+id+") to cache");
-                       if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
+                       logger.warn("Error writing programme ("+source+","+id+") to cache");
+                       logger.debug("stack trace:", e);
                }
        }
 
@@ -207,12 +204,12 @@ public class ProgrammeCache {
                        stat = db.createStatement();
                        int count = stat.executeUpdate("DELETE FROM cache WHERE date<CURRENT_DATE - 3 DAY");
                        if (!config.quiet && count>0) {
-                               System.out.println("Purged " + count + " old entries from cache");
+                               logger.info("Purged " + count + " old entries from cache");
                        }
                        stat.close();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       logger.debug("stack trace:", e);
                }
        }