pvgoran
pvgoran

Reputation: 460

How to change WEBrick :AccessLog option when running RedMine?

I'm running RedMine via WEBrick using the following command line (simplified):

bundle exec rails server webrick -e production -p 3012 -P '/var/lib/redmine/redmine.pid'

I don't like how WEBrick outputs the peer address at the beginning of its access log lines (because I'm running it behind nginx and the peer address is always 127.0.0.1), so I want to change the access log format.

I know that I need to tune the :AccessLog config option for WEBrick, but I don't know how to get my hands on it. WEBrick is run by the rails server command, via the rack abstraction, and I don't see an obvious way to pass the necessary configuration to WEBrick.

So, is there any way to do it? Some command line switch? -c is the only switch that accepts some kind of configuration file, but it references "rackup", and I have no idea how to work with it.

Maybe it can be done by changing configuration files? I tried to modify additional_environment.rb by adding config[:AccessLog] = [ [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ] ], but it had no effect (although the file was executed), so I assume this file's config is not what is passed to WEBrick.

I'm pretty sure there is some way to configure this option without creating a new Rails application and invoking WEBrick manually, and hopefully even without changing RedMine files.

Upvotes: 0

Views: 433

Answers (2)

pvgoran
pvgoran

Reputation: 460

This answer is based on halfbit's answer, but it's specific to Redmine.

The :AccessLog config option for WEBrick (which I mentioned in the question) can be accessed from Redmine's additional_environment.rb configuration file (which is instance_eval'ed from Redmine's application.rb) via the expression WEBrick::Config::HTTP[:AccessLog]; however, it will not work until webrick is imported.

Here is what my configuration (put into additional_environment.rb) now looks like:

require 'webrick'
ourAccessLogger = Logger.new('/var/lib/redmine/log/access.log')
WEBrick::Config::HTTP[:AccessLog] = [
  [ ourAccessLogger, '%{X-Real-IP}i %l %u %t "%r" %s %b' ],
  [ ourAccessLogger, WEBrick::AccessLog::REFERER_LOG_FORMAT ]
]

A simpler configuration, the one I tried to use in the question, would be applied like this:

require 'webrick'
WEBrick::Config::HTTP[:AccessLog] = [ [ $stderr, WEBrick::AccessLog::COMMON_LOG_FORMAT ] ]

Upvotes: 1

halfbit
halfbit

Reputation: 3964

I am not sure if this hits your question, I have no idea of RedMine.

But:

With Rails you can access webrick with:

::WEBrick::Config::HTTP[:AccessLog] #leading :: depend on installation

I put this line in my application.rb

WEBrick::Config::HTTP[:AccessLog]=[[STDOUT, format_in_accesslog.rb ]]

like

WEBrick::Config::HTTP[:AccessLog]=[[Logger.new(STDOUT), "%h %l %u %t \"%r\" %s %b"]]    

in accesslog.rb you find

# 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 <tt>req.attributes</tt>
# %{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 %

The syntax is [[out, format],[out, format]]

Upvotes: 1

Related Questions