Skip to content

Commit b0f4052

Browse files
committed
Clean up and simplify the DSL
Use instance_eval instead of eval'd code blocks to implement the filter and execution blocks Kudos to https://github.com/edjames for the suggestion!
1 parent 10b2574 commit b0f4052

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,44 +60,44 @@ Advanced Examples
6060

6161
* read from STDIN and print out the length of each line _(to illustrate -e option)_
6262

63-
$ ntail -e '{ |line| puts line.size }'
63+
$ ntail -e 'puts size'
6464

6565
* read from STDIN but only print out non-empty lines _(to illustrate -f option)_
6666

67-
$ ntail -f '{ |line| line.size $ 0 }'
67+
$ ntail -f 'size != 0'
6868

6969
* the following invocations behave exactly the same _(to illustrate -e and -f options)_
7070

7171
$ ntail
72-
$ ntail -f '{ |line| true }' -e '{ |line| puts line }'
72+
$ ntail -f 'true' -e 'puts self'
7373

7474
* print out all HTTP requests that are coming from a given IP address
7575

76-
$ ntail -f '{ |line| line.remote_address == "208.67.222.222" }' /var/log/nginx/access.log
76+
$ ntail -f 'remote_address == "208.67.222.222"' /var/log/nginx/access.log
7777

7878
* find all HTTP requests that resulted in a '5xx' HTTP error/status code _(e.g. Rails 500 errors)_
7979

80-
$ gunzip -S .gz -c access.log-20101216.gz | ntail -f '{ |line| line.server_error_status? }'
80+
$ gunzip -S .gz -c access.log-20101216.gz | ntail -f 'server_error_status?'
8181

8282
* generate a summary report of HTTP status codes, for all non-200 HTTP requests
8383

84-
$ ntail -f '{ |line| line.status != "200" }' -e '{ |line| puts line.status }' access.log | sort | uniq -c
84+
$ ntail -f 'status != "200"' -e 'puts status' access.log | sort | uniq -c
8585
76 301
8686
16 302
8787
2 304
8888
1 406
8989

9090
* print out GeoIP country and city information for each HTTP request _(depends on the optional `geoip` gem)_
9191

92-
$ ntail -e '{ |line| puts [line.to_country_s, line.to_city_s].join("\t") }' /var/log/nginx/access.log
92+
$ ntail -e 'puts [to_country_s, to_city_s].join("\t")' /var/log/nginx/access.log
9393
United States Los Angeles
9494
United States Houston
9595
Germany Berlin
9696
United Kingdom London
9797

9898
* print out the IP address and the corresponding host name for each HTTP request _(slows things down considerably, due to `nslookup` call)_
9999

100-
$ ntail -e '{ |line| puts [line.remote_address, line.to_host_name].join("\t") }' /var/log/nginx/access.log
100+
$ ntail -e 'puts [remote_address, to_host_s].join("\t")' /var/log/nginx/access.log
101101
66.249.72.196 crawl-66-249-72-196.googlebot.com
102102
67.192.120.134 s402.pingdom.com
103103
75.31.109.144 adsl-75-31-109-144.dsl.irvnca.sbcglobal.net

lib/ntail/application.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ def run!
5454
if log_line.parsable
5555
parsable_lines += 1
5656
unless @options.parse_only
57-
if !@options.filter || @options.filter.call(log_line)
57+
if !@options.filter || log_line.instance_eval(@options.filter)
5858
lines_processed += 1
5959
if @options.code
60-
@options.code.call(log_line)
60+
log_line.instance_eval(@options.code)
6161
else
6262
puts log_line.to_s(:color => true)
6363
end

lib/ntail/options.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ def parse_options(argv, defaults = {})
1616
end
1717

1818
opts.on '--filter', '-f CODE', "Ruby code block for filtering (parsed) lines - needs to return true or false." do |value|
19-
options.filter = eval "Proc.new #{value}"
19+
options.filter = value
2020
end
2121

2222
opts.on '--execute', '-e CODE', "Ruby code block for processing each (parsed) line." do |value|
23-
options.code = eval "Proc.new #{value}"
23+
options.code = value
2424
end
2525

2626
opts.on '--line-number', '-l LINE_NUMBER', "Only process the line with the given line number" do |value|

0 commit comments

Comments
 (0)