OSDN Git Service

ログ出力の見直し.
authoryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 16 May 2009 04:10:24 +0000 (04:10 +0000)
committeryuki <yuki@bdf3b611-c98c-6041-8292-703d9c9adbe7>
Sat, 16 May 2009 04:10:24 +0000 (04:10 +0000)
commons-loggingを私用するように変更.

git-svn-id: http://192.168.11.7/svn/repository/NicoBrowserBranches/release_20090323/NicoBrowser@109 bdf3b611-c98c-6041-8292-703d9c9adbe7

src/nicobrowser/NicoHttpClient.java
src/nicobrowser/main/Main.java

index 03b5a32..076d686 100644 (file)
@@ -24,8 +24,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.List;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import javax.swing.text.MutableAttributeSet;
 import javax.swing.text.html.HTML;
 import javax.swing.text.html.HTMLEditorKit;
@@ -36,6 +34,8 @@ import javax.xml.parsers.ParserConfigurationException;
 import nicobrowser.entity.NicoContent.Status;
 import nicobrowser.util.Result;
 import nicobrowser.util.Util;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpResponse;
@@ -60,6 +60,7 @@ import org.xml.sax.SAXException;
  */
 public class NicoHttpClient extends DefaultHttpClient {
 
+    private static Log log = LogFactory.getLog(NicoHttpClient.class);
     static NicoHttpClient instance;
     private static final String LOGIN_PAGE =
             "https://secure.nicovideo.jp/secure/login?site=niconico";
@@ -107,8 +108,7 @@ public class NicoHttpClient extends DefaultHttpClient {
 
             //post.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
             HttpResponse response = execute(post);
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "ログインステータスコード: " + response.getStatusLine().
-                    getStatusCode());
+            log.debug("ログインステータスコード: " + response.getStatusLine().getStatusCode());
 
             // ログイン可否の判定.
             HttpEntity entity = response.getEntity();
@@ -118,7 +118,7 @@ public class NicoHttpClient extends DefaultHttpClient {
                 auth = true;
             }
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("ログイン時に問題が発生", ex);
         }
         return auth;
     }
@@ -132,15 +132,14 @@ public class NicoHttpClient extends DefaultHttpClient {
         HttpGet method = new HttpGet(LOGOUT_PAGE);
         try {
             HttpResponse response = execute(method);
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "ログアウトステータスコード: " + response.getStatusLine().
-                    getStatusCode());
+            log.debug("ログアウトステータスコード: " + response.getStatusLine().getStatusCode());
 
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                 result = true;
             }
             response.getEntity().consumeContent();
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("ログアウト時に問題が発生", ex);
         }
         return result;
     }
@@ -151,7 +150,7 @@ public class NicoHttpClient extends DefaultHttpClient {
      * @return 検索結果.
      */
     public List<NicoContent> search(String word) {
-        Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "検索:" + word);
+        log.debug("検索:" + word);
 
         InputStream is = null;
         List<NicoContent> conts = new ArrayList<NicoContent>();
@@ -177,7 +176,7 @@ public class NicoHttpClient extends DefaultHttpClient {
                 is.close();
             }
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("検索結果処理時に例外発生", ex);
         }
         return conts;
     }
@@ -189,7 +188,7 @@ public class NicoHttpClient extends DefaultHttpClient {
     public List<NicoContent> loadMyListDaily() throws URISyntaxException, HttpException, InterruptedException {
         List<NicoContent> list = new ArrayList<NicoContent>();
         String url = new String("http://www.nicovideo.jp/ranking/mylist/daily/all?rss=atom");
-        Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "全動画サイトのマイリスト登録数ランキング(本日)[全体] : " + url);
+        log.debug("全動画サイトのマイリスト登録数ランキング(本日)[全体] : " + url);
 
         HttpGet get = new HttpGet(url);
 
@@ -202,15 +201,15 @@ public class NicoHttpClient extends DefaultHttpClient {
             list = getNicoContents(reader);
             response.getEntity().consumeContent();
         } catch (FeedException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException ex) {
-                    Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+                    log.fatal("", ex);
                 }
             }
         }
@@ -227,7 +226,7 @@ public class NicoHttpClient extends DefaultHttpClient {
     public List<NicoContent> loadMyList(String listNo) throws URISyntaxException, HttpException, InterruptedException {
         List<NicoContent> contList = new ArrayList<NicoContent>();
         String url = new String(MY_LIST_PAGE_HEADER + listNo + "?rss=atom");
-        Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "マイリストURL: " + url);
+        log.debug("マイリストURL: " + url);
 
         HttpGet get = new HttpGet(url);
 
@@ -237,15 +236,15 @@ public class NicoHttpClient extends DefaultHttpClient {
             reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
             contList = getNicoContents(reader);
         } catch (FeedException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, "FeedExceitpion" + listNo);
+            log.fatal("FeedExceitpion" + listNo, ex);
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException ex) {
-                    Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+                    log.fatal("", ex);
                 }
             }
         }
@@ -262,7 +261,7 @@ public class NicoHttpClient extends DefaultHttpClient {
         InputStream re = null;
         List<SyndEntryImpl> list = null;
         String url = new String(MOVIE_THUMBNAIL_PAGE_HEADER + movieNo);
-        Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "動画サムネイルURL: " + url);
+        log.debug("動画サムネイルURL: " + url);
 
         HttpGet get;
 
@@ -280,7 +279,7 @@ public class NicoHttpClient extends DefaultHttpClient {
             Element root = doc.getDocumentElement();
 
             if ("fail".equals(root.getAttribute("status"))) {
-                Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "情報取得できません: " + movieNo);
+                log.warn("情報取得できません: " + movieNo);
                 return null;
             }
 
@@ -302,18 +301,18 @@ public class NicoHttpClient extends DefaultHttpClient {
 //        } catch (ParseException ex) {
 //            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
         } catch (SAXException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } catch (IOException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } catch (ParserConfigurationException ex) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+            log.fatal("", ex);
         } finally {
             try {
                 if (re != null) {
                     re.close();
                 }
             } catch (IOException ex) {
-                Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+                log.fatal("", ex);
             }
         }
         return cont;
@@ -385,7 +384,7 @@ public class NicoHttpClient extends DefaultHttpClient {
                 Enumeration e = a.getAttributeNames();
                 while (e.hasMoreElements()) {
                     Object key = e.nextElement();
-                    System.out.println("---- " + key.toString() + " : " + a.getAttribute(key));
+                    log.debug("---- " + key.toString() + " : " + a.getAttribute(key));
                 }
             }
 
@@ -416,7 +415,7 @@ public class NicoHttpClient extends DefaultHttpClient {
                     Reader reader = new StringReader(sc.getValue());
                     new ParserDelegator().parse(reader, callBack, true);
                 } catch (IOException ex) {
-                    Logger.getLogger(NicoHttpClient.class.getName()).log(Level.SEVERE, null, ex);
+                    log.fatal("RSSの読み込み失敗: " + content.getTitle());
                 }
             }
 
@@ -461,10 +460,10 @@ public class NicoHttpClient extends DefaultHttpClient {
         String[] urls = resultString.split("&");
         final String marker = "url=";
         for (String url : urls) {
-            System.out.println(url);
             if (url.contains(marker)) {
                 String result = url.substring(marker.length());
                 result = URLDecoder.decode(result, "UTF-8");
+
                 return new URL(result);
             }
         }
@@ -490,16 +489,15 @@ public class NicoHttpClient extends DefaultHttpClient {
 
         URL url = getFlvUrl(videoID);
         if (nowStatus == Status.GET_LOW && url.toString().contains("low")) {
-            Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "lowファイル取得済みのためスキップ" + videoID + ":" +
-                    fileName);
+            log.info("lowファイル取得済みのためスキップ" + videoID + ":" + fileName);
             return nowStatus;
         }
 
         get = new HttpGet(url.toURI());
         response = execute(get);
         String contentType = response.getEntity().getContentType().getValue();
-        System.out.println(contentType);
-        System.out.println(fileName);
+        log.debug(contentType);
+        log.debug(fileName);
         if ("text/plain".equals(contentType) || "text/html".equals(contentType)) {
             return Status.GET_INFO;
         }
@@ -518,7 +516,7 @@ public class NicoHttpClient extends DefaultHttpClient {
 //            postfix++;
 //            file = new File(fileName + "(" + postfix + ")" + "." + ext);
 //        }
-        Logger.getLogger(NicoHttpClient.class.getName()).log(Level.INFO, "保存します:" + file.getPath());
+        log.info("保存します:" + file.getPath());
         FileOutputStream fos = new FileOutputStream(file);
         BufferedOutputStream out = new BufferedOutputStream(fos);
 
index c12a527..2c6fe46 100644 (file)
@@ -1,11 +1,6 @@
 /*$Id$*/
 package nicobrowser.main;
 
-import java.io.IOException;
-import java.net.URISyntaxException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import nicobrowser.*;
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -17,24 +12,30 @@ import javax.persistence.EntityManagerFactory;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Persistence;
 import javax.persistence.Query;
+import nicobrowser.Config;
+import nicobrowser.NicoHttpClient;
 import nicobrowser.entity.NicoContent;
 import nicobrowser.entity.NicoContent.Status;
-import org.apache.http.HttpException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 public class Main {
 
+    private static Log log = LogFactory.getLog(Main.class);
+
     public static void main(String[] args) {
-        System.out.println("Start");
         new Main();
     }
 
     public Main() {
+        log.info("program start");
+
         Config config = Config.getInstance();
         List<NicoContent> dailyList = null;
         ArrayList<List<NicoContent>> myLists = new ArrayList<List<NicoContent>>();
         NicoHttpClient instance = null;
         try {
-            System.out.println("リストの取得");
+            log.info("リストを取得します");
             instance = NicoHttpClient.getInstance();
             //list = instance.loadMyList("4315046");
             dailyList = instance.loadMyListDaily();
@@ -48,7 +49,8 @@ public class Main {
             return;
         }
 
-        System.out.println("DBとの突合せ");
+        log.info("今回取得したデータを過去の取得データと比較します");
+
         EntityManagerFactory factory;
         EntityManager manager;
 
@@ -94,29 +96,30 @@ public class Main {
                     long sleep = nowDate.getTime() - prevDate.getTime();
                     sleep = 5000 - sleep;
                     if (sleep > 0) {
-                        System.out.print("" + sleep + "ms sleep");
+                        log.info("" + sleep + "ms sleep");
                         Thread.sleep(sleep);
                     }
                 }
                 prevDate = Calendar.getInstance().getTime();
-                System.out.print("FLVファイル取得: " + config.getSrcSaveDir() + File.separator + c.getFileName());
+                File saveDir = new File(config.getSrcSaveDir(), c.getFileName());
+                log.info("ファイルを取得します: " + saveDir.getCanonicalPath());
                 Status status;
                 try {
                     status = instance.getFlvFile(c.getNicoId(),
-                            config.getSrcSaveDir() + File.separator + c.getFileName(), c.getStatus(),
+                            saveDir.getCanonicalPath(), c.getStatus(),
                             config.isExtMp4Use());
                     c.setStatus(status);
                     if (status == Status.GET_INFO) {
                         c.setFailTimes(c.getFailTimes() + 1);
                     }
                 } catch (Exception ex) {
-                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                     c.setFailTimes(c.getFailTimes() + 1);
+                    log.fatal("ファイル取得に失敗しました。" + c.getNicoId() + ", 通算失敗回数: " + c.getFailTimes(), ex);
                 }
                 transaction.begin();
                 manager.persist(c);
                 transaction.commit();
-                System.out.println("...完了");
+                log.info("完了しました");
             }
         } catch (Exception ex) {
             ex.printStackTrace();
@@ -133,7 +136,7 @@ public class Main {
                 setParameter(1, c.getNicoId());
         List<NicoContent> resList = query.getResultList();
         if (resList.isEmpty()) {
-            System.out.println("NEW! " + c.getNicoId() + " : " + c.getFileName());
+            log.info("NEW! " + c.getNicoId() + " : " + c.getFileName());
             manager.persist(c);
         }
     }