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
newis running), it willdie(). 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 toSTDERRand thelog()method just returns β discarding the message. Gone. Poof. π - BUT if the option
silent_recoveryis 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_warningoption 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
- opensuse-leap 16.0: perl-Log-Log4perl 1.57-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms