【问题标题】:Parsing Jetty log records解析 Jetty 日志记录
【发布时间】:2011-04-20 11:15:44
【问题描述】:

对于给定的输入示例:

70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] "GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1" 302 0 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)" 4 4

我想定义如下解析逻辑(可能是正则表达式)

  1. 提取 IP(3 位,点)* 4 => 70.80.110.200
  2. 提取日期 => 2011 年 4 月 12 日
  3. 提取时间 => 05:47:34
  4. 提取 URI(以 \" 开头并以 \" 结尾)。 => /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000

【问题讨论】:

    标签: java regex parsing logging


    【解决方案1】:

    尝试:

    /^([0-9.]+).*?\[(\d+\/\w+\/\d+):(\d+:\d+:\d+).*?\].*?(\/[^ ]*).*$/
    

    如您所料,在以下组(1、2、3、4)中,您将获得您指定的所有数据 - 例如.group(3) 是时间。

    【讨论】:

    • 抱歉,没有 - 使用 google/book 查找如何在 Java 中使用正则表达式。
    • 不是 100% 正确 - 固定版本:r = /^([0-9.]*).*?[(\d+\/\w+\/\d+):(\d+: \d+:\d+).*?].*?(\/[^ ]*).*$/ (原始版本只匹配一位数字/句号而不是整个组,并且它不会'逃避正方形括号)
    • @Frank Schmitt - 我已经在 23 分钟前修复了它。 ;)
    • @hsz,但他从中学到了什么吗?
    • 我为他提供了一个他期望的正则表达式。对不起,我懒得给懒人做作业。
    【解决方案2】:

    确保 Jetty 配置为执行 NSCA 兼容的日志记录,然后您可以使用任何 NCSA 日志分析器来分析日志。

    如果您想手动完成,那么这是正则表达式的一个很好的用例。

    【讨论】:

      【解决方案3】:

      完整的代码示例(基于hsz's answer):

      import java.util.*;
      import java.util.regex.*;
      
      public class RegexDemo {
      
        public static void main( String[] argv ) {
          String pat = "^([0-9.]*).*?\\[(\\d+\\/\\w+\\/\\d+):(\\d+:\\d+:\\d+).*?\\].*?(\\/[^ ]*).*$";
          Pattern p = Pattern.compile(pat);
          String target = "70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] \"GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1\" 302 0 \"-\" \"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)\" 4 4";
          Matcher m = p.matcher(target);
          System.out.println("pattern: " + pat);
          System.out.println("target: " + target);
      
          if (m.matches()) {
            System.out.println("found");
            for (int i=0; i <= m.groupCount(); ++i) {
              System.out.println(m.group(i));
            }
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        您可以尝试以下方法:

        String s = "70.80.110.200 -  -  [12/Apr/2011:05:47:34 +0000] \"GET /notify/click?r=http://www.xxxxxx.com/hello_world&rt=1302587231462&iid=00000 HTTP/1.1\" 302 0 \"-\" \"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; FunWebProducts; HotbarSearchToolbar 1.1; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; AskTbFWV5/5.11.3.15590)\" 4 4";
        Pattern p = Pattern.compile("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}).*?\\" + //ip
                                    "[([^:]*):"+ //date
                                    "(\\d{2}:\\d{2}:\\d{2}).*?\\].*?"+ //time
                                    "(/[^\\s]*).*$"); //uri
        
        Matcher m = p.matcher(s);
        if(m.find()){
            String ip = m.group(1);
            String date = m.group(2);
            String time = m.group(3);
            String uri = m.group(4);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-17
          • 2011-11-02
          • 2011-12-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多