= logrotate
== DESCRIPTION:
This package is a library of methods that perform log rotation on files and
directories. The log rotate methods allow the caller to specify options (via
parameters) such as how many rotated files to keep, what type of
extension to place on the rotated file (date or a simple count), and
whether to zip the rotated files. Live log files (currently being
written to by a live process) can be rotated as well. The post_rotate
option is useful in that context, as it can be used to send a HUP
signal to notify the live process to reopen its log file.
This package was inspired by the need to have a library version of the
unix logrotate tool. The unix logrotate tool requires the user to
specify options in a config file, and is usually invoked through cron.
Directories can be rotated with this library. However, the gzip option
does not work with directories. In this case, please zip/tar the directory
in question before invoking this library.
== PROBLEMS:
None (known).
== SYNOPSIS:
Here are some sample invocations of the library.
Rotate a log file with date/time extensions.
======examples/rotate_date_time.rb:
#!/usr/bin/env/ruby
require 'fileutils'
require 'rubygems'
require 'logrotate'
options = {
:date_time_ext => true,
:date_time_format => '%F_%T',
:count => 2
}
1.upto(3) do |iteration|
FileUtils.touch("/tmp/erwin.dat")
result = LogRotate.rotate_file("/tmp/erwin.dat", options)
print "=================================================\n"
print "Iteration ##{iteration}\n"
print "=================================================\n"
print result, "\n"
Sleep for a short period so that next time rotate_file is called,
the current second will be different (and thus the rotated file
name will be different than the last).
sleep(1.5)
end
clean up all files created by this example
FileUtils.rm_f(Dir.glob('/tmp/erwin.dat.*'))
======Output:
Iteration #1
Rotated Files And Dates:
file -> /tmp/erwin.dat.2008-08-21_12:37:39, date_time -> 2008-08-21T12:37:39+00:00
file -> /tmp/erwin.dat.2008-08-21_12:37:37, date_time -> 2008-08-21T12:37:37+00:00
Rotated Files:
/tmp/erwin.dat.2008-08-21_12:37:39
/tmp/erwin.dat.2008-08-21_12:37:37
Deleted Files:
New Rotated File:
/tmp/erwin.dat.2008-08-21_12:37:39
=================================================
Iteration #2
Rotated Files And Dates:
file -> /tmp/erwin.dat.2008-08-21_12:37:41, date_time -> 2008-08-21T12:37:41+00:00
file -> /tmp/erwin.dat.2008-08-21_12:37:39, date_time -> 2008-08-21T12:37:39+00:00
Rotated Files:
/tmp/erwin.dat.2008-08-21_12:37:41
/tmp/erwin.dat.2008-08-21_12:37:39
Deleted Files:
/tmp/erwin.dat.2008-08-21_12:37:37
New Rotated File:
/tmp/erwin.dat.2008-08-21_12:37:41
=================================================
Iteration #3
Rotated Files And Dates:
file -> /tmp/erwin.dat.2008-08-21_12:37:42, date_time -> 2008-08-21T12:37:42+00:00
file -> /tmp/erwin.dat.2008-08-21_12:37:41, date_time -> 2008-08-21T12:37:41+00:00
Rotated Files:
/tmp/erwin.dat.2008-08-21_12:37:42
/tmp/erwin.dat.2008-08-21_12:37:41
Deleted Files:
/tmp/erwin.dat.2008-08-21_12:37:39
New Rotated File:
/tmp/erwin.dat.2008-08-21_12:37:42
Rotate a set of log files with date extensions, and move the rotated
files to a specified directory:
======examples/rotate_date.rb:
#!/usr/bin/env ruby
require 'fileutils'
require 'rubygems'
require 'logrotate'
output_directory = "/tmp/archives"
if (!File.directory?(output_directory)) then Dir.mkdir(output_directory) end
files = (1..64).map {|i| "/tmp/erwin_#{i}.dat" }
files.each do |file|
File.open(file, "w") do |fs|
fs.write(file)
end
end
options = {
:date_time_ext => true,
:directory => output_directory
}
LogRotate.rotate_files(files, options)
clean up all files and directories created by this example
FileUtils.rm_f(Dir.glob"/tmp/erwin*.dat")
FileUtils.rm_rf("/tmp/archives")
Rotate a log file with simple extensions: .1, .2, etc, keep 3 rotated
files around (.1 .. .3), and gzip the rotated files (.1.gz, .. .3.gz):
======examples/rotate_count.rb:
#!/usr/bin/env ruby
require 'fileutils'
require 'rubygems'
require 'logrotate'
options = {
:count => 2,
:gzip => true
}
1.upto(3) do |iteration|
FileUtils.touch("/tmp/erwin.dat")
result = LogRotate.rotate_file("/tmp/erwin.dat", options)
print "=================================================\n"
print "Iteration ##{iteration}\n"
print "=================================================\n"
print result, "\n"
end
clean up all files created by this example
FileUtils.rm_f(Dir.glob('/tmp/erwin.dat.*'))
======Output:
Iteration #1
Rotated Files And Counts:
file -> /tmp/erwin.dat.1.gz, index -> 1
Rotated Files:
/tmp/erwin.dat.1.gz
Deleted Files:
New Rotated File:
/tmp/erwin.dat.1.gz
=================================================
Iteration #2
Rotated Files And Counts:
file -> /tmp/erwin.dat.1.gz, index -> 1
file -> /tmp/erwin.dat.2.gz, index -> 2
Rotated Files:
/tmp/erwin.dat.1.gz
/tmp/erwin.dat.2.gz
Deleted Files:
New Rotated File:
/tmp/erwin.dat.1.gz
=================================================
Iteration #3
Rotated Files And Counts:
file -> /tmp/erwin.dat.1.gz, index -> 1
file -> /tmp/erwin.dat.2.gz, index -> 2
Rotated Files:
/tmp/erwin.dat.1.gz
/tmp/erwin.dat.2.gz
Deleted Files:
/tmp/erwin.dat.2.gz
New Rotated File:
/tmp/erwin.dat.1.gz
Rotate a log file that is currently being written to by a live
process. After the file is rotated, notify the live process to reopen
its log file.
======examples/rotate_live.rb:
#!/usr/bin/env ruby
require 'fileutils'
require 'rubygems'
require 'logrotate'
block = Proc.new() do
File.open("/tmp/hupper.pid") do |pid_stream|
pid = pid_stream.read().to_i()
Process.kill("HUP", pid)
end
end
options = {
:gzip => true,
:count => 3,
:post_rotate => block
}
LogRotate.rotate_file("/tmp/hupper.dat", options)
clean up all files created by this example
FileUtils.rm_rf(Dir.glob("/tmp/hupper*"))
== REQUIREMENTS:
- Hoe is required but only for running the tests.
- The unix command line tool tar is required if directories are to be rotated and zipped.
== INSTALL:
sudo gem install logrotate
== AUTHORS:
=== Designing Patterns
== SUPPORT:
Please post questions, concerns, or requests for enhancement to the forums on
the project page. Alternatively, direct contact information for
Designing Patterns can be found on the project page for this gem.
== ENHANCEMENTS:
Please feel free to contact us with any ideas; we will try our best to
enhance the software and respond to user requests. Of course, we are more
likely to work on a particular enhancement if we know that there are users
who want it. Designing Patterns provides contracting and consulting services,
so if there is an enhancement that must get done (and in a specified time
frame), please inquire about retaining our services!
== LICENSE:
The license text can be found in the +LICENSE+ file at the root of the
distribution.
This package is licensed with an MIT license:
Copyright (c) 2008-2009 Designing Patterns
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
== SHARE AND ENJOY!