2

I am the author of the Log4perl::KISS module, and I want to convert the log_open function so that it can switch the current log file on the fly at any time without full Log::Log4perl reinitialization.

I know that the file appender in Log4perl can switch to the file with the same name (this possibility was added to allow log rotation), but I need to switch to a different filename. I don't know how to change the filename used by the file appender before switching to another file handle.

How to change the appender's filename if it is possible?

Borodin
  • 126,100
  • 9
  • 70
  • 144
drvtiny
  • 705
  • 3
  • 13
  • 1
    You have not released that module to CPAN yet. Your repo also doesn't look like a typical Perl distribution. Do you need help with structuring it properly and releasing it? – simbabque Sep 07 '17 at 12:19
  • I want to release this module on CPAN but i think it's functionality is not enough. I've prepared Git repository for CPAN upload on my machine (after reading article about preparing CPAN modules on habrahabr.ru), so i'm ready to make Log4perl::KISS available on CPAN... very soon :) – drvtiny Sep 08 '17 at 12:43
  • You also might want to look into Dist::Zilla and similar. I used Module::Starter for my first module, but I don't like it any more. I've switched to Minilla since, but honestly the dists I took over that use dzil are just so much nicer to use. It's just a bit of work to set it up, but then it's awesome. – simbabque Sep 08 '17 at 12:59

1 Answers1

4

There is a file_switch method in Log::Log4perl::Appender::File that's briefly touched upon in the Description section.

If you want to switch over to a different logfile, use the file_switch($newfile) method which will first close the old file handle and then open a one to the new file specified.

There's no further documentation on it, but I believe you can use that.

simbabque
  • 53,749
  • 8
  • 73
  • 136
  • Thank you! I was very unobservant when reading this documentation. Yes, file_switch is exactly what i need! – drvtiny Sep 08 '17 at 12:37
  • But... how to get access to Log4perl's "internal" instance of Log::Log4perl::Appender::File object class? – drvtiny Sep 08 '17 at 12:51
  • @drvtiny I don't know. I've never set up an l4p myself. We do use it at `$work` for a few projects, but I don't really interact with it. I'd have to play around to do that. You can, probably. Use Data::Printer to understand what kind of accessors there are on the objects you have, and work your way down. – simbabque Sep 08 '17 at 12:58