From: Livshits, A. G. <Ale...@rb...> - 2007-03-23 13:27:32
|
Yes, this is exactly what I wished for!!! I can reuse existing subs to=0D= =0Aprint into log4perl logger file=2E Thanks a lot=2E=0D=0A=0D=0A-----Origi= nal Message-----=0D=0AFrom: Mike Schilli [mailto:m@perlmeister=2Ecom] =0D= =0ASent: Friday, March 23, 2007 1:47 AM=0D=0ATo: Livshits, Alex, GCM=0D=0AC= c: log4perl-devel@lists=2Esourceforge=2Enet=0D=0ASubject: Re: [log4perl-dev= el] How to print to log file via file handle=0D=0Aor stderr, stdout?=0D=0A= =0D=0A=0D=0AOn Thu, 22 Mar 2007, Livshits, Alex, GCM wrote:=0D=0A=0D=0A> I = got standard error header message printing environment variables=0D=0Awhich= =0D=0A> takes either file handle or stdout, stderr=2E What's the way to pri= nt it=0D=0A> to the logger?=0D=0A=0D=0ALet me summarize to make sure I unde= rstand correctly: You have a=0D=0Afunction=0D=0Athat prints to a filehandle= =2E You want to tie into that filehandle and=0D=0Aforward all arriving mess= ages to a Log4perl logger=2E=0D=0A=0D=0AFirst, let's write a package that t= ies a file handle and forwards it=0D=0Ato a Log4perl logger:=0D=0A=0D=0A = package FileHandleLogger;=0D=0A use Log::Log4perl qw(:levels get_logger= );=0D=0A=0D=0A sub TIEHANDLE {=0D=0A my($class, %options) =3D @_;= =0D=0A=0D=0A my $self =3D {=0D=0A level =3D> $DEBUG,=0D= =0A category =3D> '',=0D=0A %options=0D=0A };=0D= =0A=0D=0A $self->{logger} =3D get_logger($self->{category}),=0D=0A = bless $self, $class;=0D=0A }=0D=0A=0D=0A sub PRINT {=0D=0A = my($self, @rest) =3D @_;=0D=0A $Log::Log4perl::caller_depth++;=0D= =0A $self->{logger}->log($self->{level}, @rest);=0D=0A $Log::= Log4perl::caller_depth--;=0D=0A }=0D=0A=0D=0A sub PRINTF {=0D=0A = my($self, $fmt, @rest) =3D @_;=0D=0A $Log::Log4perl::caller_depth= ++;=0D=0A $self->PRINT(sprintf($fmt, @rest));=0D=0A $Log::Log= 4perl::caller_depth--;=0D=0A }=0D=0A=0D=0A 1;=0D=0A=0D=0ANow, if you = have a function like=0D=0A=0D=0A sub function_printing_to_fh {=0D=0A = my($fh) =3D @_;=0D=0A printf $fh "Hi there!\n";=0D=0A }=0D=0A= =0D=0Awhich takes a filehandle and prints something to it, it can be used= =0D=0Awith Log4perl:=0D=0A=0D=0A use Log::Log4perl qw(:easy);=0D=0A u= sa FileHandleLogger;=0D=0A=0D=0A Log::Log4perl->easy_init($DEBUG);=0D=0A= =0D=0A tie *SOMEHANDLE, 'FileHandleLogger' or=0D=0A die "tie fail= ed ($!)";=0D=0A=0D=0A function_printing_to_fh(*SOMEHANDLE);=0D=0A = # prints "2007/03/22 21:43:30 Hi there!"=0D=0A=0D=0AIf you want, you can e= ven specify a different log level or category:=0D=0A=0D=0A tie *SOMEHAND= LE, 'FileHandleLogger',=0D=0A level =3D> $INFO, category =3D> "Foo::= Bar" or die "tie failed ($!)";=0D=0A=0D=0AIs this what you're looking for?= =0D=0A=0D=0A-- Mike=0D=0A=0D=0AMike Schilli=0D=0Am@perlmeister=2Ecom=0D=0A= =0D=0A-----------------------------------------=0D=0A**********************= *********************************************=0D=0A*=0D=0A=0D=0AThis e-mail= is intended only for the addressee named above=2E=0D=0AAs this e-mail may = contain confidential or privileged information,=0D=0Aif you are not the nam= ed addressee, you are not authorized=0D=0Ato retain, read, copy or dissemin= ate this message or any part of=0D=0Ait=2E=0D=0A=0D=0A*********************= **********************************************=0D=0A*=0D=0A |