PHP regular parsing Apache log file

Source: Internet
Author: User
Tags apache log file http authentication apache log
You can scroll the logs by the hour, using the PHP regular analysis log method to solve

$logLine = ' 127.0.0.1--[22/may/2015:17:09:13 +0800] ' get/sale/images/y-select.png http/1.1 ' 1095 '; $pattern = '/^ (? P
 
  
   
  [0-9.] +) - - \[(? P
  
   [^\]]+] \]+ "GET (? P
   
    
     
    [^]+) http\/1.[ 1|0|2] "(? P
    
     
      
     [0-9.] +) (? P
     
      
       
      [0-9.] +)/I ';p reg_match ($pattern, $log, $match);//var_dump ($match); $ip     = $match [' IP ']; $time   = Strtotime ($match [ "Time"); $url    = $match [' url ']; $status = $match [' status ']; $size   = $match [' Size '];p rintf ("ip:%s Access:%s URL: %s Status:%s File Size:%s ", $ip, $time, $url, $status, $size);
     
      
    
     
   
     
 
  

And that's it.

Use regular expressions to separate Apache log files

Www.MyException.Cn netizens share in: 2015-08-26 views: 17 times

Separating Apache log files with regular expressions
Examples of Apache log files in the Common log format:
127.0.0.1-frank [10/oct/2000:13:55:36-0700] "Get/apache_pb.gif http/1.0" 200 2326
Examples of Apache log files in the combined log format:
127.0.0.1-frank [10/oct/2000:13:55:36-0700] "Get/apache_pb.gif http/1.0" 2326 "mozilla/4.08 [en] (Win98; I; NAV) "
The IP address of the client.
The RFC1413 identity, which is determined by the client-side Identd process, indicates that the information here is not valid because of the symbol "-" in the output.
The HTTP authentication system gets the customer ID (userid) To access the page, and if the page is not password protected, this entry will be "-".
The time when the server completed the request processing.
The client's action \ Requested resource \ protocol used.
The status code returned by the server to the client.
The number of bytes returned to the client that do not include the response header. If no information is returned, this entry should be "-".
"Referer" request header.
"User-agent" request header.
The regular expression used to extract information consists of:
^: matches the beginning of each line.
([0-9.]+) \s: Match IP address.
([\w.-]+) \s: Matches an identity, consisting of a numeric letter underscore or a dot delimiter.
([\w.-]+) \s: Matches the UserID, consisting of a numeric letter underscore or a dot delimiter.
(\[[^\[\]]+\]) \s: Match time.
"((?: [^"]|\ ") +)" \s: Match request information, double quotes may appear escaped double quotation marks \ ".
(\d{3}) \s: matches the status code.
(\d+|-) \s: Matches the number of response bytes or-.
"((?: [^"]|\ ") +)" \s: Match "Referer" request header, double quotes may appear in the escaped double quotation mark \ ".
"((?: [^"]|\ ") +)": Match "user-agent" request header, double quotation marks may appear in the double quotation mark \ ".
$: Matches the end of the line.
The final expression is as follows:
^ ([0-9.] +) \s ([\w.-]+) \s ([\w.-]+] \s (\[[^\[\]]+\]) \s "((?: [^"]|\ ") +)" \s (\d{3}) \s (\d+|-) \s "((?: [^"]|\ ") +)" \s "((?: [^"]|\ ") + )"$

The above describes the PHP regular parsing Apache log file files, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.