| 1 | package FirePHP::Log4perl::Appender; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | FirePHP::Log4perl::Appender | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | In your C<Log::Log4perl> config: | ||
| 13 | |||
| 14 | log4perl.rootLogger = DEBUG, FIREPHP | ||
| 15 | |||
| 16 | log4perl.appender.FIREPHP = FirePHP::Appender | ||
| 17 | log4perl.appender.FIREPHP.layout = FirePHP::Layout | ||
| 18 | log4perl.appender.FIREPHP.layout.ConversionPattern = [ %c ] %m | ||
| 19 | |||
| 20 | In the dispatcher of your application: | ||
| 21 | |||
| 22 | my $appender = first { $_->isa( 'FirePHP::Log4perl::Appender' ) } | ||
| 23 | map{ $_->{appender} } values %{ Log::Log4perl->appenders() }; | ||
| 24 | local $appender->{fire_php} = | ||
| 25 | FirePHP::Dispatcher->new( $your_http_headers_compatile_object ); | ||
| 26 | |||
| 27 | # the normal dispatch stuff | ||
| 28 | |||
| 29 | $appender->fire_php->finalize; | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This is a very simple appender for writing to a FirePHP console. | ||
| 34 | Nontheless it is not easy to use because of scoping and threading | ||
| 35 | issues because L<Log::Log4perl> normally just provides an application | ||
| 36 | wide appender object that has no access whatsoever to the L<HTTP::Headers> | ||
| 37 | object of the current object. Your best bet is to use a plugin that is | ||
| 38 | spezialised for your framework. | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | use base qw/Log::Log4perl::Appender/; | ||
| 43 | |||
| 44 | use Carp; | ||
| 45 | use Scalar::Util qw/looks_like_number blessed weaken/; | ||
| 46 | use Data::Dump qw/dump/; | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 $class->new( %options ) | ||
| 51 | |||
| 52 | This creates a new, unassociated appender object. Most of the time | ||
| 53 | you do not want to bind to a L<FirePHP::Dispatcher> object during | ||
| 54 | creation but directly set (and un-set) it in the appropriate time | ||
| 55 | during your application life-cycle. | ||
| 56 | |||
| 57 | Returns: a new C<FirePHP::Log4perl::Appender> object | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $class = shift; | ||
| 64 | my $self = bless { @_ }, $class; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | =head2 $self->fire_php | ||
| 69 | |||
| 70 | Returns: the associated L<FirePHP::Dispatcher> or C<undef> | ||
| 71 | if the appender is not bound to one | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | sub fire_php { | ||
| 76 | my $self = shift; | ||
| 77 | return $self->{fire_php}; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | =head2 $self->log( %params ) | ||
| 82 | |||
| 83 | Used by L<Log::Log4perl::Appender> to send messages | ||
| 84 | to the FirePHP console | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | sub log { | ||
| 89 | my ( $self, %p ) = @_; | ||
| 90 | return unless $self->fire_php; | ||
| 91 | $self->fire_php->send_headers( $p{message} ); | ||
| 92 | return 1; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | __END__ |