Changeset 379

Show
Ignore:
Timestamp:
12/18/01 20:39:27
Author:
miyagawa
Message:

changed API

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • Log-Dispatch-Config/trunk/Changes

    r377 r379  
    22 
    330.06 
     4        --- developer API change --- 
    45        * Added Log::Dispatch::Configurator and docs/tests for it 
    56          (Thanks to Matt Sergeant <matt@sergeant.org>) 
     
    910          (Thanks to Matt Sergeant <matt@sergeant.org>) 
    1011        - Deprecated ${XXX} style format 
     12        - Switched to fully qualified variable from use vars (for inheritance) 
    1113 
    12140.05  Thu Dec  6 19:05:11 JST 2001 
     
    1416 
    15170.04  Thu Dec  6 18:33:50 JST 2001 
    16         * --- API change (with backward compatibility) --- 
    17         Whole architecture redesign: now inherits from Log::Dispatch. 
     18        --- API change (with backward compatibility) --- 
     19        * Whole architecture redesign: now inherits from Log::Dispatch. 
    1820 
    19210.03  Tue Dec  4 11:33:15 JST 2001 
  • Log-Dispatch-Config/trunk/README

    r377 r379  
    5858      screen.format = %m 
    5959 
    60     In this example, config file is written in AppConfig format, see the 
    61     section on "PLUGGABLE CONFIGURATOR" for other config parsing scheme. 
     60    In this example, config file is written in AppConfig format. Using . ini 
     61    style config file is also okay. See the 
     62    Log::Dispatch::Configurator::AppConfig manpage for the details. 
     63 
     64    See the section on "PLUGGABLE CONFIGURATOR" for other config parsing 
     65    scheme. 
    6266 
    6367  GLOBAL PARAMETERS 
     
    8387 
    8488        Note that datetime (%d) format is configurable by passing "strftime" 
    85         fmt in braket after %d. 
     89        fmt in braket after %d. (I know it looks quite messy, but its 
     90        compatible with Java Log4j ;) 
    8691 
    8792          format = [%d{%Y%m%d}] %m  # datetime is now strftime "%Y%m%d" 
     
    136141 
    137142PLUGGABLE CONFIGURATOR 
    138     If you pass filename to "configure()" method call, this module handles 
    139     the config file with AppConfig. You can change config parsing scheme by 
     143    If you pass filename to "configure" method call, this module handles the 
     144    config file with AppConfig. You can change config parsing scheme by 
    140145    passing another pluggable configurator object. 
    141146 
     
    154159          } 
    155160 
    156     *   Implement three required object methods "global_format", 
    157         "dispatchers", "attrs". 
    158  
    159         "global_format" should return format string used in global 
    160         parameters. 
    161  
    162           sub global_format { 
     161    *   Implement two required object methods "get_attrs_global" and 
     162        "get_attrs". 
     163 
     164        "get_attrs_global" should return hash reference of global 
     165        parameters. "dispatchers" should be an array reference of names of 
     166        dispatchers. 
     167 
     168          sub get_attrs_global { 
    163169              my $self = shift; 
    164               return undef; 
    165           } 
    166  
    167         "dispatchers" should return list of names of dispatchers. 
    168  
    169           sub dispatchers { 
    170               my $self = shift; 
    171               return 'file', 'screen'; 
    172           } 
    173  
    174         "attrs" should accept name of dispatcher, and return hash reference 
    175         of parameters for the dispatcher. 
    176  
    177           sub attrs { 
     170              return { 
     171                  'format' => undef, 
     172                  dispatchers => [ qw(file screen) ], 
     173              }; 
     174          } 
     175 
     176        "get_attes" accepts name of a dispatcher and should return hash 
     177        reference of parameters associated with the dispatcher. 
     178 
     179          sub get_attrs { 
    178180              my($self, $name) = @_; 
    179181              if ($name eq 'file') { 
     
    185187                      'format'  => '[%d] [%p] %m at %F line %L%n', 
    186188                  }; 
    187               } elsif ($name eq 'screen') { 
     189              } 
     190              elsif ($name eq 'screen') { 
    188191                  return { 
    189192                      class     => 'Log::Dispatch::Screen', 
     
    193196                  }; 
    194197              } 
     198              else { 
     199                  die "invalid dispatcher name: $name"; 
     200              } 
    195201          } 
    196202 
    197203    *   Implement optional "needs_reload" and "reload" method. 
    198         "needs_reload" should return boolean value if the object is stale 
    199         and needs reloading itself. 
    200  
    201         Stub confif file mtime based "needs_reload" method is declared in 
     204        "needs_reload" accepts Log::Dispatch::Config instance and should 
     205        return boolean value if the object is stale and needs reloading 
     206        itself. 
     207 
     208        Stub config file mtime based "needs_reload" method is declared in 
    202209        Log::Dispatch::Configurator as below, so if your config class is 
    203210        based on filesystem files, you do not need to reimplement this. 
     
    208215          } 
    209216 
    210         "reload" method is called when "needs_reload" returns true. 
    211         Typically you should place configuration parsing again on "reload" 
    212         method, so Log::Dispatch::Configurator again declares stub "reload" 
    213         method that clones your object. 
     217        "reload" method is called when "needs_reload" returns true, and 
     218        should return new Configurator instance. Typically you should place 
     219        configuration parsing again on this method, so 
     220        Log::Dispatch::Configurator again declares stub "reload" method that 
     221        clones your object. 
    214222 
    215223          sub reload { 
     
    221229    *   Thats all. Now you can plug your own configurator (Hardwired) into 
    222230        Log::Dispatch::Config. What you should do is to pass configurator 
    223         object to "config()" method call instead of config file name. 
     231        object to "configure" method call instead of config file name. 
    224232 
    225233          use Log::Dispatch; 
     
    227235 
    228236          my $config = Log::Dispatch::Configurator::Hardwired->new; 
    229           Log::Dispatch::Config->confifure($config); 
     237          Log::Dispatch::Config->configure($config); 
    230238 
    231239TODO 
  • Log-Dispatch-Config/trunk/lib/Log/Dispatch/Config.pm

    r377 r379  
    6666    my($class, $config) = @_; 
    6767 
    68     my $callback = $class->format_to_cb($config->global_format, 3); 
     68    my $global = $config->get_attrs_global; 
     69    my $callback = $class->format_to_cb($global->{format}, 3); 
    6970    my %dispatchers; 
    70     foreach my $disp ($config->dispatchers) { 
     71    foreach my $disp (@{$global->{dispatchers}}) { 
    7172        $dispatchers{$disp} = $class->config_dispatcher( 
    72                 $disp, $config->attrs($disp), 
     73                $disp, $config->get_attrs($disp), 
    7374            ); 
    7475    } 
     
    217218  screen.format = %m 
    218219 
    219 In this example, config file is written in AppConfig format, see 
    220 L</"PLUGGABLE CONFIGURATOR"> for other config parsing scheme. 
     220In this example, config file is written in AppConfig format. Using . 
     221ini style config file is also okay. See 
     222L<Log::Dispatch::Configurator::AppConfig> for the details. 
     223 
     224See L</"PLUGGABLE CONFIGURATOR"> for other config parsing scheme. 
    221225 
    222226=head2 GLOBAL PARAMETERS 
     
    246250 
    247251Note that datetime (%d) format is configurable by passing C<strftime> 
    248 fmt in braket after %d. 
     252fmt in braket after %d. (I know it looks quite messy, but its 
     253compatible with Java Log4j ;) 
    249254 
    250255  format = [%d{%Y%m%d}] %m  # datetime is now strftime "%Y%m%d" 
     
    310315=head1 PLUGGABLE CONFIGURATOR 
    311316 
    312 If you pass filename to C<configure()> method call, this module 
    313 handles the config file with AppConfig. You can change config parsing 
    314 scheme by passing another pluggable configurator object. 
     317If you pass filename to C<configure> method call, this module handles 
     318the config file with AppConfig. You can change config parsing scheme 
     319by passing another pluggable configurator object. 
    315320 
    316321Here is a way to declare new configurator class. The example below is 
     
    333338=item * 
    334339 
    335 Implement three required object methods C<global_format>, 
    336 C<dispatchers>, C<attrs>. 
    337  
    338 C<global_format> should return format string used in global 
    339 parameters. 
    340  
    341   sub global_format
     340Implement two required object methods C<get_attrs_global> and 
     341C<get_attrs>. 
     342 
     343C<get_attrs_global> should return hash reference of global parameters. 
     344C<dispatchers> should be an array reference of names of dispatchers. 
     345 
     346  sub get_attrs_global
    342347      my $self = shift; 
    343       return undef; 
     348      return { 
     349          'format' => undef, 
     350          dispatchers => [ qw(file screen) ], 
     351      }; 
    344352  } 
    345353 
    346 C<dispatchers> should return list of names of dispatchers. 
    347  
    348   sub dispatchers { 
    349       my $self = shift; 
    350       return 'file', 'screen'; 
    351   } 
    352  
    353 C<attrs> should accept name of dispatcher, and return hash reference 
    354 of parameters for the dispatcher. 
    355  
    356   sub attrs { 
     354C<get_attes> accepts name of a dispatcher and should return hash 
     355reference of parameters associated with the dispatcher. 
     356 
     357  sub get_attrs { 
    357358      my($self, $name) = @_; 
    358359      if ($name eq 'file') { 
     
    364365              'format'  => '[%d] [%p] %m at %F line %L%n', 
    365366          }; 
    366       } elsif ($name eq 'screen') { 
     367      } 
     368      elsif ($name eq 'screen') { 
    367369          return { 
    368370              class     => 'Log::Dispatch::Screen', 
     
    372374          }; 
    373375      } 
     376      else { 
     377          die "invalid dispatcher name: $name"; 
     378      } 
    374379  } 
    375380 
     
    377382 
    378383Implement optional C<needs_reload> and C<reload> 
    379 method. C<needs_reload> should return boolean value if the object is 
    380 stale and needs reloading itself. 
    381  
    382 Stub confif file mtime based C<needs_reload> method is declared in 
     384method. C<needs_reload> accepts Log::Dispatch::Config instance and 
     385should return boolean value if the object is stale and needs reloading 
     386itself. 
     387 
     388Stub config file mtime based C<needs_reload> method is declared in 
    383389Log::Dispatch::Configurator as below, so if your config class is based 
    384390on filesystem files, you do not need to reimplement this. 
     
    389395  } 
    390396 
    391 C<reload> method is called when C<needs_reload> returns 
    392 true. Typically you should place configuration parsing again on 
    393 C<reload> method, so Log::Dispatch::Configurator again declares stub 
    394 C<reload> method that clones your object. 
     397C<reload> method is called when C<needs_reload> returns true, and 
     398should return new Configurator instance. Typically you should place 
     399configuration parsing again on this method, so 
     400Log::Dispatch::Configurator again declares stub C<reload> method that 
     401clones your object. 
    395402 
    396403  sub reload { 
     
    404411Thats all. Now you can plug your own configurator (Hardwired) into 
    405412Log::Dispatch::Config. What you should do is to pass configurator 
    406 object to C<config()> method call instead of config file name. 
     413object to C<configure> method call instead of config file name. 
    407414 
    408415  use Log::Dispatch; 
     
    410417 
    411418  my $config = Log::Dispatch::Configurator::Hardwired->new; 
    412   Log::Dispatch::Config->confifure($config); 
     419  Log::Dispatch::Config->configure($config); 
    413420 
    414421=back 
  • Log-Dispatch-Config/trunk/lib/Log/Dispatch/Configurator.pm

    r378 r379  
    2626} 
    2727 
    28 sub global_format { _abstract_method('global_format') } 
    29 sub dispatchers   { _abstract_method('dispatchers') } 
    30 sub attrs         { _abstract_method('attrs') } 
     28sub get_attrs_global { _abstract_method('get_attrs_global') } 
     29sub get_attrs        { _abstract_method('get_attrs') } 
    3130 
    32311; 
     
    4241  use base qw(Log::Dispatch::Configurator); 
    4342 
    44   sub global_format { } 
    45   sub dispatchers   { } 
    46   sub attrs         { } 
     43  sub get_attrs_global { } 
     44  sub get_attrs        { } 
    4745 
    4846=head1 DESCRIPTION 
  • Log-Dispatch-Config/trunk/lib/Log/Dispatch/Configurator/AppConfig.pm

    r378 r379  
    2525sub _config { $_[0]->{_config} } 
    2626 
    27 sub global_format
     27sub get_attrs_global
    2828    my $self = shift; 
    29     return $self->_config->get('format'); 
     29    return { 
     30        'format'    => scalar $self->_config->get('format'), 
     31        dispatchers => [ split /\s+/, $self->_config->get('dispatchers') ], 
     32    }; 
    3033} 
    3134 
    32 sub dispatchers { 
    33     my $self = shift; 
    34     return split /\s+/, $self->_config->get('dispatchers'); 
    35 
    36  
    37 sub attrs { 
     35sub get_attrs { 
    3836    my($self, $name) = @_; 
    39     my %var = $self->_config->varlist("^$name\."); 
     37    my $regex = "^$name" . '[\._]'; 
     38    my %var = $self->_config->varlist($regex); 
    4039    my %param = map { 
    41         (my $key = $_) =~ s/^$name\.//; 
     40        (my $key = $_) =~ s/$regex//; 
    4241        $key => $var{$_}; 
    4342    } keys %var; 
     
    8281  screen.format = %m 
    8382 
    84 See L<Log::Dispatch::Config> for details. 
     83You can use ini style grouping. 
     84 
     85  [file] 
     86  class = Log::Dispatch::File 
     87  min_level = debug 
     88 
     89  [screen] 
     90  class = Log::Dispatch::Screen 
     91  min_level = info 
     92 
     93If you use _ (underscore) in dispatcher name, something very B<bad> 
     94may happen. It is safe when you avoid doing so. 
    8595 
    8696=head1 AUTHOR 
  • Log-Dispatch-Config/trunk/t/06_configurator.t

    r378 r379  
    1414    my $disp = Log::Dispatch::Config->instance; 
    1515}; 
    16 like $@, qr/global_format/, $@; 
     16like $@, qr/get_attrs_global is/, $@; 
    1717 
    1818 
  • Log-Dispatch-Config/trunk/t/07_hardwired.t

    r378 r379  
    1010    sub new { bless {}, shift; } 
    1111 
    12     sub global_format
     12    sub get_attrs_global
    1313        my $self = shift; 
    14         return undef; 
     14        return { 
     15            'format' => undef, 
     16            dispatchers => [ qw(file screen) ], 
     17        }; 
    1518    } 
    1619 
    17     sub dispatchers { 
    18         return 'file', 'screen'; 
    19     } 
    20  
    21     sub attrs { 
     20    sub get_attrs { 
    2221        my($self, $name) = @_; 
    2322        if ($name eq 'file') {