miyagawa / Starman

Starman is a high-performance preforking Perl PSGI web server
http://search.cpan.org/dist/Starman
Other
287 stars 84 forks source link

Added options to pass 'Log::Log4perl' as logger to Net::Server #121

Closed Elhodred closed 8 years ago

Elhodred commented 8 years ago

As Net::Server is able to use Log::Log4perl as logger, we can leverage it and use some of the advanced options that Log4perl provides. For instance, a USR1 signal can be used to recreate the logs (hello logrotate). Here an example:

use strict;
use warnings;
use Plack::Handler::Starman;
use Plack::Builder;

$SIG{'USR1'} = 'IGNORE';

sub start {
        my $app = sub {
                die 'Hello World crashed!!!';
        };
        builder {
                mount '/' => builder { $app; };
        }
}

my $log_config = {
        "log4perl.rootLogger" => "INFO,LOGFILE",
        "log4perl.appender.LOGFILE" => "Log::Log4perl::Appender::File",
        "log4perl.appender.LOGFILE.filename" => "/var/log/test.log",
        "log4perl.appender.LOGFILE.layout"   => "Log::Log4perl::Layout::PatternLayout",
        "log4perl.appender.LOGFILE.layout.ConversionPattern" => "[%r] %F %L %m%n",
        "log4perl.appender.LOGFILE.recreate" => 1,
        "log4perl.appender.Logfile.recreate_check_signal" => "USR1"
};

my $server = Plack::Handler::Starman->new(listen => ["127.0.0.1:8080"], daemonize => 1, error_log => 'Log::Log4perl', log4perl_conf => $log_config);
$server->run(start());

With that example, you can remove /var/log/test.log, send a USR1 signal to the master process and the file will be recreated.

davidcsi commented 8 years ago

Nice! Very interesting!

miyagawa commented 8 years ago

You don't need this patch, Starman has an option to pass Net::Server options in net_server_args.

Elhodred commented 8 years ago

Sometimes I think I'm blind. You are right, I didn't realise about net_server_args. Thanks.