Dear Monks,

i am trying to write a program that does the follwoing:

1. Open 3 threads which write to the same filename.

2. Every 24h one of the threads will close the file (which all 3 threads write too and open a new file for all the 3 threads).

A friend of my was trying to help me with this task and he wrote me a program that uses log4perl and SIG, but the this code have a problem, after i send the SIG kill -QUIT pid it closed the file but doesn't write again to the new file (i change the file name in the conf file before i send the SIG):

here is the code:
#!/usr/bin/perl -w use threads; use threads::shared; use Log::Log4perl qw(get_logger :levels); Log::Log4perl->init_and_watch('/home/kg/log.conf', 'QUIT'); my $logger = get_logger(); $logger->level($INFO); my $thr1 = threads->create(\&thread1); my $thr2 = threads->create(\&thread2); my $thr3 = threads->create(\&thread3); $SIG{INT} = sub { my $sig = shift; my $tid = threads->tid(); print "Thread $tid received $sig\n"; foreach my $thr (threads->list()) { print "Sending QUIT to thread with tid ", $thr->tid, "\n"; $thr->kill('QUIT'); } }; print "PID: $$\n"; while () { sleep 10; my $date = `date`; chomp $date; print $date, ": I'm the main program\n"; } sub thread1 { while () { my $logger = get_logger(); $logger->info("I'm thread1"); sleep 10; } } sub thread2 { my $logger = get_logger(); while () { $logger->info("I'm thread2"); sleep 10; } } sub thread3 { my $logger = get_logger(); while () { $logger->info("I'm thread3"); sleep 10; } }

and log.conf file:

############################################################ # A simple root logger with a Log::Log4perl::Appender::File # file appender in Perl. ############################################################ log4perl.rootLogger=ERROR, LOGFILE log4perl.appender.LOGFILE=Log::Dispatch::File log4perl.appender.LOGFILE.filename=/home/kg/myerrs.log log4perl.appender.LOGFILE.mode=append log4perl.appender.LOGFILE.layout=PatternLayout log4perl.appender.LOGFILE.layout.ConversionPattern=%d - [%r] %F %L %c +- %m%n

Remarks, before i send the SIG, i changed the file name. e.g:

log4perl.appender.LOGFILE.filename=/home/kg/myerrs.log

to

log4perl.appender.LOGFILE.filename=/home/kg/myerrs1.log

then sending kill -QUIT pid the program closed the file, creat new file myerrs1.log, but dont write to it...

My questions

1. what do i need to change that the program would write the the new file

.

2. i would like instead of a fixed string as file name to give a function that each time a SIG is sent a new filename would be generated accordnig to my function. Is it possible?


In reply to passing multiply threads SIG which close\open log file using log4perl by Adler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.