How to replace Log::Dispatch::File with ::FileRotate?

Viewed 155

I've inherited a large app that's using Log::Dispatch::File. This sets up a $log object, which you call like $log->info('You are here'). There are thousands of calls to this object throughout this app.

We need to rotate the log files, so I started to play around with Log::Dispatch::FileRotate. Despite being described in its docs as "basically a Log::Dispatch::File wrapper with additions", FileRotate instead sets up a $file object, and you log things by calling $file->log( level => 'info', message => 'You are here' );. This is obviously incompatible with my existing app.

Is there some easy way to get from here to a typical $log object, so that I don't have to rewrite all of these thousands of calls?

1 Answers

Log::Dispatch::File does not provide an ->info method. You are actually creating and using a Log::Dispatch object. Yes, a Log::Dispatch::File object does get created, but you aren't using it directly. You are using a Log::Dispatch object, and you should continue to do so!

You might be using something like the following:

my $log = Log::Dispatch->new(
    outputs => [
        [ 'File', min_level => 'debug', filename => 'logfile' ],
    ],
);

If so, replace it with the following:

my $log = Log::Dispatch->new(
    outputs => [
        [ 'FileRotate', min_level => 'debug', filename => 'logfile' ],
    ],
);

Any additional parameters you want to pass to L::D::FR (size, max, etc) can be put in the array.

You might be using something like the following:

my $log = Log::Dispatch->new();
$log->add(
    Log::Dispatch::File->new(
        name      => 'file1',
        min_level => 'debug',
        filename  => 'logfile'
    )
);

If so, replace it with the following:

my $log = Log::Dispatch->new();
$log->add(
    Log::Dispatch::FileRotate->new(
        name      => 'file1',
        min_level => 'debug',
        filename  => 'logfile'
    )
);
Related