File Coverage

blib/lib/Log/Handler/Config.pm
Criterion Covered Total %
statement 57 72 79.1
branch 13 30 43.3
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 80 112 71.4


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Log::Handler::Config - The main config loader.
4              
5             =head1 SYNOPSIS
6              
7             use Log::Handler;
8              
9             my $log = Log::Handler->new();
10              
11             # Config::General
12             $log->config(config => 'file.conf');
13              
14             # Config::Properties
15             $log->config(config => 'file.props');
16              
17             # YAML
18             $log->config(config => 'file.yaml');
19              
20             Or
21              
22             use Log::Handler;
23              
24             my $log = Log::Handler->new();
25              
26             $log->config(
27             config => 'file.conf'
28             plugin => 'YAML',
29             );
30              
31             =head1 DESCRIPTION
32              
33             This module makes it possible to load the configuration from a file.
34             The configuration type is determined by the file extension. It's also
35             possible to mix file extensions with another configuration types.
36              
37             =head1 PLUGINS
38              
39             Plugin name File extensions
40             ------------------------------------------
41             Config::General cfg, conf
42             Config::Properties props, jcfg, jconf
43             YAML yml, yaml
44              
45             If the extension is not defined then C is used by default.
46              
47             =head1 METHODS
48              
49             =head2 config()
50              
51             With this method it's possible to load the configuration for your outputs.
52              
53             The following options are valid:
54              
55             =over 4
56              
57             =item B
58              
59             With this option you can pass a file name or the configuration as a
60             hash reference.
61              
62             $log->config(config => 'file.conf');
63             # or
64             $log->config(config => \%config);
65              
66             =item B
67              
68             With this option it's possible to say which plugin you want to use.
69             Maybe you want to use the file extension C with C, which
70             is reserved for the plugin C.
71              
72             Examples:
73              
74             # this would use Config::General
75             $log->config(
76             config => 'file.conf'
77             );
78              
79             # this would force .conf with YAML
80             $log->config(
81             config => 'file.conf',
82             plugin => 'YAML'
83             );
84              
85             =item B
86              
87             If you want to write the configuration into a global configuration file
88             then you can create a own section for the logger:
89              
90            
91            
92             filename = file.log
93             minlevel = emerg
94             maxlevel = warning
95            
96              
97            
98             minlevel = emerg
99             maxlevel = debug
100            
101            
102              
103            
104             foo = bar
105             bar = baz
106             baz = foo
107            
108              
109             Now your configuration is placed in the C section. You can load this
110             section with
111              
112             $log->config(
113             config => 'file.conf',
114             section => 'logger',
115             );
116              
117             # or if you load the configuration yourself to %config
118              
119             $log->config(
120             config => \%config,
121             section => 'logger',
122             );
123              
124             # or just
125              
126             $log->config( config => $config{logger} );
127              
128             =back
129              
130             =head1 PLUGINS
131              
132             Config::General - inspired by the well known apache config format
133             Config::Properties - Java-style property files
134             YAML - optimized for human readability
135              
136             =head1 EXAMPLES
137              
138             =head2 Config structures
139              
140             A very simple configuration looks like:
141              
142             $log->config(config => {
143             file => {
144             alias => 'file1',
145             filename => 'file1.log',
146             maxlevel => 'info',
147             minlevel => 'warn',
148             },
149             screen => {
150             alias => 'screen1',
151             maxlevel => 'debug',
152             minlevel => 'emerg',
153             }
154             });
155              
156             Now, if you want to add another file-output then you can pass the outputs
157             with a array reference:
158              
159             $log->config(config => {
160             file => [
161             {
162             alias => 'file1,
163             filename => 'file1.log',
164             maxlevel => 'info',
165             minlevel => 'warn',
166             },
167             {
168             alias => 'file2',
169             filename => 'file2.log',
170             maxlevel => 'error',
171             minlevel => 'emergency',
172             }
173             ],
174             screen => {
175             alias => 'screen1',
176             maxlevel => 'debug',
177             minlevel => 'emerg',
178             },
179             });
180              
181             It's also possible to pass the outputs as a hash reference.
182             The hash keys "file1" and "file2" will be used as aliases.
183              
184             $log->config(config => {
185             file => {
186             file1 => {
187             filename => 'file1.log',
188             maxlevel => 'info',
189             minlevel => 'warn',
190             },
191             file2 => {
192             filename => 'file2.log',
193             maxlevel => 'error',
194             minlevel => 'emergency',
195             }
196             },
197             screen => {
198             alias => 'screen1',
199             maxlevel => 'debug',
200             minlevel => 'emerg',
201             },
202             });
203              
204             If you pass the configuration with the alias as a hash key then
205             it's also possible to pass a section called "default". The options
206             from this section will be used as defaults.
207              
208             $log->config(config => {
209             file => {
210             default => { # defaults for all file-outputs
211             mode => 'append',
212             },
213             file1 => {
214             filename => 'file1.log',
215             maxlevel => 'info',
216             minlevel => 'warn',
217             },
218             file2 => {
219             filename => 'file2.log',
220             maxlevel => 'error',
221             minlevel => 'emergency',
222             }
223             },
224             screen => {
225             alias => 'screen1',
226             maxlevel => 'debug',
227             minlevel => 'emerg',
228             },
229             });
230              
231             =head2 Examples for the config plugins
232              
233             =head3 Config::General
234              
235            
236             alias = file1
237             fileopen = 1
238             reopen = 1
239             permissions = 0640
240             maxlevel = info
241             minlevel = warn
242             mode = append
243             timeformat = %b %d %H:%M:%S
244             debug_mode = 2
245             filename = example.log
246             message_layout = '%T %H[%P] [%L] %S: %m'
247            
248              
249             Or
250              
251            
252            
253             fileopen = 1
254             reopen = 1
255             permissions = 0640
256             maxlevel = info
257             minlevel = warn
258             mode = append
259             timeformat = %b %d %H:%M:%S
260             debug_mode = 2
261             filename = example.log
262             message_layout = '%T %H[%P] [%L] %S: %m'
263            
264            
265              
266             =head3 YAML
267              
268             ---
269             file:
270             alias: file1
271             debug_mode: 2
272             filename: example.log
273             fileopen: 1
274             maxlevel: info
275             minlevel: warn
276             mode: append
277             permissions: 0640
278             message_layout: '%T %H[%P] [%L] %S: %m'
279             reopen: 1
280             timeformat: '%b %d %H:%M:%S'
281              
282             Or
283              
284             ---
285             file:
286             file1:
287             debug_mode: 2
288             filename: example.log
289             fileopen: 1
290             maxlevel: info
291             minlevel: warn
292             mode: append
293             permissions: 0640
294             message_layout: '%T %H[%P] [%L] %S: %m'
295             reopen: 1
296             timeformat: '%b %d %H:%M:%S'
297              
298             =head3 Config::Properties
299              
300             file.alias = file1
301             file.reopen = 1
302             file.fileopen = 1
303             file.maxlevel = info
304             file.minlevel = warn
305             file.permissions = 0640
306             file.mode = append
307             file.timeformat = %b %d %H:%M:%S
308             file.debug_mode = 2
309             file.filename = example.log
310             file.message_layout = '%T %H[%P] [%L] %S: %m'
311              
312             Or
313              
314             file.file1.alias = file1
315             file.file1.reopen = 1
316             file.file1.fileopen = 1
317             file.file1.maxlevel = info
318             file.file1.minlevel = warn
319             file.file1.permissions = 0640
320             file.file1.mode = append
321             file.file1.timeformat = %b %d %H:%M:%S
322             file.file1.debug_mode = 2
323             file.file1.filename = example.log
324             file.file1.message_layout = '%T %H[%P] [%L] %S: %m'
325              
326             =head1 PREREQUISITES
327              
328             Carp
329             Params::Validate
330              
331             =head1 EXPORTS
332              
333             No exports.
334              
335             =head1 REPORT BUGS
336              
337             Please report all bugs to .
338              
339             If you send me a mail then add Log::Handler into the subject.
340              
341             =head1 AUTHOR
342              
343             Jonny Schulz .
344              
345             =head1 COPYRIGHT
346              
347             Copyright (C) 2007-2009 by Jonny Schulz. All rights reserved.
348              
349             This program is free software; you can redistribute it and/or
350             modify it under the same terms as Perl itself.
351              
352             =cut
353              
354             package Log::Handler::Config;
355              
356 15     15   58 use strict;
  15         17  
  15         367  
357 15     15   53 use warnings;
  15         17  
  15         519  
358             our $VERSION = '0.09';
359              
360 15     15   50 use Carp;
  15         15  
  15         730  
361 15     15   60 use File::Spec;
  15         17  
  15         136  
362 15     15   297 use Params::Validate;
  15         15  
  15         8557  
363              
364             sub config {
365 4     4 1 7 my $self = shift;
366 4         14 my $params = $self->_validate(@_);
367 4         11 my $config = $self->_get_config($params);
368              
369 4 50       10 if (ref($config) ne 'HASH') {
370 0         0 croak "Bad config structure!";
371             }
372              
373             # Structure:
374             # $log_config{file} = [ \%a, \%b, \%c ]
375             # $log_config{dbi} = [ \%a, \%b, \%c ]
376 4         7 my %log_config;
377              
378 4         12 foreach my $type (keys %$config) {
379 5         7 my $output = $config->{$type};
380 5         7 my $ref = ref($output);
381              
382 5 100       20 if ($ref eq 'HASH') {
    50          
383 2         3 push @{$log_config{$type}}, $self->_get_hash_config($output);
  2         10  
384             } elsif ($ref eq 'ARRAY') {
385 3         16 push @{$log_config{$type}}, @$output;
  3         36  
386             } else {
387 0         0 croak "Bad config structure for '$type'";
388             }
389             }
390              
391 4         14 return \%log_config;
392             }
393              
394             #
395             # private stuff
396             #
397              
398             sub _get_config {
399 4     4   6 my ($self, $params) = @_;
400 4         5 my $config = ();
401 4         8 my $plugin = $params->{plugin};
402              
403 4 50       11 if (ref($params->{config})) {
    0          
404 4         9 $config = $params->{config};
405             } elsif ($params->{config}) {
406 0         0 eval "require $plugin";
407 0 0       0 if ($@) {
408 0         0 croak "unable to load plugin '$plugin' - $@";
409             }
410 0         0 $config = $plugin->get_config($params->{config});
411             }
412              
413 4 50       11 if ($params->{section}) {
414 0         0 return $config->{ $params->{section} };
415             }
416              
417 4         6 return $config;
418             }
419              
420             sub _get_hash_config {
421 2     2   3 my ($self, $config) = @_;
422 2         4 my @config = ();
423 2         5 my %default = ();
424              
425 2 100       5 if (exists $config->{default}) {
426 1         1 %default = %{ $config->{default} };
  1         11  
427             }
428              
429 2         8 foreach my $alias (keys %$config) {
430 3 100       14 next if $alias eq "default";
431 2         3 my $param = $config->{$alias};
432              
433 2 100       7 if (ref($param) ne 'HASH') {
434 1         1 push @config, $config;
435 1         2 last;
436             }
437              
438 1         2 $param->{alias} = $alias;
439 1         5 my %config = (%default, %$param);
440 1         2 push @config, \%config;
441             }
442              
443 2         6 return @config;
444             }
445              
446             sub _validate {
447 4     4   6 my $self = shift;
448              
449 4         156 my %options = Params::Validate::validate(@_, {
450             config => {
451             type => Params::Validate::SCALAR
452             | Params::Validate::HASHREF
453             | Params::Validate::ARRAYREF,
454             optional => 1,
455             },
456             plugin => {
457             type => Params::Validate::SCALAR,
458             optional => 1,
459             },
460             section => {
461             type => Params::Validate::SCALAR,
462             optional => 1,
463             },
464             });
465              
466 4         21 my $ref = ref($options{config});
467              
468 4 50       13 if ($ref ne 'HASH') {
469 0 0       0 if ($ref eq 'ARRAY') {
470 0         0 $options{config} = File::Spec->catfile(@{$options{config}});
  0         0  
471             }
472              
473 0 0       0 if (!$options{plugin}) {
474 0 0       0 if ($options{config} =~ /\.ya{0,1}ml\z/) {
    0          
475 0         0 $options{plugin} = 'Log::Handler::Plugin::YAML';
476             } elsif ($options{config} =~ /\.(?:props|jc(?:onf|fg))\z/) {
477 0         0 $options{plugin} = 'Log::Handler::Plugin::Config::Properties';
478             } else {
479 0         0 $options{plugin} = 'Log::Handler::Plugin::Config::General';
480             }
481             }
482             }
483              
484 4         11 return \%options;
485             }
486              
487             1;