Permalink
Browse files

Fixed documentation, I confused parse behavior with parse

  • Loading branch information...
1 parent 4fa372c commit 025c6b81d80790361226d14d11760e949690a7c8 @weppos committed Feb 15, 2009
Showing with 33 additions and 24 deletions.
  1. +17 −12 README.rdoc
  2. +16 −12 lib/apache_log_regex.rb
View
@@ -29,12 +29,8 @@ It tries to parse the `access.log` file and echoes each parsed line.
parser = ApacheLogRegex.new(format)
File.foreach('/var/apache/access.log') do |line|
- begin
- parser.parse(line)
- # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"87.18.183.252", ... }
- rescue ApacheLogRegex::ParseError => e
- puts "Error parsing log file: " + e.message
- end
+ 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.
@@ -44,14 +40,23 @@ The following example iterates all log lines, parses them and returns an array o
parser = ApacheLogRegex.new(format)
File.readlines('/var/apache/access.log').collect do |line|
- begin
- parser.parse(line)
- # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"87.18.183.252", ... }
- rescue ApacheLogRegex::ParseError => e
- nil
- end
+ 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 <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
@@ -33,12 +33,8 @@
# parser = ApacheLogRegex.new(format)
#
# File.foreach('/var/apache/access.log') do |line|
-# begin
-# parser.parse(line)
-# # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"87.18.183.252", ... }
-# rescue ApacheLogRegex::ParseError => e
-# puts "Error parsing log file: " + e.message
-# end
+# 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.
@@ -48,14 +44,22 @@
# parser = ApacheLogRegex.new(format)
#
# File.readlines('/var/apache/access.log').collect do |line|
-# begin
-# parser.parse(line)
-# # {"%r"=>"GET /blog/index.xml HTTP/1.1", "%h"=>"87.18.183.252", ... }
-# rescue ApacheLogRegex::ParseError => e
-# nil
-# end
+# 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 <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
+#
class ApacheLogRegex
NAME = 'ApacheLogRegex'

0 comments on commit 025c6b8

Please sign in to comment.