From: Mike S. <m...@pe...> - 2007-03-10 23:33:17
|
On Sat, 10 Mar 2007, [iso-8859-1] J=FCrgen Sch=F6neberg wrote: > but I thought this solution with > perl.appender.FileApp.filename =3D sub { "$0.log" } > is just for defining a dynamic name for the log file at initialization ph= ase > of Log4perl (only once in the beginning of the perl script) > > but what I want to do is > - have one main script (called run.pl) with the Log4perl initialization (= only > here) > > - execute other scripts in a loop (1.pl ... 10.pl) from run.pl > (in these scripts I dont have a new Log4perl initialization ,(so no 'fil= ename > =3D sub { "$0.log" }' ) Ok, so I'm assuming you're "executing" scripts this way in your main program: for my $file (qw(t1 t2)) { require "$file.pl"; } which means they're running within the same process (if they were being run in different subprocesses, you would need to use a different logging approach altogether). And let's also assume that your main program initializes Log4perl like this: log4perl.category =3D DEBUG, Logfile log4perl.appender.Logfile =3D Log::Log4perl::Appender::File log4perl.appender.Logfile.filename =3D test.log log4perl.appender.Logfile.layout =3D Log::Log4perl::Layout::PatternLayout log4perl.appender.Logfile.layout.ConversionPattern =3D %m%n Specifics don't matter, just note that the name of the file appender you're using "Logfile". So if you want to have this file appender switch its log file every time you require/run one of your 'tN.pl' scripts, simply use my $app =3D Log::Log4perl->appender_by_name("Logfile"); for my $file (qw(t1 t2)) { $app->file_switch("$file.log"); require "$file.pl"; } and a script named "t1.pl" will log to "t1.log", while another script, named "t2.pl" will use "t2.log". Closer to what you were looking for? -- Mike Mike Schilli m...@pe... > > - I just want to start and write to a new logfile when I start a new scri= pt in > the loop > > - the scripts are using a old subroutine inside , Prints::print_log($text= ) > and I changed Prints::print_log($text) to > > package Prints; > sub print_log { > $logger->info(shift); > } > > the scripts I cant change (no log4perl statements inside them) > > so the change of the name for the logfile has to be done in the main scri= pt > run.pl > > so I added for every new script a new FileApp with the right logfile name= , > executed the script and removed the appender. This works, but I dont have= the > possibility > to configure the FileApp with the Log4perl configuration file because eve= ry > appender has a differnt name. > I have to do it inside the code of run.pl which is not so nice. > > > But I still wonder how to do this in the 'right and easy' way. > > or maybe > perl.appender.FileApp.filename =3D sub { "$0.log" } > was already the solution (I havent tried)?? > > Thanks for your help > J=FCrgen > > > > > > > > > > > > > > > > > > On Friday 09 March 2007 23:06, Mike Schilli wrote: > > On Fri, 9 Mar 2007, [iso-8859-1] J=FCrgen Sch=F6neberg wrote: > > > the problem: > > > > > > I want to execute a number of different perl scripts (named > > > 1.pl..10.pl in the code below) . > > > > Just use a subroutine in your Log4perl configuration: > > > > log4perl.appender.FileApp.filename =3D sub { "$0.log" } > > > > This will even work if you stuff your l4p configuration into a config > > file. > > > > -- Mike > > > > Mike Schilli > > m...@pe... > > > > > every executed file should be logged to a corresponding logfile (1.lo= g .. > > > 10.log) > > > > > > I want to have the same layout for the log in the files as on the scr= een > > > and want to configure all with a configuration file for log4perl > > > (in the code below its done in a string instead) > > > > > > > > > what is the recommended way to change the name of the logfiles in > > > runtime? > > > > > > I experimented a little with > > > add_appender and remove_appender under runtime > > > > > > what's with file_switch($new_file_log); > > > how can I use this? > > > > > > thanks for some explanations or even better some code examples! > > > thank you for help > > > > > > Juergen > > > > > > > > > > > > > > > here the frame code: > > > > > > #!/usr/bin/perl > > > > > > use warnings; > > > use strict; > > > > > > use Log::Log4perl qw(get_logger); > > > > > > # Define configuration > > > my $conf =3D q( > > > log4perl.logger =3D ERROR, FileApp, ScreenApp > > > log4perl.appender.FileApp =3D Log::Log4perl::Appender::F= ile > > > log4perl.appender.FileApp.filename =3D test.log > > > log4perl.appender.FileApp.layout =3D PatternLayout > > > log4perl.appender.FileApp.layout.ConversionPattern =3D %d> %m%n > > > > > > log4perl.appender.ScreenApp =3D > > > Log::Log4perl::Appender::Screen log4perl.appender.ScreenApp.layout = =3D > > > PatternLayout > > > log4perl.appender.ScreenApp.layout.ConversionPattern =3D %d> %m%n > > > > > > > > > ); > > > > > > # Initialize logging behaviour > > > Log::Log4perl->init( \$conf ); > > > > > > # Obtain a logger instance > > > my $logger =3D get_logger("test"); > > > > > > > > > foreach my $new_file (1..10) { > > > > > > $new_file_log .=3D '.log'; > > > > > > # > > > # do '$new_file'.'.pl'; > > > # > > > > > > } > > > > > > > > > > > > ---------------------------------------------------------------------= ---- > > > Take Surveys. Earn Cash. Influence the Future of IT > > > Join SourceForge.net's Techsay panel and you'll get the chance to sha= re > > > your opinions on IT & business topics through brief surveys-and earn = cash > > > http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CI= D=3DDEVDEV > > > _______________________________________________ > > > log4perl-devel mailing list > > > log...@li... > > > https://lists.sourceforge.net/lists/listinfo/log4perl-devel > |