vizi_logger 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ = ViziLogger
2
+
3
+ == Introduction
4
+
5
+ This gem module creates a pagelogger that can used to log the visit to each
6
+ web page in a Ruby based web application (e.g., Ramaze, Rails). Each web page is
7
+ logged in Common Log Format to support analysis by an external Web Log Analyzer
8
+
9
+ == Installation
10
+
11
+ Just run:
12
+
13
+ gem install vizi_logger
14
+
15
+ The following code will simulate the implementation of the software in a Ruby
16
+ based web application. When used in a web application, the pagelogger should be
17
+ called with the following:
18
+
19
+ weblog.page page_url # where page_url provides the url information
20
+
21
+ The following example will allow for the testing of the gem in your development
22
+ environment.
23
+
24
+ You will need a Web Log Analyzer program to review the results of your application
25
+ logging. The output of this gem has been tested with the Summary web log analyzer.
26
+ Check it out at www.summary.net.
27
+
28
+ == Usage
29
+
30
+ require 'rubygems' # needed for ruby 1.8.7
31
+ require 'vizi_logger'
32
+
33
+ # weblog = Vizi::PageLogger.new(STDOUT)
34
+
35
+ weblog = Vizi::PageLogger.new('./log/vizidemo.log',shift_age = 'weekly')
36
+
37
+ weblog.addremote '127.0.0.1' # for batch testing
38
+ # weblog.addremote request.remote_addr # use this in live mode
39
+
40
+ weblog.formatter=Vizi::LogFormatter.new
41
+
42
+ urlist = ["/","/users/login","/users/list","/posts","/posts/view/1"]
43
+
44
+ 25.times do
45
+ sleep rand*2 # create random interval
46
+ page_url = urlist[(5*rand).to_i] # select a random page url
47
+ weblog.page page_url
48
+ end
49
+
50
+ == License
51
+
52
+ This code is made available under the MIT license.
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/testtask'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = "vizi_whois"
8
+ s.version = "0.1.0"
9
+ s.author = "Al Kivi"
10
+ s.email = "al.kivi at vizitrax.com"
11
+ s.homepage = "http://github.com/al-kivi/Vizi_Whois"
12
+ s.summary = "Global whois module to select the right whois server and get response data"
13
+ s.description = "This gem module provides a classes to find the right Regional Internet Registry
14
+ for a given IP Address. The query method will navigate each major RIR until a response is found"
15
+
16
+ s.platform = Gem::Platform::RUBY
17
+ s.has_rdoc = true
18
+ s.extra_rdoc_files = ["README.rdoc"]
19
+
20
+ s.require_path = "lib"
21
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("lib/**/*")
22
+ end
23
+
24
+ Rake::GemPackageTask.new(spec) do |pkg|
25
+ pkg.need_tar = true
26
+ end
27
+
28
+ Rake::RDocTask.new(:rdoc) do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'HttpLogParser'
31
+ rdoc.options << '--line-numbers' << '--inline-source'
32
+ rdoc.rdoc_files.include('README')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
35
+
36
+ Rake::TestTask.new do |t|
37
+ t.libs << 'test'
38
+ t.test_files = FileList["test/**/*_test.rb"]
39
+ t.verbose = true
40
+ end
41
+
42
+ task :default => "pkg/#{spec.name}-#{spec.version}.gem" do
43
+ puts "generated latest version"
44
+ end
45
+
@@ -0,0 +1,9 @@
1
+ # run with ... sudo bash build_gem.sh
2
+
3
+ gem uninstall vizi_logger
4
+
5
+ chmod -R a+r ./lib
6
+
7
+ gem build vizi_logger.gemspec
8
+
9
+ gem install -l vizi_logger-0.1.0.gem
@@ -0,0 +1 @@
1
+ sudo gem uninstall vizi_logger
@@ -0,0 +1 @@
1
+ sudo gem build vizi_logger.gemspec
@@ -0,0 +1,3 @@
1
+ ## sudo gem install --local vizi_logger-0.1.0.gem
2
+
3
+ sudo gem install -l vizi_logger-0.1.0.gem
@@ -0,0 +1,6 @@
1
+ ruby "testit.rb"
2
+
3
+ pause
4
+ del "%0"
5
+
6
+ pause
@@ -0,0 +1,6 @@
1
+ ruby "vizi_whois.rb"
2
+
3
+ pause
4
+ del "%0"
5
+
6
+ pause
@@ -0,0 +1,55 @@
1
+ # This gem module creates a pagelogger that can used to log the visit to each
2
+ # web page in a Ruby based web application (e.g., Ramaze, Rails). Each web page is
3
+ # logged in Common Log Format to support analysis by an external Web Log Analyzer
4
+ #
5
+ # Author:: Al Kivi <al.kivi@vizitrax.com>
6
+ # License:: MIT
7
+
8
+ require 'logger'
9
+
10
+ module Vizi
11
+ # This class includes a set of methods that extend the Ruby Logger
12
+ class PageLogger < ::Logger
13
+
14
+ attr_accessor :ip
15
+
16
+ # This method will add the remote ip address to the Logger class
17
+ def initialize (*args)
18
+ super
19
+ ip = ''
20
+ end
21
+
22
+ # This method will add the ip address to the logger class
23
+ def addremote(ipaddr)
24
+ self.ip = ipaddr
25
+ end
26
+
27
+ # This method will create the log entry adding the request information
28
+ # Calling format is ... log.page '/admin/login.htm' or similar
29
+ def page(*args)
30
+ request = args.first
31
+ self.info([self.ip, request])
32
+ end
33
+
34
+ end # Pagelogger
35
+
36
+ # This class customizes the Logger::Formatter to match the Common Log Format
37
+ class LogFormatter < Logger::Formatter
38
+ # Provide a call() method that returns the formatted message.
39
+ def call(severity, time, program_name, message)
40
+ if severity == "INFO"
41
+ message.insert(1, ' -')
42
+ message.insert(2, ' - ')
43
+ message.insert(3, "["+Time.now.strftime("%d/%b/%Y:%H:%M:%S %z")+"] ")
44
+ request = message[4]
45
+ message[4] = '"'+request+'"'
46
+ message.insert(5, ' 200')
47
+ message.insert(7, ' 0')
48
+ print_message = "#{String(message)}\n"
49
+ else
50
+ super
51
+ end
52
+ end
53
+ end
54
+
55
+ end # Vizi
@@ -0,0 +1,4 @@
1
+ # module Vizi # :doc:
2
+ require 'vizi/vizi_logger' # use for the gem
3
+ ## load './lib/vizi/vizi_logger.rb' # use for testing
4
+ # end
@@ -0,0 +1,7 @@
1
+ 192.168.198.92 - - [22/Jul/2012:23:08:37 -0400] "GET / HTTP/1.1" 200 6394 www.yahoo.com "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1...)" "-"
2
+ 192.168.198.92 - - [22/Jul/2012:23:08:38 -0400] "GET /images/logo.gif HTTP/1.1" 200 807 www.yahoo.com "http://www.some.com/" "Mozilla/4.0 (compatible; MSIE 6...)" "-"
3
+ 192.168.72.177 - - [22/Jul/2012:23:32:14 -0400] "GET /news/sports.html HTTP/1.1" 200 3500 www.yahoo.com "http://www.some.com/" "Mozilla/4.0 (compatible; MSIE ...)" "-"
4
+ 192.168.72.177 - - [22/Jul/2012:23:32:14 -0400] "GET /favicon.ico HTTP/1.1" 404 1997 www.yahoo.com "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3)..." "-"
5
+ 192.168.72.177 - - [22/Jul/2012:23:32:15 -0400] "GET /style.css HTTP/1.1" 200 4138 www.yahoo.com "http://www.yahoo.com/index.html" "Mozilla/5.0 (Windows..." "-"
6
+ 192.168.72.177 - - [22/Jul/2012:23:32:16 -0400] "GET /js/ads.js HTTP/1.1" 200 10229 www.yahoo.com "http://www.search.com/index.html" "Mozilla/5.0 (Windows..." "-"
7
+ 192.168.72.177 - - [22/Jul/2012:23:32:19 -0400] "GET /search.php HTTP/1.1" 400 1997 www.yahoo.com "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; ...)" "-"
@@ -0,0 +1,26 @@
1
+ # Logfile created on Sun Jul 22 12:14:42 -0400 2012 by logger.rb/1.2.6
2
+ 127.0.0.1 - - [22/Jul/2012:12:14:42 -0400] "/users/list" 200 0
3
+ 127.0.0.1 - - [22/Jul/2012:12:14:42 -0400] "/users/login" 200 0
4
+ 127.0.0.1 - - [22/Jul/2012:12:14:43 -0400] "/users/login" 200 0
5
+ 127.0.0.1 - - [22/Jul/2012:12:14:43 -0400] "/users/login" 200 0
6
+ 127.0.0.1 - - [22/Jul/2012:12:14:43 -0400] "/" 200 0
7
+ 127.0.0.1 - - [22/Jul/2012:12:14:44 -0400] "/users/login" 200 0
8
+ 127.0.0.1 - - [22/Jul/2012:12:14:46 -0400] "/users/login" 200 0
9
+ 127.0.0.1 - - [22/Jul/2012:12:14:47 -0400] "/users/list" 200 0
10
+ 127.0.0.1 - - [22/Jul/2012:12:14:49 -0400] "/users/login" 200 0
11
+ 127.0.0.1 - - [22/Jul/2012:12:14:49 -0400] "/posts" 200 0
12
+ 127.0.0.1 - - [22/Jul/2012:12:14:50 -0400] "/posts" 200 0
13
+ 127.0.0.1 - - [22/Jul/2012:12:14:50 -0400] "/users/login" 200 0
14
+ 127.0.0.1 - - [22/Jul/2012:12:14:51 -0400] "/posts/view/1" 200 0
15
+ 127.0.0.1 - - [22/Jul/2012:12:14:53 -0400] "/posts" 200 0
16
+ 127.0.0.1 - - [22/Jul/2012:12:14:55 -0400] "/users/list" 200 0
17
+ 127.0.0.1 - - [22/Jul/2012:12:14:55 -0400] "/posts/view/1" 200 0
18
+ 127.0.0.1 - - [22/Jul/2012:12:14:57 -0400] "/posts/view/1" 200 0
19
+ 127.0.0.1 - - [22/Jul/2012:12:14:59 -0400] "/users/login" 200 0
20
+ 127.0.0.1 - - [22/Jul/2012:12:14:59 -0400] "/" 200 0
21
+ 127.0.0.1 - - [22/Jul/2012:12:15:00 -0400] "/users/list" 200 0
22
+ 127.0.0.1 - - [22/Jul/2012:12:15:00 -0400] "/users/login" 200 0
23
+ 127.0.0.1 - - [22/Jul/2012:12:15:01 -0400] "/users/list" 200 0
24
+ 127.0.0.1 - - [22/Jul/2012:12:15:02 -0400] "/" 200 0
25
+ 127.0.0.1 - - [22/Jul/2012:12:15:03 -0400] "/users/login" 200 0
26
+ 127.0.0.1 - - [22/Jul/2012:12:15:04 -0400] "/users/list" 200 0
@@ -0,0 +1,48 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ require 'uri'
4
+
5
+ class ParserTest < Test::Unit::TestCase
6
+
7
+ def test_parser_creation
8
+
9
+ parser = HttpLogParser.new
10
+ assert_not_nil parser
11
+ assert_equal 5, parser.formats.size
12
+
13
+ parser = HttpLogParser.new('%h %l %u %t \"%r\" %>s %b')
14
+ assert_not_nil parser
15
+ assert_equal 1, parser.formats.size
16
+
17
+ parser = HttpLogParser.new({
18
+ :common => '%h %l %u %t \"%r\" %>s %b',
19
+ :common_with_virtual => '%v %h %l %u %t \"%r\" %>s %b',
20
+ })
21
+ assert_not_nil parser
22
+ assert_equal 2, parser.formats.size
23
+
24
+ end
25
+
26
+ def test_simple_parsing
27
+
28
+ parser = HttpLogParser.new
29
+ assert_not_nil parser
30
+
31
+ parsed = parser.parse_line('111.222.333.444 - - [21/Apr/2010:01:02:03 +0000] "GET /some/url?some=parameter HTTP/1.1" 302 123 "http://somewhere.com" "Browser (Version 1.0)"')
32
+
33
+ assert_equal '111.222.333.444', parsed[:ip]
34
+ assert_equal '111.222.333.444', parsed[:domain]
35
+ assert_equal '-', parsed[:auth]
36
+ assert_equal '-', parsed[:username]
37
+ assert_equal '21/Apr/2010:01:02:03 +0000', parsed[:datetime]
38
+ assert_equal 'GET /some/url?some=parameter HTTP/1.1', parsed[:request]
39
+ assert_equal '302', parsed[:status]
40
+ assert_equal '123', parsed[:bytecount]
41
+ assert_equal 'http://somewhere.com', parsed[:referer]
42
+ assert_equal 'Browser (Version 1.0)', parsed[:user_agent]
43
+
44
+ assert_equal 11, parsed.size
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/http_log_parser.rb"
3
+ begin; require 'ruby-debug'; rescue LoadError; end
@@ -0,0 +1,30 @@
1
+ # This is a sample application that uses the Vizi_logger gem classes
2
+ #
3
+ # This application will create a log file with a set of simulated web log records
4
+ # You will need a web log analyzer (e.g., Analog) to confirm the readability of this file
5
+ #
6
+ # Author:: Al Kivi <al.kivi@vizitrax.com>
7
+
8
+ # require '~/appgems/vizi_logger/lib/vizi_logger.rb' # use this line for testing
9
+
10
+ require 'rubygems' # needed for ruby 1.8.7
11
+ require 'vizi_logger'
12
+
13
+ # weblog = Vizi::PageLogger.new(STDOUT)
14
+
15
+ weblog = Vizi::PageLogger.new('./log/vizidemo.log',shift_age = 'weekly')
16
+
17
+ weblog.addremote '127.0.0.1' # for batch testing
18
+ # weblog.addremote request.remote_addr # use this in live mode
19
+
20
+ weblog.formatter=Vizi::LogFormatter.new
21
+
22
+ urlist = ["/","/users/login","/users/list","/posts","/posts/view/1"]
23
+
24
+ 25.times do
25
+ sleep rand*2 # create random interval
26
+ page_url = urlist[(5*rand).to_i] # select a random page url
27
+ weblog.page page_url
28
+ end
29
+
30
+ #weblog.page
@@ -0,0 +1,49 @@
1
+ # This is a sample application that uses the Vizi_whois gem classes
2
+ #
3
+ # This application will read a text file with IP addresses.
4
+ # For each IP address, the whois method will be called and responses received.
5
+ #
6
+ # Author:: Al Kivi <al.kivi@vizitrax.com>
7
+
8
+ #require 'c:\rails\vizi_whois\lib\vizi_whois'
9
+ require 'rubygems' # needed for ruby 1.8.7
10
+ require 'vizi_whois'
11
+
12
+ require 'logger'
13
+ require 'socket'
14
+ require 'yaml'
15
+
16
+ syslog = Logger.new('./log/system.log',shift_age = 'weekly')
17
+ syslog.info "Starting IP address test file ... >>> "+Time.now.to_s
18
+
19
+ out_file = File.new('./log/output.log', 'w')
20
+
21
+ File.delete('./log/formatted.log') if File.exist?('./log/formatted.log')
22
+ parse_file = File.new('./log/formatted.log', 'w')
23
+
24
+ p 'starting'
25
+
26
+ # Open test file for reading
27
+ File.open('./data/testfile.txt', 'r') do |file|
28
+ rec_count = 0
29
+ while(line = file.gets) # Read each line of the test file, one IP address per line
30
+ p line
31
+ @whoisresult = Vizi::Gowhois.new
32
+ p line.chomp
33
+ rarray = @whoisresult.query(line.chomp)
34
+ @contents = rarray[0]
35
+ out_file.puts '----------------------------------------------------------------'
36
+ out_file.puts '> ' + line
37
+ out_file.puts '>> ' + rarray[1]
38
+ out_file.puts '>>> ' + rarray[2]
39
+ out_file.puts '----------------------------------------------------------------'
40
+ out_file.puts @contents
41
+ @result = Vizi::Formatter.new
42
+ @formatted = @result.parse(@contents, rarray[1])
43
+ @formatted = @formatted.gsub('\n','<br/>')
44
+ p @formatted
45
+ rec_count = rec_count + 1
46
+ end
47
+ syslog.info "Record count is "+rec_count.to_s
48
+ syslog.info "Ending ... >>> "+Time.now.to_s
49
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'vizi_logger'
5
+ s.version = '0.1.0'
6
+ s.summary = "Ruby based logger that will record web page visits in the common log format"
7
+ s.description = "This gem module provides a logger class and a log formatter class that will create
8
+ a log file in a common log format.
9
+
10
+ A ruby based application can generate a log file record with each page visit. This capability provides
11
+ logging that can be highly customized by the ruby developer. A web log analyzer will be needed to
12
+ summarize and analyze the web log records"
13
+ s.files = Dir.glob("**/**/**")
14
+ s.test_files = Dir.glob("test/*_test.rb")
15
+ s.author = "Al Kivi"
16
+ s.homepage = "http://www.vizitrax.com"
17
+ s.email = "al.kivi@yahoo.com"
18
+ s.has_rdoc = true
19
+ s.required_ruby_version = '>= 1.8.7'
20
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vizi_logger
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Al Kivi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-22 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: |-
22
+ This gem module provides a logger class and a log formatter class that will create
23
+ a log file in a common log format.
24
+
25
+ A ruby based application can generate a log file record with each page visit. This capability provides
26
+ logging that can be highly customized by the ruby developer. A web log analyzer will be needed to
27
+ summarize and analyze the web log records
28
+ email: al.kivi@yahoo.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - log/vizidemo.log
37
+ - log/apache.log
38
+ - testit.rb
39
+ - test/test_helper.rb
40
+ - test/parser_test.rb
41
+ - build_gem.sh
42
+ - Rakefile
43
+ - build_gem3.sh
44
+ - geany_run_script.bat
45
+ - build_gem1.sh
46
+ - testit_old.rb
47
+ - build_gem2.sh
48
+ - vizi_logger.gemspec
49
+ - lib/vizi/geany_run_script.bat
50
+ - lib/vizi/vizi_logger.rb
51
+ - lib/vizi_logger.rb
52
+ - README.rdoc
53
+ - vizi_logger-0.1.0.gem
54
+ homepage: http://www.vizitrax.com
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ hash: 57
68
+ segments:
69
+ - 1
70
+ - 8
71
+ - 7
72
+ version: 1.8.7
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Ruby based logger that will record web page visits in the common log format
89
+ test_files:
90
+ - test/parser_test.rb