in reply to Re^2: Graceful handling of Log::Log4perl errors
in thread Graceful handling of Log::Log4perl errors
That looks pretty good! I'm gratified that you took my suggestion and ran with it. Thanks for sharing it with us! Looking at what you have, I think it could be made a bit more compact.
use File::Path qw( mkpath ); my $base_path = '/my/base/path'; my $init_done = 0; my %paths = (); INIT: while ( ! $init_done ) { $init_done = eval { Log::Log4perl->init_once($LOGGER_CONF_FILE); 1 }; # If there's an error, and it's THIS one, if ( $@ =~ m{\A Can't \s open \s \Q$base_path\E (?: / ( \S+ ) )? / [^/]+ \s \( No \s such \s file \s or \s directory \)}xms ) { # This is the path that's missing my $target_path = "$base_path/$1"; # If we've already tried to create it once, don't try again if ( $paths{ $target_path }++ ) { last INIT; } # Try to create the path eval { mkpath( $target_path ) }; if ( ! -d $target_path ) { warn "Can't create missing directory '$target_path': $@\n" +; } } } # if we couldn't get the config file to initlize, load a blank one and + move on without logging if ( ! $init_done ) { warn "Couldn't initilize logger, defaulting to blank config file"; Log::Log4perl->init( \'' ); }
Note the differences, though (good and bad).
Thanks again for posting your work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Graceful handling of Log::Log4perl errors
by chaos_cat (Scribe) on May 29, 2008 at 15:16 UTC |