GithubHelp home page GithubHelp logo

nbui / apachelogregex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from weppos/apachelogregex

0.0 2.0 0.0 95 KB

Ruby parser for Apache log files based on regular expressions.

Home Page: http://code.simonecarletti.com/apachelogregex

License: Other

apachelogregex's Introduction

Apache Log Regex

Apache Log Regex is a Ruby port of Peter Hickman’s Apache::LogRegex 1.4 Perl module, available at 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"=>"87.18.183.252", ... }
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"=>"87.18.183.252", ... }
end

If you want more control over the parser you can use the parse! method. It raises a ParseError 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 Peter Hickman, the author of the original package. This Ruby GEM would probably not exists without your contribution!

I also want to thank you Harry Fuecks and Hamish Morgan, the authors of the Python and PHP 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

Resources

Changelog

See the CHANGELOG.rdoc file for details.

License

Copyright © 2008-2009 Simone Carletti, ApacheLogRegex is released under the MIT license.

apachelogregex's People

Contributors

weppos avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.