您可以通过在 conf 文件中指定自定义 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'