1

是否可以使用 Log4Perl 将所有消息/级别直接记录到 OutputDebugString(Windows 系统)中?

我有一些已经使用 log4perl 的模块,但现在我想在所有日志消息都是 OutputDebugStrings 的环境中使用这些模块 - 在这种环境中,消息是用 DbgView 读取的,我也想用 DbgView 读取我的模块的输出。我不想将 Log4Perl 日志文件与 DbgView 输出合并。

我使用以下 perl 代码直接从 Perl 编写 OutputDebugStrings:

use Win32::API::OutputDebugString qw(OutputDebugString DStr);
OutputDebugString("Foo bar", "baz\n"); # sends  Foo barbaz\n to the debugger

我找到了 log4net.Appender.OutputDebugStringAppender - 我怎样才能为 Perl 实现相同的目标

提前致谢。

4

1 回答 1

3

==== 找到了解决方案 ==== 感谢 daxim,我编写了自己的附加程序。这里是源

- 使用以下代码生成 perl 包

package Log4PerlOutputDebugStringAppender;

sub new {
   my($class, %options) = @_;
   my $self = { %options };
   bless $self, $class;
   return $self;
}

sub log {
   my($self, %params) = @_;
   use Win32::API::OutputDebugString qw(OutputDebugString DStr);
   OutputDebugString( "$params{message}" );
}

1;

- 使用以下内容生成 Log4Perl 配置文件

log4perl.logger = INFO, OutputDebugString
log4perl.appender.OutputDebugString=Log4PerlOutputDebugStringAppender
log4perl.appender.OutputDebugString.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.OutputDebugString.layout.ConversionPattern=[%-5p] %-15.15c{1} (%4L) - %m%n

- 在 Perl 脚本中初始化记录器

use Log::Log4perl qw(:easy);
Log::Log4perl->init("logconf.txt");
INFO("INFO");
WARN("WARN");
FATAL("FATAL");

等等 - 所有日志消息都通过 OutputDebugStrings

感谢大信的帮助。

于 2013-07-25T12:32:45.347 回答