From: Jan-Pascal van Best Date: Thu, 26 Apr 2012 20:51:19 +0000 (+0200) Subject: More logging work X-Git-Tag: 0.9.4~7 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=d26f9a96977f51f8eb598c6ab5947da0c107f947;p=tv_grab_nl_java More logging work --- diff --git a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java index c92afb7..26cf99e 100644 --- a/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java +++ b/src/main/java/org/vanbest/xmltv/AbstractEPGSource.java @@ -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++; diff --git a/src/main/java/org/vanbest/xmltv/Config.java b/src/main/java/org/vanbest/xmltv/Config.java index 7ed397b..6890ba7 100644 --- a/src/main/java/org/vanbest/xmltv/Config.java +++ b/src/main/java/org/vanbest/xmltv/Config.java @@ -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; diff --git a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java index c48f165..33bd0c2 100644 --- a/src/main/java/org/vanbest/xmltv/ProgrammeCache.java +++ b/src/main/java/org/vanbest/xmltv/ProgrammeCache.java @@ -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 (currentSchemaSCHEMA_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 date0) { - 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); } }