尝试使用Log-Log4perl,但文件为空(0长度)

时间:2011-12-17 09:37:29

标签: perl logging

我正在尝试在Perl脚本中使用Log-Log4perl模块。一切正常但日志记录却没有。 这是我的代码:

sub create_log
{
    BEGIN
    {
        eval
        {
            require Log::Log4perl;
        };
        if( $@ )
        {
            print "Log::Log4perl module not installed - stubbing.\n";
            no strict qw( refs );
            *{"main::$_"} = sub {} for qw( DEBUG INFO WARN ERROR FATAL );
        }
        else
        {
            print "Log::Log4perl module installed.\n";
            require Log::Log4perl::Level;
            Log::Log4perl::Level->import( __PACKAGE__ );
            my $conf = q(
                log4perl.category.My.Script = INFO, FileApp, Screen
                log4perl.appender.FileApp = Log::Log4perl::Appender::File
                log4perl.appender.FileApp.mode = append
                log4perl.appender.FileApp.syswrite = 1
                log4perl.appender.FileApp.filename = my.log
                log4perl.appender.FileApp.layout = PatternLayout
                log4perl.appender.FileApp.layout.ConversionPattern = [%p][%d]{%F line %L} %m%n %d> %m%n
                log4perl.appender.Screen = Log::Log4perl::Appender::Screen
                log4perl.appender.Screen.stderr = 0
                log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
                    );
            Log::Log4perl->init( \$conf );
        }
    }
}

create_log();
$logger = Log::Log4perl->get_logger( "My;:Script" );
%month_nums = ("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4, May  => 5, "Jun" => 6, "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12);
$tries = 1;
$company_fetched = 0;
$logger->logdie( "\n" ) if !getopts( "w:f:", \%options );
$logger->logdie( "Incompatible options." ) if $options{'w'} && $options{'f'};

当运行脚本并同时提供-w和-f时,我在屏幕上看到如下错误:

Incompatible options. at /usr/lib/perl5/vendor_perl/5.12.2/Log/Log4perl/Logger.pm line 887

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

看起来像是一个错字:

$logger = Log::Log4perl->get_logger( "My;:Script" );

应该是

$logger = Log::Log4perl->get_logger( "My::Script" );