Appender::Socket(3)

πŸ’– NAME πŸ’–

Log::Log4perl::Appender::Socket β€” the appender that literally just logs to a socket, bestie. That's it, that's the whole vibe. πŸ¦‹

(also, hi, you're reading the Appender::Socket(3) page from the User Contributed Perl Documentation β€” welcome to the group chat ✨)


✨ SYNOPSIS ✨

use Log::Log4perl::Appender::Socket;

my $appender = Log::Log4perl::Appender::Socket->new(
  PeerAddr => "server.foo.com",
  PeerPort => 1234,
);

$appender->log(message => "Log me\n");

okay so that's the whole thing. build it, log to it, slay. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

It's a simple appender for writing to a socket β€” no cap, "simple" is doing a lot of work here and we love that for it. It relies on IO::Socket::INET and offers all the parameters that module offers, so whatever you can pass to IO::Socket::INET, you can pass here. iykyk. πŸ’œ

When the object gets destroyed, any pending messages get flushed and the socket gets closed. Cleanup queen behavior. 🧹

Now the important part, so pay attention bestie:

  • If the appender can't contact the server during the initialization phase (that's while the constructor new is running), it will die(). Like, actually die. Not a vibe check, a real one. πŸ’€
  • If the appender fails to log a message because the socket's send() method fails (most likely because the server went down 😭), it will try to reconnect once. If the reconnect succeeds, the message gets sent. If the reconnect fails, a warning is sent to STDERR and the log() method just returns β€” discarding the message. Gone. Poof. πŸ™ˆ
  • BUT if the option silent_recovery is given to the constructor and set to a true value, the behaviour is different: if the socket connection can't be established at initialization time, a single warning is issued instead. After that, every log attempt will try to establish the connection and discard the message silently if it fails. So it's trying its best, quietly. 🫢
  • And if you don't even want that one warning? Set the no_warning option to a true value. Full silence. Not a peep. 🀫
🌸 real talk: connecting at initialization time may not be the best option when you're running under Apache1 Apache2/prefork, because the parent process creates the socket and the connections are shared among the forked children β€” all the children writing to the same socket could intermingle messages, which is chaos and not the cute kind. So instead of doing that, you can use defer_connection, which puts off making the connection until the first log message is sent. Iconic, honestly. πŸ‘‘

πŸ”₯ EXAMPLE πŸ”₯

Wanna write a server quickly? Use the IO::Socket::INET module, it's giving main character energy:

use IO::Socket::INET;

my $sock = IO::Socket::INET->new(
    Listen    => 5,
    LocalAddr => 'localhost',
    LocalPort => 12345,
    Proto     => 'tcp');

while(my $client = $sock->accept()) {
    print "Client connected\n";
    while(<$client>) {
        print "$_\n";
    }
}

Start that up, and then run the following script as a client:

use Log::Log4perl qw(:easy);

my $conf = q{
    log4perl.category                  = WARN, Socket
    log4perl.appender.Socket           = Log::Log4perl::Appender::Socket
    log4perl.appender.Socket.PeerAddr  = localhost
    log4perl.appender.Socket.PeerPort  = 12345
    log4perl.appender.Socket.layout    = SimpleLayout
};

Log::Log4perl->init(\$conf);

sleep(2);

for(1..10) {
    ERROR("Quack!");
    sleep(5);
}

and that's literally it. server + client = matched set, obsessed. πŸ’–


πŸ‘‘ LICENSE πŸ‘‘

Copyright 2002-2013 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πŸŽ€


🌈 AUTHOR 🌈

Please contribute patches to the project on Github, bestie:

http://github.com/mschilli/log4perl

Send bug reports or requests for enhancements to the authors via our

MAILING LIST (questions, bug reports, suggestions/patches): log4perl-devel@lists.sourceforge.net

Authors (please contact them via the list above, not directly): Mike Schilli <m@perlmeister.com>, Kevin Goess <cpan@goess.org>

Contributors (in alphabetical order): Ateeq Altaf, Cory Bennett, Jens Berthold, Jeremy Bopp, Hutton Davidson, Chris R. Donnelly, Matisse Enzer, Hugh Esco, Anthony Foiani, James FitzGibbon, Carl Franks, Dennis Gregorovic, Andy Grundman, Paul Harrington, Alexander Hartmaier David Hull, Robert Jacobson, Jason Kohles, Jeff Macdonald, Markus Peter, Brett Rann, Peter Rabbitson, Erik Selberg, Aaron Straup Cope, Lars Thegler, David Viner, Mac Yang.


✨ and that's the whole page!! ✨ this little gem is from perl v5.42.0, dated 2022-10-22, and it's page Appender::Socket(3) β€” go forth and log to your socket, ily, bye πŸ’œπŸ¦‹πŸŽ€

πŸ“¦ where this page comes from