OSDN Git Service

ログ出力の見直し.
[coroid/NicoBrowser.git] / src / nicobrowser / NicoHttpClient.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);