root/Log-Dispatch-Config/trunk/lib/Log/Dispatch/Configurator/AppConfig.pm

Revision 468 (checked in by miyagawa, 19 years ago)

0.12

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Log::Dispatch::Configurator::AppConfig;
2
3 use strict;
4 use vars qw($VERSION);
5 $VERSION = 0.12;
6
7 use Log::Dispatch::Configurator;
8 use base qw(Log::Dispatch::Configurator);
9 use AppConfig;
10
11 sub new {
12     my($class, $file) = @_;
13     my $self = bless { file => $file }, $class;
14     $self->parse_file;
15     return $self;
16 }
17
18 sub parse_file {
19     my $self = shift;
20     my $config = AppConfig->new({
21         CREATE => 1,
22         GLOBAL => {
23             ARGCOUNT => AppConfig::ARGCOUNT_ONE(),
24         },
25     });
26     $config->define(dispatchers => { DEFAULT => '' });
27     $config->define(format      => { DEFAULT => undef });
28     $config->file($self->{file});
29
30     $self->{_config} = $config;
31 }
32
33 sub reload {
34     my $self = shift;
35     $self->parse_file;
36 }
37
38 sub _config { $_[0]->{_config} }
39
40 sub get_attrs_global {
41     my $self = shift;
42     return {
43         format      => scalar $self->_config->get('format'),
44         dispatchers => [ split /\s+/, $self->_config->get('dispatchers') ],
45     };
46 }
47
48 sub get_attrs {
49     my($self, $name) = @_;
50     my $regex = "^$name" . '[\._]';
51     my %var = $self->_config->varlist($regex);
52     my %param = map {
53         (my $key = $_) =~ s/$regex//;
54         $key => $var{$_};
55     } keys %var;
56     return \%param;
57 }
58
59 1;
60 __END__
61
62 =head1 NAME
63
64 Log::Dispatch::Configurator::AppConfig - Configurator implementation with AppConfig
65
66 =head1 SYNOPSIS
67
68   use Log::Dispatch::Config;
69   use Log::Dispatch::Configurator::AppConfig;
70
71   my $config = Log::Dispatch::Configurator::AppConfig->new('log.cfg');
72   Log::Dispatch::Config->configure($config);
73
74   # nearby piece of code
75   my $log = Log::Dispatch::Config->instance;
76
77 =head1 DESCRIPTION
78
79 Log::Dispatch::Configurator::AppConfig is an implementation of
80 Log::Dispatch::Configurator using AppConfig format. Here is a sample
81 of config file.
82
83   dispatchers = file screen
84
85   file.class = Log::Dispatch::File
86   file.min_level = debug
87   file.filename = /path/to/log
88   file.mode = append
89   file.format = [%d] [%p] %m at %F line %L%n
90
91   screen.class = Log::Dispatch::Screen
92   screen.min_level = info
93   screen.stderr = 1
94   screen.format = %m
95
96 You can use ini style grouping.
97
98   [file]
99   class = Log::Dispatch::File
100   min_level = debug
101
102   [screen]
103   class = Log::Dispatch::Screen
104   min_level = info
105
106 If you use _ (underscore) in dispatcher name, something very B<bad>
107 may happen. It is safe when you avoid doing so.
108
109 =head1 AUTHOR
110
111 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
112
113 This library is free software; you can redistribute it and/or modify
114 it under the same terms as Perl itself.
115
116 =head1 SEE ALSO
117
118 L<Log::Dispatch::Config>, L<AppConfig>
119
120 =cut
Note: See TracBrowser for help on using the browser.