diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2014-11-01 01:47:12 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2014-11-01 01:47:12 +0000 |
commit | 94566f012421026c8311552f99175a5989eba063 (patch) | |
tree | 0bfd47111b94a1715d14b8c4ab2d82ad1abc09b1 /eg | |
download | Log-Log4perl-tarball-94566f012421026c8311552f99175a5989eba063.tar.gz |
Log-Log4perl-1.46HEADLog-Log4perl-1.46master
Diffstat (limited to 'eg')
-rwxr-xr-x | eg/5005it.pl | 84 | ||||
-rw-r--r-- | eg/L4pResurrectable.pm | 12 | ||||
-rw-r--r-- | eg/benchmarks/Makefile | 10 | ||||
-rwxr-xr-x | eg/benchmarks/simple | 79 | ||||
-rwxr-xr-x | eg/color | 26 | ||||
-rw-r--r-- | eg/dupe-warning.conf | 7 | ||||
-rw-r--r-- | eg/jabber.conf | 14 | ||||
-rwxr-xr-x | eg/l4p-tmpl | 63 | ||||
-rw-r--r-- | eg/log4j-file-append-java.conf | 12 | ||||
-rw-r--r-- | eg/log4j-file-append-perl.conf | 13 | ||||
-rw-r--r-- | eg/log4j-manual-1.conf | 13 | ||||
-rw-r--r-- | eg/log4j-manual-2.conf | 13 | ||||
-rw-r--r-- | eg/log4j-manual-3.conf | 14 | ||||
-rw-r--r-- | eg/log4j-utf8.conf | 5 | ||||
-rwxr-xr-x | eg/newsyslog-test | 30 | ||||
-rwxr-xr-x | eg/override_appender | 73 | ||||
-rwxr-xr-x | eg/prototype | 34 | ||||
-rwxr-xr-x | eg/syslog.pl | 86 | ||||
-rwxr-xr-x | eg/yamlparser | 93 |
19 files changed, 681 insertions, 0 deletions
diff --git a/eg/5005it.pl b/eg/5005it.pl new file mode 100755 index 0000000..82545d2 --- /dev/null +++ b/eg/5005it.pl @@ -0,0 +1,84 @@ +#!/usr/bin/perl +########################################### +# 5005it -- make a PM file 5005-compatible +# Mike Schilli, 2002 (m@perlmeister.com) +########################################### +use 5.00503; +use strict; + +use File::Find; + +my $USEVARS_DONE = 0; +my @OUR_VARS = (); + +########################################### +sub mk5005 { +########################################### + find(\&process_file, "lib", "t"); +} + +########################################### +sub process_file { +########################################### + my($file) = $_; + + return unless -f $file; + + $USEVARS_DONE = 0; + @OUR_VARS = (); + + open FILE, "<$file" or die "Cannot open $file"; + my $data = join '', <FILE>; + close FILE; + + while($data =~ /^our[\s(]+([\$%@][\w_]+).*[;=]/mg) { + push @OUR_VARS, $1; + } + + # Replace 'our' variables + $data =~ s/^our[\s(]+[\$%@][\w_]+.*/rep_our($&)/meg; + + # Replace 'use 5.006' lines + $data =~ s/^use\s+5\.006/\nuse 5.00503/mg; + + # Delete 'no/use warnings;': \s seems to eat newlines, so use [] + $data =~ s/^[ \t]*use warnings;//mg; + $data =~ s/^[ \t]*no warnings.*?;/\$\^W = undef;/mg; + + # 5.00503 can't handle constants that start with a _ + $data =~ s/_INTERNAL_DEBUG/INTERNAL_DEBUG/g; + + # Anything before 5.6.0 doesn't have the two argument binmode. + # Convert to one arg version by discarding second arg. + $data =~ s{ binmode \s* \(? (.*?) , .* \)? \s* ; }{ "binmode $1 ;" }gex; + + open FILE, ">$file" or die "Cannot open $file"; + print FILE $data; + close FILE; +} + +########################################### +sub rep_our { +########################################### + my($line) = @_; + + my $out = ""; + + if(!$USEVARS_DONE) { + $out = "use vars qw(" . join(" ", @OUR_VARS) . "); "; + $USEVARS_DONE = 1; + } + + if($line =~ /=/) { + # There's an assignment, just skip the 'our' + $line =~ s/^our\s+//; + } else { + # There's nothing, just get rid of the entire line + $line = "\n"; + } + + $out .= $line; + return $out; +} + +1; diff --git a/eg/L4pResurrectable.pm b/eg/L4pResurrectable.pm new file mode 100644 index 0000000..fd527ca --- /dev/null +++ b/eg/L4pResurrectable.pm @@ -0,0 +1,12 @@ +package L4pResurrectable; +use Log::Log4perl qw(:easy); +use vars qw($VERSION); + +$VERSION = "0.01"; + +sub foo { + ###l4p DEBUG "foo was here"; + ###l4p INFO "bar was here"; +} + +1; diff --git a/eg/benchmarks/Makefile b/eg/benchmarks/Makefile new file mode 100644 index 0000000..b07d17e --- /dev/null +++ b/eg/benchmarks/Makefile @@ -0,0 +1,10 @@ + +all: + perl -I../../blib/lib -MLog::Log4perl -le 'print $$Log::Log4perl::VERSION' + perl -I../../blib/lib ./simple + +master: + cd ../..; git checkout master; perl Makefile.PL >/dev/null; make >/dev/null + +eval_free: + cd ../..; git checkout eval_free; perl Makefile.PL >/dev/null; make >/dev/null diff --git a/eg/benchmarks/simple b/eg/benchmarks/simple new file mode 100755 index 0000000..9558efc --- /dev/null +++ b/eg/benchmarks/simple @@ -0,0 +1,79 @@ +#!/usr/local/bin/perl -w +########################################### +# Log4perl Benchmarks +# Mike Schilli, 2008 (m@perlmeister.com) +########################################### +use strict; +use Benchmark qw(timeit timestr); +use Log::Log4perl qw(:easy); +use Sysadm::Install qw(:all); +use Data::Dumper; +use File::Temp qw(tempfile); + +my($tmp_fh, $tmp_file) = tempfile( UNLINK => 1 ); + +my $nof_tests = 100000; + +print "sp=suppressed w=watch sc=subcategory\n\n"; + +for my $watch (0, 1) { + test_init({ level => "DEBUG", watch => $watch }); + run("sp0 sc0 w$watch", \&debug_logger); + + test_init({ level => "ERROR", watch => $watch }); + run("sp1 sc0 w$watch", \&debug_logger); + + test_init({ level => "DEBUG", watch => $watch }); + run("sp0 sc1 w$watch", \&subcat_logger); + + test_init({ level => "ERROR", watch => $watch }); + run("sp1 sc1 w$watch", \&subcat_logger); +} + +########################################### +sub run { +########################################### + my($name, $sub) = @_; + + my $t = timeit(1, $sub); + printf "$name: %8.0f per sec\n", $nof_tests/$t->[1]; +} + +########################################### +sub test_init { +########################################### + my($opts) = @_; + + my $conf = qq{ + log4perl.logger = $opts->{level}, testapp + log4perl.appender.testapp = Log::Log4perl::Appender::TestBuffer + log4perl.appender.testapp.layout= SimpleLayout + }; + + if($opts->{watch}) { + blurt $conf, $tmp_file; + Log::Log4perl->init_and_watch( $tmp_file ); + } else { + Log::Log4perl->init( \$conf ); + } +} + +########################################### +sub debug_logger { +########################################### + my $logger = get_logger(""); + + for(1..$nof_tests) { + $logger->debug( "message" ); + } +} + +########################################### +sub subcat_logger { +########################################### + my $logger = get_logger("a.b.c.d.e.f.g.h.i.j.k"); + + for(1..$nof_tests) { + $logger->debug( "message" ); + } +} diff --git a/eg/color b/eg/color new file mode 100755 index 0000000..ff9f52d --- /dev/null +++ b/eg/color @@ -0,0 +1,26 @@ +#!/usr/bin/perl +###################################################################### +# color - Print messages colored by level +###################################################################### +use strict; +use warnings; + +my $VERSION = "0.01"; +our $CVSVERSION = '$Revision: 1.1 $'; + +use Log::Log4perl qw(:easy); +Log::Log4perl->init(\ <<'EOT'); + log4perl.category = DEBUG, Screen + log4perl.appender.Screen = Log::Log4perl::Appender::ScreenColoredLevels + log4perl.appender.Screen.layout = \ + Log::Log4perl::Layout::PatternLayout + log4perl.appender.Screen.layout.ConversionPattern = %d %F{1} %L> %m %n +EOT + +for(1..3) { + DEBUG "Debug Message"; + INFO "Info Message"; + WARN "Warn Message"; + ERROR "Error Message"; + FATAL "Fatal Message"; +} diff --git a/eg/dupe-warning.conf b/eg/dupe-warning.conf new file mode 100644 index 0000000..9aac61e --- /dev/null +++ b/eg/dupe-warning.conf @@ -0,0 +1,7 @@ +log4perl.category = WARN, Logfile +log4perl.appender.Logfile = Log::Log4perl::Appender::File +log4perl.appender.Logfile.filename = test.log +log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n + +log4perl.category = TRACE, Logfile diff --git a/eg/jabber.conf b/eg/jabber.conf new file mode 100644 index 0000000..dd7994d --- /dev/null +++ b/eg/jabber.conf @@ -0,0 +1,14 @@ +#here's an example of using Log::Dispatch::Jabber + +log4j.category.animal.dog = INFO, jabbender + +log4j.appender.jabbender = Log::Dispatch::Jabber +log4j.appender.jabbender.layout = Log::Log4perl::Layout::SimpleLayout +log4j.appender.jabbender.login.hostname = a.jabber.server +log4j.appender.jabbender.login.port = 5222 +log4j.appender.jabbender.login.username = ***** +log4j.appender.jabbender.login.password = ********** +log4j.appender.jabbender.login.resource = logger +log4j.appender.jabbender.to = *****@a.jabber.server +log4j.appender.jabbender.to = ******@another.jabber.server + diff --git a/eg/l4p-tmpl b/eg/l4p-tmpl new file mode 100755 index 0000000..bd0e382 --- /dev/null +++ b/eg/l4p-tmpl @@ -0,0 +1,63 @@ +#!/usr/bin/perl +########################################### +# l4p-tmpl +# 2009, Mike Schilli <m@perlmeister.com> +########################################### +use strict; +use warnings; + +print <<'EOT'; +log4perl.category = WARN, Logfile +log4perl.appender.Logfile = Log::Log4perl::Appender::File +log4perl.appender.Logfile.filename = test.log +log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n +EOT + +__END__ + +=head1 NAME + + l4p-tmpl - Print out a Log4perl template configuration + +=head1 SYNOPSIS + + l4p-tmpl >l4p.conf + +=head1 DESCRIPTION + +l4p-tmpl prints out the text of a template Log4perl configuration for +starting a new Log4perl configuration file. + +=head1 LICENSE + +Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt> +and Kevin Goess E<lt>cpan@goess.orgE<gt>. + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=head1 AUTHOR + +Please contribute patches to the project on Github: + + http://github.com/mschilli/log4perl + +Send bug reports or requests for enhancements to the authors via our + +MAILING LIST (questions, bug reports, suggestions/patches): +log4perl-devel@lists.sourceforge.net + +Authors (please contact them via the list above, not directly): +Mike Schilli <m@perlmeister.com>, +Kevin Goess <cpan@goess.org> + +Contributors (in alphabetical order): +Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton +Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony +Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy +Grundman, Paul Harrington, Alexander Hartmaier David Hull, +Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, +Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, +Lars Thegler, David Viner, Mac Yang. + diff --git a/eg/log4j-file-append-java.conf b/eg/log4j-file-append-java.conf new file mode 100644 index 0000000..efdd695 --- /dev/null +++ b/eg/log4j-file-append-java.conf @@ -0,0 +1,12 @@ +############################################################ +# A simple root logger with a FileAppender file appender +# in Java (ultimately maps to Log::Dispatch::File). +# Mike Schilli 2002 m@perlmeister.com +############################################################ +log4j.rootLogger=DEBUG, LOGFILE + +log4j.appender.LOGFILE=org.apache.log4j.FileAppender +log4j.appender.LOGFILE.File=example-java.log + +log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.LOGFILE.layout.ConversionPattern=%F %L %p %t %c - %m%n diff --git a/eg/log4j-file-append-perl.conf b/eg/log4j-file-append-perl.conf new file mode 100644 index 0000000..b587136 --- /dev/null +++ b/eg/log4j-file-append-perl.conf @@ -0,0 +1,13 @@ +############################################################ +# A simple root logger with a Log::Dispatch file appender +# in Perl. +# Mike Schilli 2002 m@perlmeister.com +############################################################ +log4j.rootLogger=DEBUG, LOGFILE + +log4j.appender.LOGFILE=Log::Log4perl::Appender::File +log4j.appender.LOGFILE.filename=example-perl.log +log4j.appender.LOGFILE.mode=append + +log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout +log4j.appender.LOGFILE.layout.ConversionPattern=%F{1} %L %p %t %c - %m%n diff --git a/eg/log4j-manual-1.conf b/eg/log4j-manual-1.conf new file mode 100644 index 0000000..4eadd3b --- /dev/null +++ b/eg/log4j-manual-1.conf @@ -0,0 +1,13 @@ +# From the Log4j manual at +# http://jakarta.apache.org/log4j/docs/manual.html +# (Just replaced ConsoleAppender by BufferAppender for testing) + +# Set root logger level to DEBUG and its only appender to A1. +log4j.rootLogger=DEBUG, A1 + +# A1 is set to be a BufferAppender (a ConsoleAppender for testing). +log4j.appender.A1=Log::Log4perl::Appender::TestBuffer + +# A1 uses PatternLayout. +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %t - %m%n diff --git a/eg/log4j-manual-2.conf b/eg/log4j-manual-2.conf new file mode 100644 index 0000000..8bab5c0 --- /dev/null +++ b/eg/log4j-manual-2.conf @@ -0,0 +1,13 @@ +# From the Log4j manual at +# http://jakarta.apache.org/log4j/docs/manual.html +# (Just replaced ConsoleAppender by BufferAppender for testing) + +log4j.rootLogger=DEBUG, A1 +log4j.appender.A1=Log::Log4perl::Appender::TestBuffer +log4j.appender.A1.layout=org.apache.log4j.PatternLayout + +# Print the date in ISO 8601 format +log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n + +# Print only messages of level WARN or above in the package com.foo. +log4j.logger.com.foo=WARN diff --git a/eg/log4j-manual-3.conf b/eg/log4j-manual-3.conf new file mode 100644 index 0000000..12556d0 --- /dev/null +++ b/eg/log4j-manual-3.conf @@ -0,0 +1,14 @@ +# Derived from the Log4j manual at +# http://jakarta.apache.org/log4j/docs/manual.html + +log4j.rootLogger=DEBUG, stdout, R + +log4j.appender.stdout=Log::Log4perl::Appender::TestBuffer +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout + +# Pattern to output the caller's file name and line number. +log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n + +log4j.appender.R=Log::Log4perl::Appender::TestBuffer +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%p %t '%c' - %m%n diff --git a/eg/log4j-utf8.conf b/eg/log4j-utf8.conf new file mode 100644 index 0000000..24d8131 --- /dev/null +++ b/eg/log4j-utf8.conf @@ -0,0 +1,5 @@ +# Config file with utf8 characters +log4perl.rootLogger=DEBUG, Ä1 +log4perl.appender.Ä1=Log::Log4perl::Appender::TestBuffer +log4perl.appender.Ä1.layout=PatternLayout +log4perl.appender.Ä1.layout.ConversionPattern=%m%n diff --git a/eg/newsyslog-test b/eg/newsyslog-test new file mode 100755 index 0000000..466c897 --- /dev/null +++ b/eg/newsyslog-test @@ -0,0 +1,30 @@ +#!/usr/local/bin/perl -w +########################################### +# newsyslog-test +# Mike Schilli, 200t (m@perlmeister.com) +########################################### +use strict; +use Log::Log4perl qw(:easy); + +# newsyslog configuration: +# /tmp/test.log 666 12 1 * B /tmp/test.pid 30 + +my $conf = q{ +log4perl.category = DEBUG, Logfile +log4perl.appender.Logfile = Log::Log4perl::Appender::File +log4perl.appender.Logfile.recreate = 1 +log4perl.appender.Logfile.recreate_check_signal = USR1 +log4perl.appender.Logfile.recreate_pid_write = /tmp/test.pid +log4perl.appender.Logfile.mode = append +log4perl.appender.Logfile.filename = /tmp/test.log +log4perl.appender.Logfile.layout = Log::Log4perl::Layout::PatternLayout +log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m%n +}; + +Log::Log4perl->init(\$conf); + +while(1) { + DEBUG "test" x 1000; + system("ls -l /tmp/test.log* | head -2; echo"); + sleep(1); +} diff --git a/eg/override_appender b/eg/override_appender new file mode 100755 index 0000000..7867fc1 --- /dev/null +++ b/eg/override_appender @@ -0,0 +1,73 @@ +#!/usr/bin/perl +###################################################################### +# override_appender -- 2003, Mike Schilli <m@perlmeister.com> +###################################################################### +# Overrided the appender layout after defining it in the conf file. +###################################################################### +use strict; +use warnings; + +my $VERSION = "0.01"; +our $CVSVERSION = '$Revision: 1.1 $'; + +use Log::Log4perl qw(:easy); +Log::Log4perl->init(\ <<'EOT'); + log4perl.category = WARN, Screen + log4perl.appender.Screen = Log::Log4perl::Appender::Screen + log4perl.appender.Screen.layout = \ + Log::Log4perl::Layout::PatternLayout + log4perl.appender.Screen.layout.ConversionPattern = %d %F{1} %L> %m %n +EOT + +my $appenders = Log::Log4perl->appenders(); +my $layout = Log::Log4perl::Layout::PatternLayout->new("%m %m%n"); +$appenders->{Screen}->layout($layout); +WARN("test message"); + +__END__ + +=head1 NAME + + override_appender - Try to change an appender's layout + +=head1 SYNOPSIS + + override_appender + +=head1 DESCRIPTION + +Change an appender's layout after it has been defined in the configuration +file. + +=head1 LICENSE + +Copyright 2002-2013 by Mike Schilli E<lt>m@perlmeister.comE<gt> +and Kevin Goess E<lt>cpan@goess.orgE<gt>. + +This library is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=head1 AUTHOR + +Please contribute patches to the project on Github: + + http://github.com/mschilli/log4perl + +Send bug reports or requests for enhancements to the authors via our + +MAILING LIST (questions, bug reports, suggestions/patches): +log4perl-devel@lists.sourceforge.net + +Authors (please contact them via the list above, not directly): +Mike Schilli <m@perlmeister.com>, +Kevin Goess <cpan@goess.org> + +Contributors (in alphabetical order): +Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton +Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony +Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy +Grundman, Paul Harrington, Alexander Hartmaier David Hull, +Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, +Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, +Lars Thegler, David Viner, Mac Yang. + diff --git a/eg/prototype b/eg/prototype new file mode 100755 index 0000000..f0b0bc5 --- /dev/null +++ b/eg/prototype @@ -0,0 +1,34 @@ +#!/usr/bin/perl +########################################### +# prototype -- use a Class::Prototyped appender +# Mike Schilli, 2004 (m@perlmeister.com) +########################################### +use warnings; +use strict; + +use Class::Prototyped; + +my $class = Class::Prototyped->newPackage( + "MyAppenders::Bulletizer", + bullets => 1, + log => sub { + my($self, %params) = @_; + print "*" x $self->bullets(), + $params{message}; + }, +); + +use Log::Log4perl qw(:easy); + +Log::Log4perl->init(\ q{ + log4perl.logger = INFO, Bully + + log4perl.appender.Bully=MyAppenders::Bulletizer + log4perl.appender.Bully.bullets=3 + + log4perl.appender.Bully.layout = PatternLayout + log4perl.appender.Bully.layout.ConversionPattern=%m %n +}); + + # ... prints: "***Boo!\n"; +INFO "Boo!"; diff --git a/eg/syslog.pl b/eg/syslog.pl new file mode 100755 index 0000000..978017b --- /dev/null +++ b/eg/syslog.pl @@ -0,0 +1,86 @@ +#!/usr/bin/perl +########################################### +# Syslog test cases +# Kevin Goess, cpan@goess.org 2002 +########################################### +use warnings; +use strict; + +use Log::Log4perl; +use Test; + +our $RESULT_BUFFER; + +package Log::MyOwnAppender; + +our $IS_LOADED = 1; + +use base qw(Log::Dispatch::Output); + +sub new { + my($proto, %params) = @_; + my $class = ref $proto || $proto; + + my $self = bless {}, $class; + + $self->_basic_init(%params); + + return $self; +} + + +sub log_message { + my $self = shift; + my %params = @_; + + #params is { name => \$appender_name, + # level => 0, + # message => \$message, + + $main::RESULT_BUFFER = $params{level}; +} + + +package main; + + +my $config = <<EOL; +log4j.category.plant = DEBUG, tappndr,syslogappndr + +log4j.appender.tappndr = Log::MyOwnAppender +log4j.appender.tappndr.layout = org.apache.log4j.SimpleLayout + +log4j.appender.syslogappndr = Log::Dispatch::Syslog +log4j.appender.syslogappndr.layout = org.apache.log4j.SimpleLayout + + +EOL + + +Log::Log4perl::init(\$config); + +my $logger = Log::Log4perl::get_logger('plant'); + +$logger->fatal('foo'); +ok($RESULT_BUFFER, 7); +$RESULT_BUFFER = undef; + +$logger->error('foo'); +ok($RESULT_BUFFER, 4); +$RESULT_BUFFER = undef; + +$logger->warn('foo'); +ok($RESULT_BUFFER, 3); +$RESULT_BUFFER = undef; + +$logger->info('foo'); +ok($RESULT_BUFFER, 1); +$RESULT_BUFFER = undef; + +$logger->debug('foo'); +ok($RESULT_BUFFER, 0); +$RESULT_BUFFER = undef; + + + +BEGIN { plan tests => 5, } diff --git a/eg/yamlparser b/eg/yamlparser new file mode 100755 index 0000000..3c0a5d2 --- /dev/null +++ b/eg/yamlparser @@ -0,0 +1,93 @@ +#!/usr/bin/perl +########################################### +# yamlparser +# Mike Schilli, 2004 (m@perlmeister.com) +########################################### +use warnings; +use strict; + +package MyYAMLParser; +use base qw(Log::Log4perl::Config::BaseConfigurator); +use YAML qw(LoadFile Load); +use Data::Dumper; + +########################################### +sub new { +########################################### + my($class, %options) = @_; + + my $self = {}; + + bless $self, $class; + + $self->{text} = $options{text} if exists $options{text}; + $self->{file} = $options{file} if exists $options{file}; + + if(! exists $self->{text} and + ! exists $self->{file}) { + die "usage: ", __PACKAGE__, "->new(file => \$filename) or ", + __PACKAGE__, "->new(text => \$text)"; + } + + return $self; +} + +########################################### +sub parse { +########################################### + my($self) = @_; + + my $data = {}; + + if(exists $self->{text}) { + $self->{data} = Load($self->{text}); + } + + # Move all non-hash values under {...}->{value} + my @todo = ($self->{data}); + + while (@todo) { + my $ref = shift @todo; + for (keys %$ref) { + if(ref($ref->{$_}) eq "HASH") { + push @todo, $ref->{$_}; + } elsif($_ eq "name") { + # Appender 'name' entries are + # converted to ->{value} entries + $ref->{value} = $ref->{$_}; + delete $ref->{$_}; + } else { + my $tmp = $ref->{$_}; + $ref->{$_} = {}; + $ref->{$_}->{value} = $tmp; + } + } + } + + return $self->{data}; +} + +package main; + +use Log::Log4perl; + +my $p = MyYAMLParser->new(text => <<EOT); + category: + Bar: + Twix: WARN, Screen, Screen2 + appender: + Screen: + name: Log::Log4perl::Appender::Screen + layout: Log::Log4perl::Layout::SimpleLayout + Screen2: + name: Log::Log4perl::Appender::Screen + layout: Log::Log4perl::Layout::SimpleLayout +EOT + +# use Data::Dump qw(dump); +# print dump($p->parse()); + +Log::Log4perl->init($p); + +my $log = Log::Log4perl->get_logger("Bar::Twix"); +$log->warn('foo'); |