Xav
Cameron
I've written a rails log parser, but its crude and only reads logs
from a fairly specific version of rails.
What I've done in the past is have a separate log (I called it an audit log).
The output was tab delimited and in a machine readable format, with fields like
time of event in epoch seconds
the component (maybe UsersController)
an event label (maybe user_archived)
then some event specific parameters (user x, user y, etc)
Then reading the logs back later for analysis or, say auditing is very easy
I'd unit test and implement it as its own specific class. From the
components which use it, mock up your audit logger's interface
You could implement it as a singleton:
class Audit
def self.log(event,*args)
...
end
end
call it in your code like
Audit.log(:user_archived,user_x.id,user_y.id)
would that work?
> I could hook up something ugly by setting up a fake logger and
> checking if it's empty after running a block of code... but I was
> hoping for something more elegent.
>
> Any clues?
> Anyone done this before?
> Anyone think it's a stupid idea (and willing to share why)?
>
> Taryn
> >
>
--
Lachie
http://blog.smartbomb.com.au
http://www.flickr.com/photos/lachie/
Cam
> Does anyone know of a way to test whether or not a log event actually
> made it into the log?
> We want to make sure that some of our important events (eg "user x was
> archived by user y") actually make it into the log.
If you're using rspec, you can grab my Not A Mock plugin and use call
recording to track calls to the logger, something like:
it "should log correctly" do
RAILS_DEFAULT_LOGGER.track_method(:info)
# Run your test
RAILS_DEFAULT_LOGGER.should have_received(:info).with(...)
end
More info on Not A Mock, including installation instructions are here: http://notahat.com/not_a_mock
- Pete
Yeah, if you want to test the normal logger, use a mock
I'm using RR at the moment. I'd do it like
mock(your_object.logger).debug("the expected message")
If your_object.logger doesn't receive the debug message with "the
expected message" the test will FAIL.
other mocking libs can do the same thing, you'll just have to look up the doco
For test unit, I'd recommend either RR or Mocha (Mocha being the more mature)
> Cam
>
>
>
>
> On 04/03/2008, at 3:46 PM, taryn wrote:
>
> >
> >
> > On Mar 4, 3:42 pm, Cameron Barrie <pho...@mvpaustralia.com.au> wrote:
> >> Could you check if the logger.class was sent a method such as info or
> >> error?
> >
> > Not sure how I'd do that. Do you know?
> >
> > >
>
>
> >
>
--
Mock objects.
require 'mocha'
class MockLogTest < Test::Unit::TestCase
def test_will_pass
RAILS_DEFAULT_LOGGER.expects(:info).at_least_once.with("something funny")
RAILS_DEFAULT_LOGGER.info("something funny")
end
def test_will_fail
RAILS_DEFAULT_LOGGER.expects(:info).at_least_once.with("something funny")
RAILS_DEFAULT_LOGGER.info("something else")
end
end
- Matt