[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] Re: ANNOUNCE: CGI::Application::Plugin::LogDispatch


> > Personally, I'd add a few changes:
> >
> > 1) Create a param(), not a direct object access. So, instead of
> > $self->{logger}, I'd use $self->param( 'logger' );
> > 2) Create a logger() method that looks like:
> > sub logger { $_[0]->param( 'logger' ) }
> >
> > So, now you have
> >
> > $self->logger->error( "Uh-oh" );
> >
> > The overhead is very small compared with the maintenance gains.
> 
> Always hoping to gain more knowledge, what are the maintenance gains here?

The first is that you're not assuming anything about how the C::A
object is represented. This is a huge gain. Let's say Jesse or Mark
wants to change the C::A object to be based on an array vs. a hash.
Or, maybe Jesse goes completely nuts and bases C::A on
Class::Prototyped. $self->{logger} now breaks just because you
upgraded your C::A version. Uh-oh. So, that's why you want to use the
param() method - it's provided for -your- safety.

Second - you want to provide a logger() method because you want a
child-class to be able to provide its own logger, if it so chooses,
without overriding the logger stored in param(). Maybe it wants to add
some specific messages. Maybe it wants to log to more than one place.

Actually, now that I'm thinking about it, you really want to have a
log_error() method that looks something like:

sub log_error { (+shift)->param( 'logger' )->error( @_ ) }

That way, children can do something like:

sub log_error { (+shift)->param( 'logger' )->error( "Called from
child:\n", @_ ) }

If they so choose. And, you can auto-generate those methods doing
something like:

my @methods = qw( error info warning ); # Or whatever they're called
INIT {
    foreach my $meth (@methods)
    {
        my $full_name = __PACKAGE__ . '::' . $meth;

        no strict 'refs';

        next if defined \&{$full_name};

        my $log_meth = "log_$meth";
        *$fullname = sub { (+shift)->param( 'logger' )->$log_meth( @_ ) };
    }
}

Yes, that uses soft references. However, it does so for a good reason.
:-) And, you can put that junk in some parent class or wherever and it
will do the right thing.

Rob

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.