Monday, February 16, 2009

Logging in Ramaze

I probably should have tossed this one into yesterday's post on models, but didn't think about it at the time. With Ramaze you can print out debug messages to the console in the same way that Ramaze itself does. Here's the authorize() with some additional lines of code of the form Ramaze::Log.debug "Entering authorize()".

# This class is a very simple authorize class that
# contains a single class method that checks a
# login/password pair and if they are hello/world
# returns authorized as true and otherwise false.
class Authorize
def self.authorize(login, password)
Ramaze::Log.debug "Entering authorize()"
if ((login == 'hello') && (password == 'world'))
Ramaze::Log.debug "authorize(): password validated"
authorized = true
Ramaze::Log.debug "authorize(): password not validated"
authorized = false

There are also a couple of other forms of this type of message for warning messages Ramaze::Log.warn "Warning message" and "Informational message". The code above will print out a debug message when we enter the authorize() method and then another when we decide to authorize or not.

You can turn off various messages (info, warning, and debug) using the following, Ramaze::Log.ignored_tags = [:info, :debug]. This particular piece will turn off the info and debug messages. You can add :warn if you want everything turned off or remove :info or :debug.

No comments:

Post a Comment