[Chicago-talk] writing a log with Cron, Daemons, Perl
Ed Summers
ehs at pobox.com
Fri Sep 5 10:27:41 CDT 2003
On Fri, Sep 05, 2003 at 09:50:37AM -0500, Jay Strauss wrote:
> 1) how do I get stuff into my log file without closing the file handle? I
> tried $|++; For example:
>
> use Net::Server::Daemonize qw(daemonize);
> daemonize ("jstrauss","jstrauss","/home/jstrauss/test.pid");
> open (LOG,">/home/jstrauss/bin/test.log");
> print LOG "Help\n";
> while (1) {
> }
>
> my message never gets printed to the file.
Remember $| only works for the currently selected output filehandle. So you
will have to select your LOG filehandle then set $|.
open( LOG, ">/home/jstrauss/bin/test.log");
select( LOG );
$| = 1;
select( STDOUT );
I like using IO::File for this sort of thing myself, since it looks
prettier (IMHO).
use IO::File;
my $log = File::Handle->new();
$log->open( ">/home/jstrauss/bin/test.log" );
$log->autoflush( 1 );
print $log "blah";
You can mix and match too:
use IO::File;
open( LOG, ">/home/jstrauss/bin/test.log");
LOG->autoflush( 1 );
print LOG "blah";
> 2) how would I redirect all of STDERR so that my messages appear in the
> proper order? for example:
> #!/usr/bin/perl
> open (LOG,">/home/jstrauss/bin/test.log");
> open (STDERR, ">>&LOG");
> $|++;
> print LOG "yep\n";
> die "you jerk";
>
> :~/bin> cat test.log
> you jerk at /home/jstrauss/bin/jay line 16.
> yep
Not sure about this one.
//Ed
--
Ed Summers
aim: inkdroid
web: http://www.inkdroid.org
The deeper I go the darker it gets. [Peter Gabriel]
More information about the Chicago-talk
mailing list