代码之家  ›  专栏  ›  技术社区  ›  alex2k8

如何在Android上为apachecommons HttpClient启用日志记录

  •  30
  • alex2k8  · 技术社区  · 14 年前

    要在我使用的普通Java应用程序中为apache commons HttpClient启用日志记录,请执行以下操作:

    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
    

    4 回复  |  直到 12 年前
        1
  •  63
  •   blahdiblah    11 年前

    忽略我之前的评论。我在org.apache.http日志页面上找到了解决方案。你最初的回答是指 httpclient-3.x logging http-components logging

    java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
    java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
    
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
    System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
    System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.headers", "debug");
    

    和属性:

    adb shell setprop log.tag.org.apache.http VERBOSE
    adb shell setprop log.tag.org.apache.http.wire VERBOSE
    adb shell setprop log.tag.org.apache.http.headers VERBOSE
    

    区别在于日志标记名。

        2
  •  8
  •   alex2k8    14 年前

    adb shell setprop log.tag.httpclient.wire.header VERBOSE
    adb shell setprop log.tag.httpclient.wire.content VERBOSE
    

    代码:

    java.util.logging.Logger.getLogger("httpclient.wire.header").setLevel(java.util.logging.Level.FINEST);
    java.util.logging.Logger.getLogger("httpclient.wire.content").setLevel(java.util.logging.Level.FINEST);
    

    测试:

    java.util.logging.Logger.getLogger("httpclient.wire.content").log(java.util.logging.Level.CONFIG, "hola");
    
        3
  •  3
  •   Michael    12 年前

    细节才是关键。我正在运行2.3.3仿真器,并使其可以使用:

    java.util.logging.Logger.getLogger("org.apache.http.wire").setLevel(java.util.logging.Level.FINEST);
    java.util.logging.Logger.getLogger("org.apache.http.headers").setLevel(java.util.logging.Level.FINEST);
    

    # setprop log.tag.org.apache.http.wire VERBOSE
    # setprop log.tag.org.apache.http.headers VERBOSE
    

    因此,日志说明符似乎是不同的。

        4
  •  3
  •   nut    12 年前

    java.util.logging.Logger.getLogger(yourFullClassName).setLevel(java.util.logging.Level.All);
    

    adb shell setprop log.tag.correspondingTag VERBOSE
    

    Android使用此函数从类全名获取相应的标记:

    public static String loggerNameToTag(String loggerName)
      {
        if (loggerName == null) {
          return "null";
        }
    
        int length = loggerName.length();
        if (length <= 23) {
          return loggerName;
        }
    
        int lastPeriod = loggerName.lastIndexOf(".");
        return length - (lastPeriod + 1) <= 23 ? loggerName.substring(lastPeriod + 1) : loggerName.substring(loggerName.length() - 23);
      }
    

    String clzName = "org.apache.http.impl.client.DefaultRequestDirector";
    String newClzName = loggerNameToTag(clzName);
    System.out.println("className:" + clzName + " tagName is " + newClzName);    //get tagName from class full name,and then it will be used in setprop
    Logger jdkLogger = Logger.getLogger(clzName);
    jdkLogger.setLevel(Level.ALL);
    if (jdkLogger.isLoggable(Level.FINE))
    {
            jdkLogger.log(Level.FINE, "jdk log msg");    
            jdkLogger.log(Level.Fine,"tagName is")
    }
    

    然后在壳里

    setprop log.tag.DefaultRequestDirector VERBOSE