perl - 我可以使用 Log::Log4perl 将一些结果写入文件而不依赖于默认级别吗?

标签 perl logfile log4perl

我想将一些结果记录到文件中,而不弄乱日志的级别。是否可以使用 Log::Log4perl 来做到这一点。我试图遵循文档,但我只能发现它取决于打印文件的日志级别?如here :

og::Log4perl->easy_init( { level    => $DEBUG,
                            file     => ">>test.log",
                            layout   => '%F{1}-%L-%M: %m%n' },
                          { level    => $DEBUG,
                            file     => "STDOUT",
                            layout   => '%m%n' },
                        );

但我想继续在屏幕上记录我的内容,只将其他消息放入日志文件中。但我一直没能找到一种方法将结果写入日志:

use strict;
use warnings;
use Log::Log4perl;
use Win32::Console::ANSI;

my $results = "result.txt";
my $conf = q(
  log4perl.appender.SCREEN         = Log::Log4perl::Appender::ScreenColoredLevels
  log4perl.appender.SCREEN.layout  = Log::Log4perl::Layout::PatternLayout
  log4perl.appender.SCREEN.color.INFO = bright_white    
  log4perl.appender.SCREEN.color.WARN = bright_yellow    
  log4perl.appender.SCREEN.color.ERROR = bright_red    
  log4perl.appender.SCREEN.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] %m  %50C::%L %n
);

my $log_level = "TRACE";
my $log_level_conf = "log4perl.category.".__FILE__." = ".$log_level.", SCREEN\n";   
$conf = $log_level_conf.$conf;
    
Log::Log4perl::init( \$conf );

my $log = Log::Log4perl::get_logger(__FILE__);

$log->info("INFO");
$log->debug("DEBUG");
$log->error("error");
$log->fatal("FATAL");
$log->trace("TRACE");

print "\n\n";

$log->info("Im doing foo");
$log->debug( "doing foo");
$log->trace( "crap from foo");
$log->info( "Im doing bar");
$log->debug( "Im doing foo");
$log->trace( "crap from bar");

# $log->result "Foo and bar done";

显示结果的行就是我正在寻找的行,通过某种方式可以使用相同的 $log 将不同的函数写入日志。

这可能吗?

编辑:按照@amit bhosale的建议,我仍然无法使其工作:

use strict;
use warnings;
use Log::Log4perl;
use Win32::Console::ANSI;


my $conf = q(

    log4perl.category = TRACE, AppResult

    log4perl.appender.SCREEN         = Log::Log4perl::Appender::ScreenColoredLevels
    log4perl.appender.SCREEN.layout  = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.SCREEN.color.INFO = bright_white    
    log4perl.appender.SCREEN.color.WARN = bright_yellow    
    log4perl.appender.SCREEN.color.ERROR = bright_red    
    log4perl.appender.SCREEN.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] %m  %50C::%L %n
    
    
    # Custom RESULT logs
    log4perl.filter.ResultsFilter  = Log::Log4perl::Filter::LevelMatch
    log4perl.filter.ResultsFilter.LevelToMatch  = RESULT
    log4perl.filter.ResultsFilter.AcceptOnMatch = true  
     
    log4perl.appender.AppResult = Log::Log4perl::Appender::File
    log4perl.appender.AppResult.filename = results.log
    log4perl.appender.AppResult.mode=append  
    log4perl.appender.AppResult.Filter   = ResultsFilter
    log4perl.appender.AppResult.layout  = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.AppResult.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] %m  %50C::%L %n


);

my $log_level = "TRACE";
my $log_level_conf = "log4perl.category.".__FILE__." = ".$log_level.", SCREEN\n";   
$conf = $log_level_conf.$conf;

Log::Log4perl::Logger::create_custom_level("RESULT", "WARN");
Log::Log4perl::init( \$conf );

my $log = Log::Log4perl::get_logger(__FILE__);

$log->info("INFO");
$log->debug("DEBUG");
$log->error("ERROR");
$log->warn("WARN");
$log->fatal("FATAL");
$log->trace("TRACE");

print "\n\n";

$log->info("Im doing foo");
$log->debug( "doing foo");
$log->trace( "crap from foo");
$log->info( "Im doing bar");
$log->debug( "Im doing foo");
$log->trace( "crap from bar");

$log->log('RESULT', "Foo and bar done");  

我在 test_logs.pl 第 60 行遇到 priority RESULT is not numeric at test_logs.pl line 60. 异常

最佳答案

另一种实现方式(无需创建自定义级别)。 (上述解决方案是有效的,这只是另一种方式)

Created log.conf file

############################################################
# A simple root logger with a Log::Log4perl::Appender::File 
# file appender in Perl.
############################################################
#There are six predefined log levels: FATAL, ERROR, WARN, INFO, DEBUG, and TRACE
# (in descending priority). Your configured logging level has to at least match 
#the priority of the logging message.

#If your configured logging level is TRACE, then messages logged with info(), 
#debug(), and trace() fatal(), error() and warn() will make their way through, 
#because their priority is higher or equal than the configured setting.

#This enables messages of priority TRACE or higher in the root hierarchy
# if a function/method wants a reference to the logger, it just calls the Logger's static get_logger($category) 
#method to obtain a reference to the one and only possible logger object 
#of a certain category.
log4perl.category.My.SCREEN = TRACE, Screen
#Appenders will be triggered whenever the configured logging 
#level requires a message to be logged
# log a message (display) on screen
log4perl.appender.Screen        = Log::Log4perl::Appender::ScreenColoredLevels
log4perl.appender.Screen.layout  = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Screen.color.INFO = bright_white    
log4perl.appender.Screen.color.WARN = bright_yellow    
log4perl.appender.Screen.color.ERROR = bright_red    
log4perl.appender.Screen.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5p] %m  %50C::%L %n

# RESULT appender
log4perl.category.My.Result= INFO, resultlog
# log a message to log file (in this case app.result file)
log4perl.appender.resultlog= Log::Log4perl::Appender::File
log4perl.appender.resultlog.filename = app.result
log4perl.appender.resultlog.mode=append
log4perl.appender.resultlog.layout=PatternLayout
log4perl.appender.resultlog.layout.ConversionPattern=[%p] %F %L %c - %m%n 

Main perl script

use strict;
use warnings;
use Log::Log4perl;
use Win32::Console::ANSI;

#configuration file is saved as log.conf, you need to read it in the startup section of your code
# After this done somewhere in the code, you can retrieve logger objects anywhere in the code.
# provide log configuration file path
Log::Log4perl->init("log.conf");

#Log::Log4perl uses categories to determine if a log statement 
#in a component should be executed or suppressed at the current logging level. 
#Most of the time, these categories are just the classes the log statements
my $log_screen = Log::Log4perl->get_logger("My::SCREEN");
my $log_result = Log::Log4perl->get_logger("My::Result");
# sample logging statement
$log_screen->info("INFO");
$log_screen->debug("DEBUG");
$log_screen->error("ERROR");
$log_screen->warn("WARN");
$log_screen->fatal("FATAL");
$log_screen->trace("TRACE");
# result
$log_result->info("this is a result message1");

Output to file (example)

[INFO] log.pl 24 My.Result - this is a result message1

Output to Screen (different colour)

[2020-08-16 20:52:40.721] [INFO ] INFO                                                main::16
[2020-08-16 20:52:40.723] [DEBUG] DEBUG                                                main::17
[2020-08-16 20:52:40.723] [ERROR] ERROR                                                main::18
[2020-08-16 20:52:40.725] [WARN ] WARN                                                main::19
[2020-08-16 20:52:40.726] [FATAL] FATAL                                                main::20
[2020-08-16 20:52:40.727] [TRACE] TRACE                                                main::21

关于perl - 我可以使用 Log::Log4perl 将一些结果写入文件而不依赖于默认级别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63398385/

相关文章:

c++ - 如何在 C++ 中的日志文件中写入一些跟踪

java - 我不明白 apache 访问日志文件

perl - 哪个脚本初始化模块?

perl - 运行 Build.pl 非交互式

Perl Test::More 和设置测试要求

python - 读取 ASL 日志文件 - Python

perl - 使用 log4perl 限制日志大小

perl - 如何在运行时更改 log4perl 附加程序的过滤器?

perl - 映射硬编码配置文件

bash - dc(桌面计算器)- 哈希类型