您可以通過在你的conf文件中指定custom cspec做到這一點。
# basic config to write the log to a file
log4perl.logger.mylog = DEBUG, MyLog
log4perl.appender.MyLog = Log::Log4perl::Appender::File
log4perl.appender.MyLog.filename = my.log
# create a custom cspec called 'A' with an anonymous sub.
# The sub would be called with the following args: $layout, $message, $category, $priority, $caller_level
# Then apply the regex as you like.
log4j.PatternLayout.cspec.A = sub { $_[1] =~ s/module/package/; $_[1] }
# then use the cspec here with '%A'
log4perl.appender.MyLog.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.MyLog.layout.ConversionPattern = %A%n
而且你的代碼不應該需要改變:
use Log::Log4perl;
log::Log4perl::init('log.conf');
my $logger = Log::Log4perl->get_logger('mylog');
$logger->info("Testing the logger module"); # prints 'Testing the logger package'
請出示一些代碼。你如何調用日誌記錄?你有沒有嘗試過任何東西? – simbabque