Custom logger in Rails 2
The default logging format in Rails lacks timestamp and log level, this is how configure logging to the format of your preference.
In Rail 1.2.X, it was enough to just redefine the format_message of the Logger class by just inserting the following lines at the bottom of config/environment.rb:
# custom logging format
class Logger
private
def format_message(severity, timestamp, progname, msg)
"[%5s %s] %s\n" % [severity,
timestamp.strftime("%m/%d %H:%M:%S"),
msg]
end
end
No longer in Rails 2.X as the logger object is now an instance of the ActiveSupport::BufferedLogger class. A bit of monkey patching is needed. Create a new file called custom_logger.rb in the lib directory:
require 'active_support'
# Logger class for custom logging format
class CustomLogger < ActiveSupport::BufferedLogger
private
# CustomLogger doesn't define strings for log levels
# so we have to do it ourselves
def severity_string(level)
case level
when DEBUG
:DEBUG
when INFO
:INFO
when WARN
:WARN
when ERROR
:ERROR
when FATAL
:FATAL
else
:UNKNOWN
end
end
public
# monkey patch the CustomLogger add method so that
# we can format the log messages the way we want
def add(severity, message = nil, progname = nil, &block)
return if @level > severity
message = (message || (block && block.call) || progname).to_s
# If a newline is necessary then create a new message ending with a newline.
# Ensures that the original message is not mutated.
message = "[%5s %s] %s\n" % [severity_string(severity),
Time.now.strftime("%m/%d %H:%M:%S"),
message] unless message[-1] == ?\n
buffer << message
auto_flush
message
end
end
And modify environment.rb the following way:
require File.join(File.dirname(__FILE__), 'boot')
require 'custom_logger'
Rails::Initializer.run do |config|
...
# Force all environments to use the same logger level
# (by default production uses :info, the others :debug)
config.log_level = ENV['RAILS_ENV']=='production' ?
ActiveSupport::BufferedLogger::Severity::INFO :
ActiveSupport::BufferedLogger::Severity::DEBUG
# initializing custom logger
config.logger = CustomLogger.new(config.log_path, config.log_level)
end
That does 2 things:
- if running in production mode, no message with a level lower than INFO will be printed
- the format of every log message will include level, timestamp and message
Example:
[ INFO 08/04 20:34:41] Session ID: 78e4de86b30d6d704a9f6f44717a4288
[ INFO 08/04 20:34:41] Parameters: {"action"=>"buy", "controller"=>"article"}
VPS for RoR review
I’ve been wanting to try one one those Virtual Private Server hosting solutions for some time. Today was rainy day, I consequently spent the day on it. I started my search on that railshostinginfo.com and resolved to start my investigation with railsplayground.com. That company offers the cheapest credible deal and has a good reputation on the rails-talk mailing list. The price is $15 per month with a 12 months commitment for 256MB memory (up to 512MB spikes, what that means exactly I don’t know), 10GB disk space, 100GB. bandwitdh.
Getting started: 5 minutes after I entered my CC number, the server was up and running.
I went for the Ubuntu 8.04 server since this is distro I know well.
First impressions:
- I logged in with ssh, the server is very responsive, not the slightest delay when typing.
- Nice web console that allows you to track the status of your server.
- As specified, 10GB disk is available.
- The Ubuntu setup only has the bare essentials, no mysql, no apache, no compiler, no ruby.
- The internet connection appears to be lightning fast.
total used free shared buffers cached Mem: 524288 17860 506428 0 0 0 -/+ buffers/cache: 17860 506428 Swap: 0 0 0That means 512MB available, but don’t think about relying on swap space.
Setup:
- apt is setup with only the main repository without access to updates, you’ll need to add the universe to /etc/apt/sources.list:
deb http://us.archive.ubuntu.com/ubuntu/ hardy-updates main restricted deb http://us.archive.ubuntu.com/ubuntu/ hardy universe
- “apt-get update” to get your local cache up-to-date
- “apt-get -u upgrade” to upgrade everything
- mysql-server
- ruby
- build-essential
- wget
- rdoc
- ruby1.8-dev
- libmagick10
- libmagick9-dev
- irb
- libfcgi-ruby1.8
- libmysqlclient15-dev
- libopenssl-ruby
- rails
- gruff
- mongrel
- willpaginate
- mysql
- rmagick
Web server I naturally started with Apache that I intended to run with mod_fcgid. But, wow, right after setup with all the default options, Apache takes 250MB. It’s a no go.
Mongrel is a good replacement except that it only gives you one process listening for client requests. On the other hand, the memory footprint is less than 40MB.
I decide to give a try to Lighttpd that I have never used before. That’s a winner, the instructions at http://wiki.rubyonrails.org/rails/pages/Lighttpd are a breeze and it uses just ~25MB per lighttpd process. I don’t need more than 2.
Overall I’m very satisfied, everything is running in less that 115 MB, that leaves 385MB of memory and 9G of disk to spare. My fear that the 256MB VSP plans were just teasers was not justified. I originally just bought one month but I think I’m going to convert that to an annual subscription.