mschilli / log4perl

Log4j Implementation For Perl
http://log4perl.com
Other
116 stars 66 forks source link

How to get appender file name using logger object? #89

Closed pokhriyalp closed 4 years ago

pokhriyalp commented 6 years ago

I want to get appender file name using logger object.

my $log_conf = {
   'log4perl.rootLogger'             => "DEBUG, LOG1, SCREEN",
   'log4perl.appender.SCREEN'        => "Log::Log4perl::Appender::Screen",
   'log4perl.appender.SCREEN.stderr' => "0",
   'log4perl.appender.SCREEN.layout' => "Log::Log4perl::Layout::PatternLayout",
   'log4perl.appender.SCREEN.layout.ConversionPattern' =>
     "CloudAuTOM:%d{yyyy_MMM_d H:m:s}_" . get_ip_address() . "_%C::: %m %n",
   'log4perl.appender.LOG1'          => "Log::Log4perl::Appender::File",
   'log4perl.appender.LOG1.filename' => file_path($file_name, $path),
   #'log4perl.appender.LOG1.mode'                        => "append",
   'log4perl.appender.LOG1.layout' => "Log::Log4perl::Layout::PatternLayout",
   'log4perl.appender.LOG1.layout.ConversionPattern' =>
     "CloudAuTOM:%d{yyyy_MMM_d H:m:s}_%C::: %m %n"
};
Log::Log4perl->init($log_conf);
my $logger = Log::Log4perl->get_logger($file_name);

# somewhat like below:
print $logger->filename;  

I went over doc and it says that I can access appenders using appender_by_name() class method. I tried to access it using the command $logger->appender_by_name(), but it is returning me following error

Can't locate object method "appender_by_name" via package "Log::Log4perl::Logger"

Following is the dump of my logger object:

$VAR1 = bless( {
                 'TRACE' => sub { "DUMMY" },
                 'DEBUG' => sub { "DUMMY" },
                 'FATAL' => $VAR1->{'DEBUG'},
                 'additivity' => 1,
                 'level' => undef,
                 'layout' => undef,
                 'appender_names' => [],
                 'OFF' => $VAR1->{'DEBUG'},
                 'is_WARN' => sub { "DUMMY" },
                 'is_TRACE' => sub { "DUMMY" },
                 'is_OFF' => sub { "DUMMY" },
                 'is_DEBUG' => sub { "DUMMY" },
                 'ERROR' => $VAR1->{'DEBUG'},
                 'is_INFO' => sub { "DUMMY" },
                 'is_ERROR' => sub { "DUMMY" },
                 'is_ALL' => sub { "DUMMY" },
                 'WARN' => $VAR1->{'DEBUG'},
                 'INFO' => $VAR1->{'DEBUG'},
                 'num_appenders' => 0,
                 'is_FATAL' => sub { "DUMMY" },
                 'category' => 'autom_controller.log',
                 'ALL' => $VAR1->{'TRACE'}
               }, 'Log::Log4perl::Logger' );
mittwingate commented 6 years ago

Hi pokhriyalp,

the appender_by_name() method is part of Log::Log4perl, not Log::Log4perl::Logger: https://github.com/mschilli/log4perl/blob/master/lib/Log/Log4perl.pm#L2436 so if you call Log::Log4perl->appender_by_name("SCREEN") you should receive what you expect. Hope that helps!

mohawk2 commented 4 years ago

There's also an FAQ entry. I'm closing this, but please reopen if there are still problems!