Steven Haryanto > File-Write-Rotate > File::Write::Rotate

Download:
File-Write-Rotate-0.17.tar.gz

Dependencies

Annotate this POD

Website

View/Report Bugs
Module Version: 0.17   Source  

NAME ^

File::Write::Rotate - Write to files that archive/rotate themselves

VERSION ^

This document describes version 0.17 of File::Write::Rotate (from Perl distribution File-Write-Rotate), released on 2014-07-10.

SYNOPSIS ^

 use File::Write::Rotate;

 my $fwr = File::Write::Rotate->new(
     dir          => '/var/log',    # required
     prefix       => 'myapp',       # required
     #suffix      => '.log',        # default is ''
     size         => 25*1024*1024,  # default is 10MB, unless period is set
     histories    => 12,            # default is 10
     #buffer_size => 100,           # default is none
 );

 # write, will write to /var/log/myapp.log, automatically rotate old log files
 # to myapp.log.1 when myapp.log reaches 25MB. will keep old log files up to
 # myapp.log.12.
 $fwr->write("This is a line\n");
 $fwr->write("This is", " another line\n");

 # compress old log files
 $fwr->compress;

DESCRIPTION ^

This module can be used to write to file, usually for logging, that can rotate itself. File will be opened in append mode. Locking will be done to avoid conflict when there are multiple writers. Rotation can be done by size (after a certain size is reached), by time (daily/monthly/yearly), or both.

I first wrote this module for logging script STDERR output to files (see Tie::Handle::FileWriteRotate).

ATTRIBUTES ^

buffer_size => INT

Get or set buffer size. If set to a value larger than 0, then when a write() failed, instead of dying, the message will be stored in an internal buffer first (a regular Perl array). When the number of items in the buffer exceeds this size, then write() will die upon failure. Otherwise, every write() will try to flush the buffer.

Can be used for example when a program runs as superuser/root then temporarily drops privilege to a normal user. During this period, logging can fail because the program cannot lock the lock file or write to the logging directory. Before dropping privilege, the program can set buffer_size to some larger-than-zero value to hold the messages emitted during dropping privilege. The next write() as the superuser/root will succeed and flush the buffer to disk (provided there is no other error condition, of course).

METHODS ^

$obj = File::Write::Rotate->new(%args)

Create new object. Known arguments:

$fwr->write(@args)

Write to file. Will automatically rotate file if period changes or file size exceeds specified limit. When rotating, will only keep a specified number of histories and delete the older ones. Uses locking, so multiple writers do not clobber one another. Lock file is named <prefix>.lck. Will wait for up to 1 minute to acquire lock, will die if failed to acquire lock.

Does not append newline so you'll have to do it yourself.

$fwr->compress

Compress old rotated files. Currently uses pigz or gzip program to do the compression. Extension given to compressed file is .gz.

Normally, should be done using a separate process so as to avoid blocking the writers.

Will not lock writers, but will create <prefix>-compress.pid PID file to prevent multiple compression processes running and to signal the writers to postpone rotation.

After compression is finished, will remove the PID file, so rotation can be done again on the next write() if necessary.

SEE ALSO ^

Log::Dispatch::FileRotate, which inspires this module. Differences between File::Write::Rotate (FWR) and Log::Dispatch::FileRotate (LDFR) are as follows:

There is no significant overhead difference between FWR and LDFR (FWR is slightly faster than LDFR on my testing).

Tie::Handle::FileWriteRotate and Log::Dispatch::FileWriteRotate, which use this module.

FAQ ^

Why use autorotating log?

Mainly convenience and low maintenance. You no longer need a separate rotator like the Unix logrotate utility (which when accidentally disabled or misconfigured will cause your logs to stop being rotated and grow indefinitely).

What is the downside of using FWR (and LDFR)?

Mainly performance overhead, as every write() involves locking to make it safe to use with multiple processes. Tested on my Core i5 3.1 GHz desktop, writing log lines in the size of ~ 200 bytes, raw writing to disk (SSD) has the speed of around 3.4mil/s, while using FWR it comes down to around 19.5k/s.

However, this is not something you'll notice or need to worry about unless you're logging near that speed.

HOMEPAGE ^

Please visit the project's homepage at https://metacpan.org/release/File-Write-Rotate.

SOURCE ^

Source repository is at https://github.com/sharyanto/perl-File-Write-Rotate.

BUGS ^

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=File-Write-Rotate

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

AUTHOR ^

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE ^

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: