my %paths = (); my $init_fail = 0; INIT: while (1) { eval {Log::Log4perl->init_once($LOGGER_CONF_FILE)}; if ($@) { # Config file error of some kind # Send some alert message here # if we get a missing directory error, try some (sane) recovery if ($@ =~ m{Can't\sopen\s/my/base/path/(.+)\s\(No such file or directory\)}) { my $path = $1; if ( $paths{$path}++ ) { # only try once to create a given path. This prevents infinite loops here $init_fail = 1; last INIT; } my $dir = "/my/base/path"; # if the base path isn't there, creating it is above this module's pay grade my @dir_list = split '/', $path; my $file = pop @dir_list; foreach my $sub_dir (@dir_list) { $dir .= "/$sub_dir"; if (! -d$dir) { eval {mkdir ($dir)}; # not too concerned with catching errors here, the next round will bail on duplicate path attempt } } } # for other config file errors, throw our hands up in disgust and move on else { $init_fail = 1; last INIT; } } else { $init_fail = 0; last INIT; } } # if we couldn't get the config file to initlize, load a blank one and move on without logging if ($init_fail) { warn "Couldn't initilize logger, defaulting to blank config file"; my $default_config_file = "/my/base/path/etc/default.logger.conf"; open (my $fh, ">", $default_config_file); close ($fh); Log::Log4perl::init_once($default_config_file); }