Module: WEBrick::AccessLog (Ruby 2.2.2)
Description: Module : WEBrick::AccessLog - Ruby 2.2.2

- Home
- Core 2.2.2
- Std-lib 2.2.2
- Downloads

Support for the Ruby 2.2 series has ended. See here for reference.

### Home Classes Methods

### In Files

- webrick/accesslog.rb
- webrick/httpserver.rb

### Namespace

- CLASS WEBrick::AccessLog::AccessLogError

### Methods

- #escape
- #format
- #setup_params

### Class/Module Index

Quicksearch

- Dir
- Errno
- Errno::ECONNABORTED
- Errno::ECONNRESET
- Errno::EPROTO
- File
- Object
- WEBrick
- WEBrick::AccessLog
- WEBrick::AccessLog::AccessLogError
- WEBrick::BasicLog
- WEBrick::CGI
- WEBrick::Config
- WEBrick::Config::OpenSSL
- WEBrick::Cookie
- WEBrick::Daemon
- WEBrick::GenericServer
- WEBrick::HTMLUtils
- WEBrick::HTTPAuth
- WEBrick::HTTPAuth::Authenticator
- WEBrick::HTTPAuth::BasicAuth
- WEBrick::HTTPAuth::DigestAuth
- WEBrick::HTTPAuth::Htdigest
- WEBrick::HTTPAuth::Htgroup
- WEBrick::HTTPAuth::Htpasswd
- WEBrick::HTTPAuth::ProxyAuthenticator
- WEBrick::HTTPAuth::ProxyBasicAuth
- WEBrick::HTTPAuth::ProxyDigestAuth
- WEBrick::HTTPAuth::UserDB
- WEBrick::HTTPProxyServer
- WEBrick::HTTPRequest
- WEBrick::HTTPResponse
- WEBrick::HTTPServer
- WEBrick::HTTPServerError
- WEBrick::HTTPServlet
- WEBrick::HTTPServlet::AbstractServlet
- WEBrick::HTTPServlet::CGIHandler
- WEBrick::HTTPServlet::DefaultFileHandler
- WEBrick::HTTPServlet::ERBHandler
- WEBrick::HTTPServlet::FileHandler
- WEBrick::HTTPServlet::HTTPServletError
- WEBrick::HTTPServlet::ProcHandler
- WEBrick::HTTPStatus
- WEBrick::HTTPStatus::ClientError
- WEBrick::HTTPStatus::EOFError
- WEBrick::HTTPStatus::Error
- WEBrick::HTTPStatus::Info
- WEBrick::HTTPStatus::Redirect
- WEBrick::HTTPStatus::ServerError
- WEBrick::HTTPStatus::Status
- WEBrick::HTTPStatus::Success
- WEBrick::HTTPUtils
- WEBrick::HTTPUtils::FormData
- WEBrick::HTTPVersion
- WEBrick::Log
- WEBrick::ServerError
- WEBrick::SimpleServer
- WEBrick::Utils
- WEBrick::Utils::TimeoutHandler
- WEBrick::Utils::TimeoutHandler::Thread

No matching classes.

# WEBrick::AccessLog

AccessLog provides logging to various files in various formats.

Multiple logs may be written to at the same time:

    access_log = [
      [$stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT],
      [$stderr, WEBrick::AccessLog::REFERER_LOG_FORMAT],
    ]

    server = WEBrick::HTTPServer.new :AccessLog => access_log

Custom log formats may be defined. WEBrick::AccessLog provides a subset of the formatting from Apache's mod_log_config httpd.apache.org/docs/mod/mod_log_config.html#formats. See AccessLog::setup_params for a list of supported options

### Constants

AGENT_LOG_FORMAT

  User-Agent Log Format

CLF

  Short alias for Common Log Format

CLF_TIME_FORMAT

  The Common Log Format's time format

COMBINED_LOG_FORMAT

  Combined Log Format

COMMON_LOG_FORMAT

  Common Log Format

REFERER_LOG_FORMAT

  Referer Log Format

### Public Instance Methods

escape(data) click to toggle source

Escapes control characters in `data`

                   # File webrick/accesslog.rb, line 150
    def escape(data)
      if data.tainted?
        data.gsub(/[[:cntrl:]\\]+/) {$&.dump[1...-1]}.untaint
      else
        data
      end
    end

format(format_string, params) click to toggle source

Formats `params` according to `format_string` which is described in setup_params.

                   # File webrick/accesslog.rb, line 122
    def format(format_string, params)
      format_string.gsub(/\%(?:\{(.*?)\})?>?([a-zA-Z%])/){
         param, spec = $1, $2
         case spec[0]
         when ?e, ?i, ?n, ?o
           raise AccessLogError,
             "parameter is required for \"#{spec}\"" unless param
           (param = params[spec][param]) ? escape(param) : "-"
         when ?t
           params[spec].strftime(param || CLF_TIME_FORMAT)
         when ?p
           case param
           when 'remote'
             escape(params["i"].peeraddr[1].to_s)
           else
             escape(params["p"].to_s)
           end
         when ?%
           "%"
         else
           escape(params[spec].to_s)
         end
      }
    end

setup_params(config, req, res) click to toggle source

This format specification is a subset of mod_log_config of Apache:

%a

  Remote IP address

%b

  Total response size

%e{variable}

  Given variable in ENV

%f

  Response filename

%h

  Remote host name

%{header}i

  Given request header

%l

  Remote logname, always “-”

%m

  Request method

%{attr}n

  Given request attribute from `req.attributes`

%{header}o

  Given response header

%p

  Server's request port

%{format}p

  The canonical port of the server serving the request or the actual port or the client's actual port. Valid formats are canonical, local or remote.

%q

  Request query string

%r

  First line of the request

%s

  Request status

%t

  Time the request was received

%T

  Time taken to process the request

%u

  Remote user from auth

%U

  Unparsed URI

%%

  Literal %

                   # File webrick/accesslog.rb, line 94
    def setup_params(config, req, res)
      params = Hash.new("")
      params["a"] = req.peeraddr[3]
      params["b"] = res.sent_size
      params["e"] = ENV
      params["f"] = res.filename || ""
      params["h"] = req.peeraddr[2]
      params["i"] = req
      params["l"] = "-"
      params["m"] = req.request_method
      params["n"] = req.attributes
      params["o"] = res
      params["p"] = req.port
      params["q"] = req.query_string
      params["r"] = req.request_line.sub(/\x0d?\x0a\z/o, '')
      params["s"] = res.status       # won't support "%>s"
      params["t"] = req.request_time
      params["T"] = Time.now - req.request_time
      params["u"] = req.user || "-"
      params["U"] = req.unparsed_uri
      params["v"] = config[:ServerName]
      params
    end