Changeset 899

Show
Ignore:
Timestamp:
12/15/07 00:02:04 (9 months ago)
Author:
wayneeseguin
Message:

Most of the logger refactoring is done.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/bin/mongrel_rails

    r891 r899  
    7676      if @generate 
    7777        @generate = File.expand_path(@generate) 
    78         STDERR.puts "** Writing config to \"#@generate\"." 
     78        log(:error, "** Writing config to \"#@generate\".") 
    7979        open(@generate, "w") {|f| f.write(settings.to_yaml) } 
    80         STDERR.puts "** Finished.  Run \"mongrel_rails start -C #@generate\" to use the config file." 
     80        log(:error, "** Finished.  Run \"mongrel_rails start -C #@generate\" to use the config file.") 
    8181        exit 0 
    8282      end 
     
    147147            system cmd 
    148148          else 
    149             STDERR.puts "Can't restart unless in daemon mode." 
     149            log(:error, "Can't restart unless in daemon mode.") 
    150150            exit 1 
    151151          end 
     
    161161        settings = YAML.load_file(@config_file) 
    162162      ensure 
    163         STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon]  
     163        log(:error, "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] ) 
    164164      end 
    165165 
  • trunk/examples/builder.rb

    r121 r899  
    55 
    66  def process(request, response) 
    7     STDERR.puts "My options are: #{options.inspect}" 
    8     STDERR.puts "Request Was:" 
    9     STDERR.puts request.params.to_yaml 
     7    log(:error, "My options are: #{options.inspect}") 
     8    log(:error, "Request Was:") 
     9    log(:error, request.params.to_yaml) 
    1010  end 
    1111end 
  • trunk/lib/mongrel.rb

    r889 r899  
    1  
    21require 'socket' 
    32require 'tempfile' 
     
    1514 
    1615require 'http11' 
     16require 'mongrel/log' 
    1717require 'mongrel/cgi' 
    1818require 'mongrel/handlers' 
     
    4141    attr_accessor :http_body 
    4242  end 
    43  
    4443 
    4544  # This is the main driver of Mongrel, while the Mongrel::HttpParser and Mongrel::URIClassifier 
     
    8483    # socket.accept calls in order to give the server a cheap throttle time.  It defaults to 0 and 
    8584    # actually if it is 0 then the sleep is not done at all. 
    86     def initialize(host, port, num_processors=950, throttle=0, timeout=60
     85    def initialize(host, port, num_processors=950, throttle=0, timeout=60, log=nil
    8786       
    8887      tries = 0 
     
    9695      @num_processors = num_processors 
    9796      @timeout = timeout 
     97      Mongrel::Logger = Mongrel::Log.new(log || "mongrel-#{port}.log") 
    9898    end 
    9999 
     
    181181        client.close rescue nil 
    182182      rescue HttpParserError => e 
    183         STDERR.puts "#{Time.now.httpdate}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}" 
    184         STDERR.puts "#{Time.now.httpdate}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n" 
     183        log(:error, "#{Time.now.httpdate}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}") 
     184        log(:error, "#{Time.now.httpdate}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n") 
    185185        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4 
    186186        client.write(Const::ERROR_400_RESPONSE) 
     
    188188        reap_dead_workers('too many files') 
    189189      rescue Object => e 
    190         STDERR.puts "#{Time.now.httpdate}: Read error: #{e.inspect}" 
    191         STDERR.puts e.backtrace.join("\n"
     190        log(:error, "#{Time.now.httpdate}: Read error: #{e.inspect}") 
     191        log(:error, e.backtrace.join("\n")
    192192      ensure 
    193193        begin 
     
    196196          # Already closed 
    197197        rescue Object => e 
    198           STDERR.puts "#{Time.now.httpdate}: Client error: #{e.inspect}" 
    199           STDERR.puts e.backtrace.join("\n"
     198          log(:error, "#{Time.now.httpdate}: Client error: #{e.inspect}") 
     199          log(:error, e.backtrace.join("\n")
    200200        end 
    201201        request.body.delete if request and request.body.class == Tempfile 
     
    209209    def reap_dead_workers(reason='unknown') 
    210210      if @workers.list.length > 0 
    211         STDERR.puts "#{Time.now.httpdate}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'" 
     211        log(:error, "#{Time.now.httpdate}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'") 
    212212        error_msg = "#{Time.now.httpdate}: Mongrel timed out this thread: #{reason}" 
    213213        mark = Time.now 
     
    216216 
    217217          if mark - worker[:started_on] > @timeout + @throttle 
    218             STDERR.puts "#{Time.now.httpdate}: Thread #{worker.inspect} is too old, killing." 
     218            log(:error, "#{Time.now.httpdate}: Thread #{worker.inspect} is too old, killing.") 
    219219            worker.raise(TimeoutError.new(error_msg)) 
    220220          end 
     
    231231    def graceful_shutdown 
    232232      while reap_dead_workers("shutdown") > 0 
    233         STDERR.puts "#{Time.now.httpdate}: Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds." 
     233        log(:error, "#{Time.now.httpdate}: Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds.") 
    234234        sleep @timeout / 10 
    235235      end 
     
    277277   
    278278              if worker_list.length >= @num_processors 
    279                 STDERR.puts "#{Time.now.httpdate}: Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection." 
     279                log(:error, "#{Time.now.httpdate}: Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection.") 
    280280                client.close rescue nil 
    281281                reap_dead_workers("max processors") 
     
    296296              client.close rescue nil 
    297297            rescue Object => e 
    298               STDERR.puts "#{Time.now.httpdate}: Unhandled listen loop exception #{e.inspect}." 
    299               STDERR.puts e.backtrace.join("\n"
     298              log(:error, "#{Time.now.httpdate}: Unhandled listen loop exception #{e.inspect}.") 
     299              log(:error, e.backtrace.join("\n")
    300300            end 
    301301          end 
     
    303303        ensure 
    304304          @socket.close 
    305           # STDERR.puts "#{Time.now.httpdate}: Closed socket." 
     305          # log(:error, "#{Time.now.httpdate}: Closed socket.") 
    306306        end 
    307307      end 
  • trunk/lib/mongrel/cgi.rb

    r889 r899  
    174174    # The stdoutput should be completely bypassed but we'll drop a warning just in case 
    175175    def stdoutput 
    176       STDERR.puts "#{Time.now.httpdate}: WARNING: Your program is doing something not expected.  Please tell Zed that stdoutput was used and what software you are running.  Thanks." 
     176      STDERR.puts "#{Time.now.rfc822}: WARNING: Your program is doing something not expected.  Please tell Zed that stdoutput was used and what software you are running.  Thanks." 
    177177      @response.body 
    178178    end     
    179179 
    180180  end 
     181 
    181182end 
  • trunk/lib/mongrel/configurator.rb

    r891 r899  
    6060 
    6161        if uid != target_uid or gid != target_gid 
    62           log "Initiating groups for #{user.inspect}:#{group.inspect}." 
     62          log(:info, "Initiating groups for #{user.inspect}:#{group.inspect}.") 
    6363          Process.initgroups(user, target_gid) 
    6464         
    65           log "Changing group to #{group.inspect}." 
     65          log(:info, "Changing group to #{group.inspect}.") 
    6666          Process::GID.change_privilege(target_gid) 
    6767 
    68           log "Changing user to #{user.inspect}."  
     68          log(:info, "Changing user to #{user.inspect}." ) 
    6969          Process::UID.change_privilege(target_uid) 
    7070        end 
    7171      rescue Errno::EPERM => e 
    72         log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}." 
    73         log "Mongrel failed to start." 
     72        log(:critical, "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}.") 
     73        log(:critical, "Mongrel failed to start.") 
    7474        exit 1 
    7575      end 
     
    8383    def write_pid_file 
    8484      unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ 
    85         log "Writing PID file to #{@pid_file}" 
     85        log(:info, "Writing PID file to #{@pid_file}") 
    8686        open(@pid_file,"w") {|f| f.write(Process.pid) } 
    8787        open(@pid_file,"w") do |f| 
     
    193193          logfile = File.join(ops[:cwd],logfile) 
    194194          if not File.exist?(File.dirname(logfile)) 
    195             log "!!! Log file directory not found at full path #{File.dirname(logfile)}.  Update your configuration to use a full path." 
     195            log(:critical, "!!! Log file directory not found at full path #{File.dirname(logfile)}.  Update your configuration to use a full path.") 
    196196            exit 1 
    197197          end 
     
    204204 
    205205      else 
    206         log "WARNING: Win32 does not support daemon mode." 
     206        log(:warning, "WARNING: Win32 does not support daemon mode.") 
    207207      end 
    208208    end 
     
    250250 
    251251      # check all the mime types to make sure they are the right format 
    252       mime.each {|k,v| log "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 } 
     252      mime.each {|k,v| log(, "WARNING: MIME type #{k} must start with '.'") if k.index(".") != 0 } 
    253253 
    254254      return mime 
     
    362362 
    363363      # forced shutdown, even if previously restarted (actually just like TERM but for CTRL-C) 
    364       trap("INT") { log "INT signal received."; stop(false) } 
    365  
    366       # clean up the pid file always 
     364      trap("INT") { log(:notice, "INT signal received."; stop(false) } 
     365 
     366      # always clean up the pid file 
    367367      at_exit { remove_pid_file } 
    368368 
    369369      unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ 
    370370        # graceful shutdown 
    371         trap("TERM") { log "TERM signal received."; stop } 
    372         trap("USR1") { log "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client } 
     371        trap("TERM") { log(:notice, "TERM signal received."; stop) } 
     372        # debug mode 
     373        trap("USR1") { log(:notice, "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client) } 
    373374        # restart 
    374         trap("USR2") { log "USR2 signal received."; stop(true) } 
    375  
    376         log "Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart)." 
     375        trap("USR2") { log(:notice, "USR2 signal received."; stop(true)) } 
     376 
     377        log(:notice, "Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart).") 
    377378      else 
    378         log "Signals ready.  INT => stop (no restart)." 
    379       end 
    380     end 
    381  
    382     # Logs a simple message to STDERR (or the mongrel log if in daemon mode). 
    383     def log(msg) 
    384       STDERR.print "#{Time.now.httpdate}: #{msg}\n" 
     379        log(:notice, "Signals ready.  INT => stop (no restart).") 
     380      end 
    385381    end 
    386382 
    387383  end 
     384 
    388385end 
  • trunk/lib/mongrel/const.rb

    r886 r899  
    111111    HOST = "HOST".freeze 
    112112  end 
     113 
    113114end 
  • trunk/lib/mongrel/debug.rb

    r889 r899  
    8787    MongrelDbg::trace(:files, open_counts.to_yaml) 
    8888  end 
    89 end   
    90  
     89end 
    9190 
    9291 
    9392module RequestLog 
    9493 
    95   # Just logs whatever requests it gets to STDERR (which ends up in the mongrel 
    96   # log when daemonized). 
    9794  class Access < GemPlugin::Plugin "/handlers" 
    9895    include Mongrel::HttpHandlerPlugin 
     
    10097    def process(request,response) 
    10198      p = request.params 
    102       STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" 
     99      #STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" 
     100      log(:info, "#{p['REMOTE_ADDR']} \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"") 
    103101    end 
    104102  end 
     
    114112     
    115113  end 
     114 
    116115 
    117116  # stolen from Robert Klemme 
     
    126125          ObjectSpace.each_object do |o|  
    127126            begin 
    128               if o.respond_to? :length 
     127              if o.respond_to?(:length) 
    129128                len = o.length 
    130129                lengths[o.class] ||= Mongrel::Stats.new(o.class) 
     
    160159  end 
    161160 
     161 
    162162  class Params < GemPlugin::Plugin "/handlers" 
    163163    include Mongrel::HttpHandlerPlugin 
     
    169169 
    170170  end 
     171 
    171172 
    172173  class Threads < GemPlugin::Plugin "/handlers" 
  • trunk/projects/mongrel_cluster/lib/mongrel_cluster/init.rb

    r889 r899  
    1414      @valid 
    1515    end 
    16        
     16     
    1717    def read_options 
    1818      @options = {  
     
    9797        log_verbose exec_cmd 
    9898        output = `#{exec_cmd}` 
    99         log_error output unless $?.success? 
     99        log(:error, output) unless $?.success? 
    100100      end 
    101101    end 
     
    126126        log_verbose exec_cmd 
    127127        output = `#{exec_cmd}` 
    128         log_error output unless $?.success? 
     128        log(:error, output) unless $?.success? 
    129129         
    130130      end 
     
    139139        pid = check_process(port)         
    140140        unless pid_file_exists?(port)         
    141           log "missing pid_file: #{port_pid_file(port)}"   
     141          log(:error, "missing pid_file: #{port_pid_file(port)}") 
    142142          status = STATUS_ERROR 
    143143        else 
    144           log "found pid_file: #{port_pid_file(port)}" 
     144          log(:info, "found pid_file: #{port_pid_file(port)}") 
    145145        end     
    146146        if pid 
    147           log "found mongrel_rails: port #{port}, pid #{pid}" 
     147          log(:info, "found mongrel_rails: port #{port}, pid #{pid}") 
    148148        else 
    149           log "missing mongrel_rails: port #{port}" 
     149          log(:error, "missing mongrel_rails: port #{port}") 
    150150          status = STATUS_ERROR 
    151151        end 
    152         puts "" 
     152        log(:info, "") 
    153153      end 
    154154 
     
    212212    end 
    213213     
    214     def log_error(message) 
    215       log(message) 
    216     end 
    217  
    218214    def log_verbose(message) 
    219       log(message) if @verbose 
    220     end 
    221  
    222     def log(message) 
    223       puts("#{Time.now.httpdate}: #{message}") 
    224     end    
    225   end 
     215      log(:info, message) if @verbose 
     216    end 
     217 
     218  end 
     219 
    226220  class Start < GemPlugin::Plugin "/commands" 
    227221    include ExecBase