Search code examples
perllog4perloutputdebugstring

How to Log an OutputDebugString via Log4Perl


it is possible to log all messages/levels with Log4Perl directly into an OutputDebugString (Windows System)?

I have some modules which use already log4perl, but now i want to use these modules in an environment where all log messages are OutputDebugStrings - in this environment the messages are read with DbgView and I want to read the output of my modules also with DbgView. I don't want to merge the Log4Perl Log files with the DbgView output.

I write OutputDebugStrings directly from Perl using the following perl code:

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

I found the log4net.Appender.OutputDebugStringAppender - How can I achieve the same for Perl

Thanks in advance.


Solution

  • ==== found a solution ==== thanks to daxim i wrote my own appender. Here is the source

    -generate a perl package with the following code

    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;
    

    -generate a Log4Perl config file with the following

    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
    

    -initialize the logger in your Perl script

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

    Et voilà - all logging messages go through OutputDebugStrings

    Thanks daxim for help.