Skip to content
Apr 13 / Derek Reeve

Formatting the Rails logger

I’ve always been somewhat annoyed that Rails 2 doesn’t have a way to specify the log formatting. I would prefer that log messages include the logging level, for example.

Thanks to a post on Stack Overflow, I realized it’s pretty simple to override BufferedLogger.add to do what I want:

module ActiveSupport
  class BufferedLogger
    def add(severity, message = nil, progname = nil, &block)
      return if @level > severity
      message = (message || (block && block.call) || progname).to_s

      level = {
        0 => "DEBUG",
        1 => "INFO",
        2 => "WARN",
        3 => "ERROR",
        4 => "FATAL"
      }[severity] || "U"

      message = "[%s] %-6s%s" % [Time.now.strftime("%Y-%m-%d %H:%M:%S"), level,
                                 message]

      message = "#{message}\n" unless message[-1] == ?\n
      buffer << message
      auto_flush
      message
    end
  end
end

Adding this to environment.rb will cause the logger to print log messages in the format

[2010-04-13 11:55:20] DEBUG monkeys
Jan 18 / Derek Reeve

Shared command history with zsh

In addition to intelligent word boundaries, another neat feature I came across with zsh was shared command history. This can be useful if, for example, you use GNU Screen and often open new shells but don’t want to lose your previous command history.

This is as simple as adding

setopt share_history
HISTSIZE=1000
SAVEHIST=1000
HISTFILE=~/.history
setopt APPEND_HISTORY

to your ~/.zshrc. Running a new shell should automatically start with the history from the shell you were previously using!

Update: Wordpress crammed some of the options together, so I fixed it.

Dec 27 / Derek Reeve

Intelligent word boundaries in zsh

A couple years ago, after several years of constant shell usage, I found myself wanting a more efficient way to move backward through paths.  As I work from the shell, I undoubtedly end up working with a long path, a portion of which I need to use more than once. Take, for example, a Ruby on Rails project:

dreeve@puggle ~/projects/ $ ls cdreaper2/app/views/layouts

To run ls on another directory (cdreaper2/app/controllers, for example) you would normally have to backspace all the way to the shared parent directory. This is cumbersome for me: I hate holding down the backspace key. bash helps alleviate the overuse of the backspace key by allowing the user to use ctrl+w to clear entire words. However, it isn’t easy to configure the bash shell to clear words up to a forward slash.

So, I started using zsh instead. Since it’s already installed in OS X, you can set your user’s shell to zsh quite easily via the Accounts interface.

Adding

local WORDCHARS=${WORDCHARS//\//}

to your .zshrc file (found or created in your home directory) will tell zsh to use forward slash as a boundary character.

Then, when you have to move backward through a path, ctrl+w will remove the rightmost part of the path. Given the path we started with:

dreeve@puggle ~/projects $ ls cdreaper2/app/views/layouts |

Hitting ctrl+w twice will give you:

dreeve@puggle ~/projects $ ls cdreaper2/app/ |

making moving between directories in a heavily-nested projected easy.