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

I need to write very simple print statements to the rsyslog using perl. I can't use Log4perl.
my $MYMESSAGE = "My string"; my $DATE=`date +%Y-%m-%dT%H:%M:%S%:z`; chomp $DATE; $LOG_HDR = "<14>1 $DATE localhost scrubdisk $$ scrubdisk - "; open (LOG, '>>', '/dev/tcp/localhost/601'); print LOG "$LOG_HDR $MYMESSAGE"; close (LOG);

My header with the date/time stamps are all fine. My bash script works just fine with a simple echo statement, but perl does not. I have can not install any new perl modules. I would like to avoid writing 10+ lines of code to write a simple message to log file.

Replies are listed 'Best First'.
Re: Write to rsyslog
by hippo (Archbishop) on May 16, 2024 at 08:06 UTC
    I can't use Log4perl.

    I can use Log4perl but I choose not to. Perhaps you can use Sys::Syslog instead - writing to syslog is precisely what it is for, after all. I like Log::Any so I would be using Log::Any::Adapter::Syslog. TIMTOWTDI!

    My bash script works just fine with a simple echo statement, but perl does not.

    See How to ask better questions using Test::More and sample data. What you have provided here is a description of the problem entirely devoid of details and therefore nobody can help you diagnose or fix it.


    🦛 "It doesn't work" is as useful to a developer as "I'm ill" is to a medic. Try to be specific. SSCCE is best.

Re: Write to rsyslog
by NERDVANA (Priest) on May 16, 2024 at 17:12 UTC
    Your problem is that "/dev/tcp" is a virtual path only understood by bash. You can't use that path from any other environment.

    In perl, you would need to actually open that socket, for example using IO::Socket::INET

    I don't recommend that, though, since there are many other ways to interact with syslog. If regular syslog is ok (as opposed to rsyslog) you can use Perl's built-in Sys::Syslog module.

    SYNOPSIS use Sys::Syslog; # all except setlogsoc +k() use Sys::Syslog qw(:standard :macros); # standard functions & + macros openlog($ident, $logopt, $facility); # don't forget this syslog($priority, $format, @args); $oldmask = setlogmask($mask_priority); closelog();
Re: Write to rsyslog
by GrandFather (Saint) on May 16, 2024 at 03:07 UTC
Re: Write to rsyslog
by eyepopslikeamosquito (Archbishop) on May 16, 2024 at 05:57 UTC

    My bash script works just fine with a simple echo statement, but perl does not.

    Well, show us your working bash script.

    (I hope some wag doesn't suggest converting it to perl via merlyn's famous sh2perl translator :-)

    👁️🍾👍🦟
      Since (quote)...

        Sys::Syslog is an interface to the UNIX syslog(3) program.

      ... I'm supposing the OP wants to know how to use system or `backticks` in Perl, because calling CLI programs directly is interwoven into bash.

      Saying so, there even was a ACME'ish Shell module from Larry to implement transparent system calls either via AUTOLOAD or by importing them explicitly.

      Edit

      FWIW it used to be in core

        corelist Shell

        Shell was first released with perl 5, deprecated (will be CPAN-only) in v5.11.0 and removed from v5.15.0

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

Re: Write to rsyslog
by LanX (Saint) on May 16, 2024 at 01:00 UTC
    Why don't you write a subroutine syslog for this, and call it with syslog("MY MESSAGE") ?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery