<commit_before>= Apache Log Regex

Apache Log Regex is a Ruby port of <NAME>'s Apache::LogRegex 1.4 Perl module, 
available at http://cpan.uwinnipeg.ca/~peterhi/Apache-LogRegex.
It provides functionalities to parse a line from an Apache log file into a hash.


== Dependencies

* Ruby >= 1.8.6 (not tested with previous versions)

ApacheLogRegex is compatible with Ruby 1.9.1.


== Overview

Apache Log Regex is designed to be a simple class to parse Apache log files. 

It takes an Apache logging format and generates a regular expression which is used to parse a line from a log file and returns a Hash with keys corresponding to the fields defined in the log format.
The log format should match the one defined in your Apache configuration file with the LogFormat directive.


== Example Usage

The following one is the most simple example usage. 
It tries to parse the `access.log` file and echoes each parsed line.

  format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
  parser = ApacheLogRegex.new(format)
  
  File.foreach('/var/apache/access.log') do |line|
    parser.parse(line)
    # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"192.168.127.12", ... }
  end

More often, you might want to collect parsed lines and use them later in your program.
The following example iterates all log lines, parses them and returns an array of Hash with the results.

  format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
  parser = ApacheLogRegex.new(format)

  File.readlines('/var/apache/access.log').collect do |line|
    parser.parse(line)
    # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"192.168.127.12", ... }
  end

If you want more control over the parser you can use the <tt>parse!</tt> method. 
It raises a <tt>ParseError</tt> if given line doesn't match the log format.

  common_log_format = '%h %l %u %t \"%r\" %>s %b'
  parser = ApacheLogRegex.new(common_log_format)
  
  # No exception
  parser.parse(line) # => nil
  
  # Raises an exception
  parser.parse!(line) # => ParseError



== Acknowledgments

This library is essentially a Ruby port of Apache::LogRegex Perl library.
A big thanks goes to <NAME>, the author of the original package. This Ruby GEM would probably not exists without your contribution!

I also want to thank you <NAME> and <NAME>, 
the authors of the Python[http://code.google.com/p/apachelog/] and PHP[http://kitty0.org/] ports.
First of all they gave me the inspiration to translate the original Perl code to Ruby, 
as soon as I discovered nobody did it before me.
Last but not least, their library have been extremely useful to help me understand some obscure Perl statements
from the original package.


== Author

{<NAME>}[http://www.simonecarletti.com/] <<EMAIL>>


== Resources

* {Homepage}[http://code.simonecarletti.com/apachelogregex]
* {API}[http://apachelogregex.rubyforge.org/]
* {GitHub}[http://github.com/weppos/apachelogregex/]
* {RubyForge}[http://rubyforge.org/projects/apachelogregex/]


== FeedBack and Bug reports

Feel free to email {Simone Carletti}[mailto:<EMAIL>] with any questions or feedback.

Please use the {Ticket System}[http://code.simonecarletti.com/projects/show/apachelogregex] to submit bug reports or feature request.


== Changelog<commit_msg>Fixed README formatting.<commit_after>= Apache Log Regex

Apache Log Regex is a Ruby port of <NAME>'s Apache::LogRegex 1.4 Perl module, 
available at http://cpan.uwinnipeg.ca/~peterhi/Apache-LogRegex.
It provides functionalities to parse a line from an Apache log file into a hash.


== Dependencies

* Ruby >= 1.8.6 (not tested with previous versions)

ApacheLogRegex is compatible with Ruby 1.9.1.


== Overview

Apache Log Regex is designed to be a simple class to parse Apache log files. 

It takes an Apache logging format and generates a regular expression which is used to parse a line from a log file and returns a Hash with keys corresponding to the fields defined in the log format.
The log format should match the one defined in your Apache configuration file with the LogFormat directive.


== Example Usage

The following one is the most simple example usage. 
It tries to parse the `access.log` file and echoes each parsed line.

  format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
  parser = ApacheLogRegex.new(format)
  
  File.foreach('/var/apache/access.log') do |line|
    parser.parse(line)
    # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"192.168.127.12", ... }
  end

More often, you might want to collect parsed lines and use them later in your program.
The following example iterates all log lines, parses them and returns an array of Hash with the results.

  format = '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
  parser = ApacheLogRegex.new(format)

  File.readlines('/var/apache/access.log').collect do |line|
    parser.parse(line)
    # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"192.168.127.12", ... }
  end

If you want more control over the parser you can use the <tt>parse!</tt> method. 
It raises a <tt>ParseError</tt> if given line doesn't match the log format.

  common_log_format = '%h %l %u %t \"%r\" %>s %b'
  parser = ApacheLogRegex.new(common_log_format)
  
  # No exception
  parser.parse(line) # => nil
  
  # Raises an exception
  parser.parse!(line) # => ParseError



== Acknowledgments

This library is essentially a Ruby port of Apache::LogRegex Perl library.
A big thanks goes to <NAME>, the author of the original package. This Ruby GEM would probably not exists without your contribution!

I also want to thank you <NAME> and <NAME>, 
the authors of the Python[http://code.google.com/p/apachelog/] and PHP[http://kitty0.org/] ports.
First of all they gave me the inspiration to translate the original Perl code to Ruby, 
as soon as I discovered nobody did it before me.
Last but not least, their library have been extremely useful to help me understand some obscure Perl statements
from the original package.


== Credits

Author:: {<NAME>}[http://www.simonecarletti.com/] <<EMAIL>>


== Resources

* {Homepage}[http://code.simonecarletti.com/apachelogregex]
* {API}[http://apachelogregex.rubyforge.org/]
* {GitHub}[http://github.com/weppos/apachelogregex/]
* {RubyForge}[http://rubyforge.org/projects/apachelogregex/]


== FeedBack and Bug reports

Feel free to email {<NAME>}[mailto:<EMAIL>] with any questions or feedback.
Please use the {Ticket System}[http://code.simonecarletti.com/apachelogregex] to submit bug reports or feature request.


== Changelog
{'fraction_non_alphanumeric': 0.117890541162042, 'fraction_numerical': 0.010731258623332822, 'mean_word_length': 3.9536826119969626, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '165919', 'n_tokens_mistral': 2116, 'n_tokens_neox': 1997, 'n_words': 797}