How to reopen error log for rotation ? #55

Open
coxx opened this Issue Nov 21, 2012 · 9 comments

Comments

Projects
None yet
6 participants

coxx commented Nov 21, 2012

What is the right way to reopen log file (--error-log) for graceful rotation ?

Would like to know this too. Need a signal number to send, or some run flag, or something else. And this function (log file reopen) should not delay requests processing and i suppose should not force starman to reread psgi app.

Owner

miyagawa commented Jan 6, 2013

Just since you guys were asking "how" - the answer is that it's not supported as of currently.

coxx commented Jan 7, 2013

Thanks Tatsuhiko! "Not supported" is enough for "how to".

I'm looking at implementing a workaround to do this using the pluggable logging facility of Starman::Server's base class Net::Server in combination with Log::Log4perl's ability to re-open log files via the Log::Log4perl::Appender::File recreate_check_signal option. However, this requires passing command line parameters that Net::Server can access and Starman::Server->run doesn't pass them on. (starman passes unknown parameters to Plack::Runner and they get stored as key value pairs in $self->{options} but they don't make it to Net::Server which expects them in $self->{server}.)

I put a rather broad hack in that works for me but my setup doesn't really exercise all of the command line parameters so I'm not sure if the hack breaks anything else. I tried to run it against the test suite using "cpanm --look starman" but I don't know my way around the build dir structure well enough to get anything done and there weren't any instructions.

Here is the diff if you're interested:

*** lib/Starman/Server.pm       2013-04-06 22:04:57.000000000 -0700
--- lib/Starman/Server.new.pm   2013-04-17 11:14:23.121528622 -0700
***************
*** 66,69 ****
--- 67,74 ----

      $self->SUPER::run(
+         # Pass all raw command line options to our parent Which expects them
+         # here. Any that need to be modified by our option parsing get
+         # clobbered below
+         %$options,
          port                       => $port,
          host                       => $host,

Could we not simply change the logging so it closes/reopens the filehandle on every write? This file doesn't usually get written to that often, so it wouldn't be inefficient.

@karenetheridge that would be one approach to adding this functionality. So would just re-opening the handle on HUP. But, Net::Server doesn't offer an API for directly affecting the filehandle. The filehandle itself appears to be intended as private so I am hesitant to add code that depends upon it's implementation. One alternative would be to override the methods that access the filehandle, however those are big methods and it seemed like that couldn't be done easily without a lot of copy pasta. Another alternative would be to submit a patch to Net::Server that exposed the filehandle for fiddling.

I opted for the code I shared above because it's changes far fewer lines of code, uses the parent's exposed API and adds exposure to other parts of the parent's API. The downside is that the pluggable logging does require more configuration up-front.

Collaborator

lestrrat commented Apr 19, 2013

@benrifkah I know you want to use Log4perl et al, but just as a FYI, we use this setup at $work:

use strict;
use Plack::Builder;
use File::RotateLogs;

my $roratelogs = File::RotateLogs->new(
    logfile         => "/path/to/logfile.%Y%m%d%H",
    linkname        => "/path/to/logfile",  # symlink pointing to newest one
    rotationtime    => 7200, # when to rotate logs (short, because we get a LOT of hits)
    maxage          => 86400 * 14, # when to purge old olgs
);

builder {
     enable 'AccessLog', logger => sub { $rotatelogs->print(@_) };
     $app;
};

@lestrrat that is a great way to ensure that the ACCESS log gets rotated. It is similar to what we're doing with Log4perl. However, I think you'll find that it doesn't work for the ERROR log which is what this thread is about. Since the error log filehandle is owned by Net::Server which isn't aware of Plack::Builder it would take breaking some API boundaries to make it configurable via middleware.

Collaborator

lestrrat commented Apr 19, 2013

@benrifkah Oh I see. My bad. Again, just FYI we don't worry about doing error log locations because we just print it to STDERR and let daemontools' multilog handle the log management.

@gmcharlt gmcharlt pushed a commit to Koha-Community/Koha that referenced this issue Apr 1, 2016

@tomascohen @brendan1226 tomascohen + brendan1226 Bug 16138: Restart plack when rotating logfiles
On log rotation Starman/Plack keeps failing (Auth.pm related problems) due to
permission issues:

Could not compile /usr/share/koha/opac/cgi-bin/opac/opac-search.pl: Can't locate Authen/CAS/Client/Response/Failure.pm
:   ./Authen/CAS/Client/Response/Failure.pm: Permission denied at /usr/share/perl/5.20/base.pm line 97.
        ...propagated at /usr/share/perl/5.20/base.pm line 106.
BEGIN failed--compilation aborted at /usr/share/perl5/Authen/CAS/Client/Response.pm line 68.
Compilation failed in require at /usr/share/perl5/Authen/CAS/Client.pm line 8.
BEGIN failed--compilation aborted at /usr/share/perl5/Authen/CAS/Client.pm line 8.
Compilation failed in require at /usr/share/koha/lib/C4/Auth_with_cas.pm line 25.
BEGIN failed--compilation aborted at /usr/share/koha/lib/C4/Auth_with_cas.pm line 25.
Compilation failed in require at /usr/share/koha/lib/C4/Auth.pm line 63.
BEGIN failed--compilation aborted at /usr/share/koha/lib/C4/Auth.pm line 90.
Compilation failed in require at /usr/share/koha/opac/cgi-bin/opac/opac-search.pl line 34.
BEGIN failed--compilation aborted at /usr/share/koha/opac/cgi-bin/opac/opac-search.pl line 34.

As we do with zebra daemons, starman should be restarted as it doesn't handle log file rotation
gracefully[1].

I'm not sure how to reproduce it on a dev environment.

[1] miyagawa/Starman#55

Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
93efeab

@gmcharlt gmcharlt pushed a commit to Koha-Community/Koha that referenced this issue Apr 6, 2016

@tomascohen @jajm tomascohen + jajm Bug 16138: Restart plack when rotating logfiles
On log rotation Starman/Plack keeps failing (Auth.pm related problems)
due to permission issues:

Could not compile /usr/share/koha/opac/cgi-bin/opac/opac-search.pl: Can't locate Authen/CAS/Client/Response/Failure.pm
:   ./Authen/CAS/Client/Response/Failure.pm: Permission denied at /usr/share/perl/5.20/base.pm line 97.
        ...propagated at /usr/share/perl/5.20/base.pm line 106.
BEGIN failed--compilation aborted at /usr/share/perl5/Authen/CAS/Client/Response.pm line 68.
Compilation failed in require at /usr/share/perl5/Authen/CAS/Client.pm line 8.
BEGIN failed--compilation aborted at /usr/share/perl5/Authen/CAS/Client.pm line 8.
Compilation failed in require at /usr/share/koha/lib/C4/Auth_with_cas.pm line 25.
BEGIN failed--compilation aborted at /usr/share/koha/lib/C4/Auth_with_cas.pm line 25.
Compilation failed in require at /usr/share/koha/lib/C4/Auth.pm line 63.
BEGIN failed--compilation aborted at /usr/share/koha/lib/C4/Auth.pm line 90.
Compilation failed in require at /usr/share/koha/opac/cgi-bin/opac/opac-search.pl line 34.
BEGIN failed--compilation aborted at /usr/share/koha/opac/cgi-bin/opac/opac-search.pl line 34.

As we do with zebra daemons, starman should be restarted as it doesn't
handle log file rotation gracefully[1].

I'm not sure how to reproduce it on a dev environment.

[1] miyagawa/Starman#55

Signed-off-by: Mirko Tietgen <mirko@abunchofthings.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
(cherry picked from commit 93efeab)
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>
107e6de
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment