kloud has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a perl script that is intended to run various system checks
write the content to a string and then send the entire
contents of the string to syslog.
What actually happens is that only the first line of the string
variable is out to syslog, I enclose a code snippet that does exaclty
the same but is easier to read
#!/usr/local/bin/perl -w use strict; use Sys::Syslog; my $date = `date '+%d%m%Y:%H%M%S'`; my $mesg = "======== Start =======\n"; $mesg .= "TIME :\t $date\n"; $mesg .= "\n"; $mesg .= "========The end==========\n"; print $mesg; openlog( 'status', 'ndelay', 'daemon' ); syslog('warning', '%s', $mesg); closelog();
chomping $mesg does not help, the real problem is that the
string sent to syslog follows format rules as defined by
sprintf and will therefore only print out string vars to
the first newline
Is there any way around this? Thanks for your help Kloud

Replies are listed 'Best First'.
Re: Perl and syslog
by Masem (Monsignor) on Sep 18, 2001 at 17:21 UTC
    A syslog message is typically only one line (single CR) long. If a message needs to span multiple lines to avoid having a 500+ character line width, it's broken into separate messages.

    The easy way to do this with what you have is:

    my @msgs = ( "======== Start =========", $stuff, $morestuff, "======== End ==========" ); openlog( 'status', 'ndelay', 'daemon' ); map { syslog( 'warning', '%s', $_ ) } @msgs; closelog();

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

      This worked thanks,
      It was my understanding that the previous syslog requirement
      of one line messages had been removed, but then again I might well
      be wishing.


      Thanks for our help
      Kloud
Re: Perl and syslog
by MZSanford (Curate) on Sep 18, 2001 at 17:23 UTC
    I would imagine, and i am not expert, that this is a limitation to maintain consistency in the syslog. If it will not let you print newlines into the syslog, it will maintain having all lines in the correct format (date, etc). There maybe a way around, but i would guess it is probably better to write to syslog multiple times and let them all be well formed. Not much help, just my $0.02
    my own worst enemy
    -- MZSanford
      Technically you are correct but I was doing something far
      nastier before by simply concatenating into syslog, and
      occasionally causing syslogd to choke, with file violation
      errs....

      Thanks for your help
      Kloud