hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Centralized Logging With WEBrick #

by why in inspect

WEBrick is sittin pretty. We owe it dearly. For giving our Wikis the ability to stand quickly on their own two feet. For giving our web apps an instant development server. And Catapult. Instant web server, just like those little red pills that turn into foam dinosaurs when you drop ‘em in the sink.

A favorite of mine is the Gnome’s Guide to WEBrick [PDF]. A rather complete tour. And, now, a trick to go with Chapter 5: Logging. We’re just going to setup an AccessLog.

 require 'webrick'
 include WEBrick
 def start_webrick(config = {})
     # always listen on port 8080
     config.update(:Port => 8080)
     server = HTTPServer.new(config)
     yield server if block_given?
     ['INT', 'TERM'].each {|signal|
         trap(signal) {server.shutdown}
     }
     server.start
 end

 server_logger = Log.new('webrick-server.log')

 # The :AccessLog configuration takes an array.
 # Each element of the array should be
 # a two-element array where the first element
 # is the stream (or anything responding to <<) and
 # the second element is the access log format.
 # Please see webrick/accesslog.rb for available formats.
 access_log_stream = File.open('webrick-access.log', 'w')
 access_log = [ [ access_log_stream, AccessLog::COMBINED_LOG_FORMAT ] ]
 start_webrick(
     :Logger => server_logger,
     :AccessLog => access_log,
     :DocumentRoot => './'
 )

This server keeps logs of its activity and also allows clients to access the logs kept. A simple approach to centralized logging. Since WEBrick keeps “combined” format logs, you’ll get the IP address, a timestamp, the URL and such. If you’re distributing a software throughout your company (or group of friends) and you want to track how many people install it, simply have your software issue an HTTP request to your WEBrick server.

 require 'open-uri'
 open( "http://go.hobix.com:8090/install-1.0.txt" ) do |f|
   puts f.read
 end rescue nil
In the above, I have the server issue an acknowledgement that the server was contacted. If the connection is refused, installation continues unscathed.
said on 02 Mar 2005 at 20:31

fasdf

Comments are closed for this entry.