Ruby Singleton Logger

I have been working on a module lately that parses a XML file structure into a hash and writes it to a database. The module includes some classes and methods and I needed a central logger class.

I created a class that inherits from Ruby´s Logger class.

class MyLogger < Logger
end

I decided to make this class a Singleton. Singleton is a design pattern that restricts instantiation of a class to only one instance that is globally available. This can e.g. be done by simply including the Singleton Module.

class MyLogger < Logger
  include Singleton
end

I wanted this class to be able to either log to STDOUT or a file. The standard way to do this, is to pass the log device to the Logger class during instantiation. The problem is that the singleton module creates the singleton instance before it is actually needed, called eager instantiation. In order to tell the singleton instance to which log device it should log to, we need to set a member variable in the initialize method. Secondly we need a method to reassign the log device.

class MyLogger < Logger
  include Singleton

  LOG_FILE = File.open("myfile.log","a")

  def initialize
    @logdev = Logger::LogDevice.new(LOG_FILE)
    @level = INFO
  end

  def change_logdev_to_stdout
    @logdev = Logger::LogDevice.new(STDOUT)
  end

end

I could now use the logger methods from everywhere in my code, e.g.:

MyModule::MyLogger.instance.info("Hello World!")

In order to make things a little bit shorter, I added an additional method to the module:

def self.logger
  return MyModule::MyLogger.instance
end

Logging can now been done from everywhere by calling:

MyModule::logger.info("Hello Universe!")

How to get rid of bundle exec

I you feel bored by entering bundle exec (a command that executes a command in the context of the current bundle) if you want to execute a rake task, the folloowing code could help to get rid of it:
bundle install --binstubs; export PATH="./bin:$PATH"

How to get rid of Bundler::GemNotFound error at domainfactory

Today I got stuck with an issue using Bundler at domainfactory for the first time. Although a gem was installed and bundle install ran fine, passenger reclaimed:
Could not find activesupport-2.3.11 in any 
of the sources (Bundler::GemNotFound)
Thanks to my friend Stack Overflow, I could get rid of this problem by running:
bundle install --path vendor/cache

render :action in :rails

Although the Rails Guides say it is a newcomers issue, I also get sometimes confused with the render command:

"Using render with :action is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does not run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling render."

Installing MySQL and mysql gem on Snow Leopard

It´s really a hard job figuring out how to get MySQL and mysql gem up and running on Snow Leopard 10.6.2. I followed the instructions of various posts but was not successful yet:

First I followed this instruction http://hivelogic.com/articles/compiling-mysql-on-snow-leopard/" and build MySQL Version 5.1.39 from source. It installed successfully. When trying to login using (mysql -u root -p) mysql returned the following error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2), that obviously meant that the MySQL Server (mysqld) was not running (ps -axf | grep mysqld).

which mysql: /usr/local/mysql/bin/mysql
which ruby: ruby 1.8.7 (2009-04-08 patchlevel 160) [i686-darwin9])
gem -v: 1.3.5
mysql: Server version: 5.1.39 MySQL Community Server (GPL)

After a lot of googling around, I found out that

1. this command can start mysqld:
sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

2. The Preference Pane MySQL Tool is probably broken in Mac OS X 10.5 and higher

3. This command should install the mysql gem correctly on Snow Leopard:
sudo gem uninstall mysql
sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
(See this post: http://stackoverflow.com/questions/1366746/gem-install-mysql-failure-in-snow-leopard

It ended up that I got the following error using something like rake db:create:
dyld: lazy symbol binding failed: Symbol not found: _mysql_init Referenced from: /opt/local/lib/ruby/gems/1.8/gems/mysql-2.8.1/lib/mysql_api.bundle Expected in: flat namespace dyld: Symbol not found: _mysql_init Referenced from: /opt/local/lib/ruby/gems/1.8/gems/mysql-2.8.1/lib/mysql_api.bundle Expected in: flat namespace

This error led me to the following post: http://cho.hapgoods.com/wordpress/?p=158, that basically tells me that the Ruby Version that came together with XCode 3.0 does not work in 64bit environments and a solution could be to install the 32bit version. Arrrgghh!

The first answer to my post @ stackoverflowled me to the right track, doing the stuff with macports.

The way I finally solved this issue was uninstalling / reinstalling XCode and all my ports as described here:
http://trac.macports.org/wiki/Migration

Ruby, Rails & CMS

Excellent article from Aaron Longwell about the Ruby on Rails CMS dilemma: http://aaronlongwell.com/2009/06/the-ruby-on-rails-cms-dilemma.html