Custom logger in Rails 6

Posted by bikethetam Wed, 05 Aug 2009 03:19:00 GMT

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"}

 


Fixing Intel driver in Jaunty on Acer Aspire One 4

Posted by bikethetam Sun, 12 Jul 2009 22:14:00 GMT

Since Jaunty was released, most computers with Intel GPU have suffered from dismall video performances. It's not only about videos, it's also about browsing the web with Firefox, scrolling through pages is slow, sometimes blocks, and compiz effects suffer from occasional flashes.

This fix is for the AAO D150 which is equipped with Intel 945GM.

# lspci  | grep 945
00:00.0 Host bridge: Intel Corporation Mobile 945GME Express Memory Controller Hub (rev 03)
00:02.0 VGA compatible controller: Intel Corporation Mobile 945GME Express Integrated Graphics Controller (rev 03)
00:02.1 Display controller: Intel Corporation Mobile 945GM/GMS/GME, 943/940GML Express Integrated Graphics Controller (rev 03)

I tried several things. I started experimenting with the instructions at Jaunty Intel Graphics Performances Guide (safe solution) but it didn't help, if anything it got worse.

Then I reverted to the regular driver (aptitude was of great help here, I used "aptitude remove xserver-xorg-video-intel" and it kindly offered me to downgrade to the regular jaunty version instead of completely removing the package).

I found that page that explains how to revert to version 2.4 of the driver.  The instructions in the article work just fine and after restarting X, the video performances are back to normal. In short:

Add to /etc/apt-sources.list:

deb http://ppa.launchpad.net/siretart/ppa/ubuntu jaunty main

Add the server key:

sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xce90d8983e731f79

Install the 2.4 package:

sudo aptitude install xserver-xorg-video-intel-2.4

Restart X and you're done.


Ubuntu Firefox 3.5 install: ubuntu-mozilla-security 23

Posted by bikethetam Fri, 03 Jul 2009 17:28:00 GMT

Important update: The official Firefox-3.5 is now available in the universe repositories. You don't need to do any of the following, just "apt-get install firefox-3.5". No need for any ubuntu-mozilla-* line in /etc/apt/sources.list.

To uninstall firefox-3.5 that was setup using ubuntu-mozilla-*, follow the removal instructions below.

 

After digging around a little bit more, I found that there was another, safer, repository for Jaunty with firefox 3.5: ubuntu-mozilla-security.

For those of you who don't want the daily updates of firefox, which will deliver experimental releases on occasions, you should use the security repository instead of ubuntu-mozilla-daily.

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 7EBC211F

Add the following line at the bottom of /etc/apt/sources.list:
deb http://ppa.launchpad.net/ubuntu-mozilla-security/ppa/ubuntu jaunty main

sudo apt-get update
sudo apt-get install firefox-3.5 firefox-3.5-gnome-support

If had already installed firefox-3.5 from the daily repository and you want to switch to the security one, you first need to remove the ubuntu-mozilla-daily line from /etc/apt/sources.list and remove firefox-3.5 that came from that repository:

sudo apt-get remove firefox-3.5 xulrunner-1.9.1-gnome-support xulrunner-1.9.1

Only after that you should run "apt-get install firefox-3.5 firefox-3.5-gnome-support ".

 


Installing Firefox 3.5 the right way in Ubuntu Jaunty 56

Posted by bikethetam Thu, 02 Jul 2009 03:45:00 GMT

Important update: The official Firefox-3.5 is now available in the universe repositories. You don't need to do any of the following, just "apt-get install firefox-3.5". No need for any ubuntu-mozilla-* line in /etc/apt/sources.list.

To uninstall firefox-3.5 that was setup using ubuntu-mozilla-*, follow the instructions at ubuntu-mozilla-security.

Only follow the instructions below if you want to be on the bleeding edge.

 

Firefox 3.5 was officially released yesterday and it brings significant improvements in terms of speed, tab management and support of HTML5. Ubuntu does not automatically propose the upgrade so you need to help the system find the newer packages. Let's start by listing the least effective ways of getting those packages.

Installing from Canonical repositories

When searching inside of Synaptic Package Manager or using apt-cache, you are going to find firefox-3.5. Unfortunately, it's been there since the release of Jaunty and it has not been updated since then. At the moment, only the beta 4 for is available and believe me you don't want it.

$ apt-cache showpkg firefox-3.5
Package: firefox-3.5
Versions:
3.5~b4~hg20090330r24021+nobinonly-0ubuntu1 (/var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_jaunty_universe_binary-i386_Packages)
 Description Language:
                 File: /var/lib/apt/lists/us.archive.ubuntu.com_ubuntu_dists_jaunty_universe_binary-i386_Packages
                  MD5: b670b07084b5a79b912d14c4307acda4

Installing from mozilla.org

That's a better option. The process is the following:

  1. download the .tar.bz2 file
  2. uncompress it under ~/firefox
  3. make sure no other instance is already running
  4. start firefox 3.5 by running "~/firefox/firefox"

It works fine but there are several problems with this approach:

  • It completely shortcircuits the package manager which means that you will have to install future updates independently of the rest of the system.
  • When Ubuntu gets upgraded, it's very likely this firefox installation will stop working as it depends on libraries that will have changed version.
  • This firefox installation will the same ~/.mozilla directory to store your personal settings as the Ubuntu version of Firefox (3.0.11 as of today). Confusion and pain will inevitably follow. Unfortunately the last installation option described below has that same disadvantage.

 

Now the better solution.

Setting up the ubuntu-mozilla-daily repository

You can add to your system a repository maintained by the mozilla developers.

Add the following line at the bottom of /etc/apt/sources.list:

deb http://ppa.launchpad.net/ubuntu-mozilla-daily/ppa/ubuntu jaunty main

Let Ubuntu know the identification key of this repository:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 247510BE

Refresh your system cache:

sudo apt-get update

Now, same as with the first option, the package that you want is firefox-3.5. The difference is that now, the version available is the right one.

$ apt-cache showpkg firefox-3.5
Package: firefox-3.5
Versions:
3.5.1~hg20090629r26036+nobinonly-0ubuntu2~umd1~jaunty (/var/lib/apt/lists/ppa.launchpad.net_ubuntu-mozilla-daily_ppa_ubuntu_dists_jaunty_main_binary-i386_Packages)
 Description Language:
                 File: /var/lib/apt/lists/ppa.launchpad.net_ubuntu-mozilla-daily_ppa_ubuntu_dists_jaunty_main_binary-i386_Packages
                  MD5: b670b07084b5a79b912d14c4307acda4

You need to setup this package, latex-xft-fonts and also firefox-3.5-gnome-support

$ apt-get install firefox-3.5 firefox-3.5-gnome-support latex-xft-fonts

At this point, there are 2 different versions of Firefox coexisting on the system, 3.0.11 and 3.5.1. When you run "firefox" from the command line or when you select it from the Applications menu, version 3.0.11 is fired up. It might be convenient to make the newer version the default:

$ sudo su
$ cd /usr/bin
$ ll firefox*
lrwxrwxrwx 1 root root 11 2009-06-22 16:45 firefox -> firefox-3.0
lrwxrwxrwx 1 root root 32 2009-06-22 16:45 firefox-3.0 -> ../lib/firefox-3.0.11/firefox.sh
lrwxrwxrwx 1 root root 34 2009-07-01 21:17 firefox-3.5 -> ../lib/firefox-3.5.1pre/firefox.sh
$ rm firefox
$ ln -s firefox-3.5 firefox

The newly installed version is branded "Shiretoko". That's the name used to identify 3.5 during development.

By using this technique, you're guaranteed that Firefox will get updated regularly through the Ubuntu package manager and that compatibility with system libraries will be preserved.

The downside of this is that you will get a little more updates than the average users: fixes to 3.5 will be dropped here first before they are made available to all. As a consequence, it's not impossible that you will be among the first to find regression bugs.

Reference: http://www.asoftsite.org/s9y/archives/160-FAQ-Where-can-I-get-firefox-3.5-for-Ubuntu.html

Update 7/3/2009: safer ubuntu-mozilla-security repository?