logrotate 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +3 -0
- data/LICENSE +1 -1
- data/Manifest.txt +2 -2
- data/README.txt +79 -73
- data/Rakefile +5 -2
- data/lib/logrotate/impl.rb +14 -3
- data/lib/logrotate/logrotate.rb +8 -3
- data/lib/logrotate/rotateinfo.rb +8 -8
- data/test/test_logrotate.rb +198 -47
- metadata +29 -11
data/History.txt
CHANGED
data/LICENSE
CHANGED
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -2,8 +2,8 @@ Object
|
|
2
2
|
|
3
3
|
== DESCRIPTION:
|
4
4
|
|
5
|
-
This package is a library of methods that perform log rotation
|
6
|
-
log rotate methods allow the caller to specify options (via
|
5
|
+
This package is a library of methods that perform log rotation on files and
|
6
|
+
directories. The log rotate methods allow the caller to specify options (via
|
7
7
|
parameters) such as how many rotated files to keep, what type of
|
8
8
|
extension to place on the rotated file (date or a simple count), and
|
9
9
|
whether to zip the rotated files. Live log files (currently being
|
@@ -15,6 +15,10 @@ This package was inspired by the need to have a library version of the
|
|
15
15
|
unix logrotate tool. The unix logrotate tool requires the user to
|
16
16
|
specify options in a config file, and is usually invoked through cron.
|
17
17
|
|
18
|
+
Directories can be rotated with this library. However, the gzip option
|
19
|
+
does not work with directories. In this case, please zip/tar the directory
|
20
|
+
in question before invoking this library.
|
21
|
+
|
18
22
|
== PROBLEMS:
|
19
23
|
|
20
24
|
None (known).
|
@@ -22,75 +26,6 @@ None (known).
|
|
22
26
|
== SYNOPSIS:
|
23
27
|
Here are some sample invocations of the library.
|
24
28
|
|
25
|
-
Rotate a log file with simple extensions: .1, .2, etc, keep 3 rotated
|
26
|
-
files around (.1 .. .3), and gzip the rotated files (.1.gz, .. .3.gz):
|
27
|
-
======<tt>examples/rotate_count.rb</tt>:
|
28
|
-
#!/usr/bin/env ruby
|
29
|
-
|
30
|
-
require 'fileutils'
|
31
|
-
require 'rubygems'
|
32
|
-
require 'logrotate'
|
33
|
-
|
34
|
-
options = {
|
35
|
-
:count => 2,
|
36
|
-
:gzip => true
|
37
|
-
}
|
38
|
-
|
39
|
-
1.upto(3) do |iteration|
|
40
|
-
|
41
|
-
FileUtils.touch("/tmp/erwin.dat")
|
42
|
-
result = LogRotate.rotate_file("/tmp/erwin.dat", options)
|
43
|
-
|
44
|
-
print "=================================================\n"
|
45
|
-
print "Iteration \##{iteration}\n"
|
46
|
-
print "=================================================\n"
|
47
|
-
print result, "\n"
|
48
|
-
end
|
49
|
-
|
50
|
-
# clean up all files created by this example
|
51
|
-
FileUtils.rm_f(Dir.glob('/tmp/erwin.dat.*'))
|
52
|
-
|
53
|
-
======<tt>Output</tt>:
|
54
|
-
=================================================
|
55
|
-
Iteration #1
|
56
|
-
=================================================
|
57
|
-
Rotated Files And Counts:
|
58
|
-
file -> /tmp/erwin.dat.1.gz, index -> 1
|
59
|
-
Rotated Files:
|
60
|
-
/tmp/erwin.dat.1.gz
|
61
|
-
Deleted Files:
|
62
|
-
New Rotated File:
|
63
|
-
/tmp/erwin.dat.1.gz
|
64
|
-
|
65
|
-
=================================================
|
66
|
-
Iteration #2
|
67
|
-
=================================================
|
68
|
-
Rotated Files And Counts:
|
69
|
-
file -> /tmp/erwin.dat.1.gz, index -> 1
|
70
|
-
file -> /tmp/erwin.dat.2.gz, index -> 2
|
71
|
-
Rotated Files:
|
72
|
-
/tmp/erwin.dat.1.gz
|
73
|
-
/tmp/erwin.dat.2.gz
|
74
|
-
Deleted Files:
|
75
|
-
New Rotated File:
|
76
|
-
/tmp/erwin.dat.1.gz
|
77
|
-
|
78
|
-
=================================================
|
79
|
-
Iteration #3
|
80
|
-
=================================================
|
81
|
-
Rotated Files And Counts:
|
82
|
-
file -> /tmp/erwin.dat.1.gz, index -> 1
|
83
|
-
file -> /tmp/erwin.dat.2.gz, index -> 2
|
84
|
-
Rotated Files:
|
85
|
-
/tmp/erwin.dat.1.gz
|
86
|
-
/tmp/erwin.dat.2.gz
|
87
|
-
Deleted Files:
|
88
|
-
/tmp/erwin.dat.2.gz
|
89
|
-
New Rotated File:
|
90
|
-
/tmp/erwin.dat.1.gz
|
91
|
-
|
92
|
-
|
93
|
-
|
94
29
|
Rotate a log file with date/time extensions.
|
95
30
|
======<tt>examples/rotate_date_time.rb</tt>:
|
96
31
|
#!/usr/bin/env/ruby
|
@@ -199,6 +134,75 @@ files to a specified directory:
|
|
199
134
|
FileUtils.rm_rf("/tmp/archives")
|
200
135
|
|
201
136
|
|
137
|
+
Rotate a log file with simple extensions: .1, .2, etc, keep 3 rotated
|
138
|
+
files around (.1 .. .3), and gzip the rotated files (.1.gz, .. .3.gz):
|
139
|
+
======<tt>examples/rotate_count.rb</tt>:
|
140
|
+
#!/usr/bin/env ruby
|
141
|
+
|
142
|
+
require 'fileutils'
|
143
|
+
require 'rubygems'
|
144
|
+
require 'logrotate'
|
145
|
+
|
146
|
+
options = {
|
147
|
+
:count => 2,
|
148
|
+
:gzip => true
|
149
|
+
}
|
150
|
+
|
151
|
+
1.upto(3) do |iteration|
|
152
|
+
|
153
|
+
FileUtils.touch("/tmp/erwin.dat")
|
154
|
+
result = LogRotate.rotate_file("/tmp/erwin.dat", options)
|
155
|
+
|
156
|
+
print "=================================================\n"
|
157
|
+
print "Iteration \##{iteration}\n"
|
158
|
+
print "=================================================\n"
|
159
|
+
print result, "\n"
|
160
|
+
end
|
161
|
+
|
162
|
+
# clean up all files created by this example
|
163
|
+
FileUtils.rm_f(Dir.glob('/tmp/erwin.dat.*'))
|
164
|
+
|
165
|
+
======<tt>Output</tt>:
|
166
|
+
=================================================
|
167
|
+
Iteration #1
|
168
|
+
=================================================
|
169
|
+
Rotated Files And Counts:
|
170
|
+
file -> /tmp/erwin.dat.1.gz, index -> 1
|
171
|
+
Rotated Files:
|
172
|
+
/tmp/erwin.dat.1.gz
|
173
|
+
Deleted Files:
|
174
|
+
New Rotated File:
|
175
|
+
/tmp/erwin.dat.1.gz
|
176
|
+
|
177
|
+
=================================================
|
178
|
+
Iteration #2
|
179
|
+
=================================================
|
180
|
+
Rotated Files And Counts:
|
181
|
+
file -> /tmp/erwin.dat.1.gz, index -> 1
|
182
|
+
file -> /tmp/erwin.dat.2.gz, index -> 2
|
183
|
+
Rotated Files:
|
184
|
+
/tmp/erwin.dat.1.gz
|
185
|
+
/tmp/erwin.dat.2.gz
|
186
|
+
Deleted Files:
|
187
|
+
New Rotated File:
|
188
|
+
/tmp/erwin.dat.1.gz
|
189
|
+
|
190
|
+
=================================================
|
191
|
+
Iteration #3
|
192
|
+
=================================================
|
193
|
+
Rotated Files And Counts:
|
194
|
+
file -> /tmp/erwin.dat.1.gz, index -> 1
|
195
|
+
file -> /tmp/erwin.dat.2.gz, index -> 2
|
196
|
+
Rotated Files:
|
197
|
+
/tmp/erwin.dat.1.gz
|
198
|
+
/tmp/erwin.dat.2.gz
|
199
|
+
Deleted Files:
|
200
|
+
/tmp/erwin.dat.2.gz
|
201
|
+
New Rotated File:
|
202
|
+
/tmp/erwin.dat.1.gz
|
203
|
+
|
204
|
+
|
205
|
+
|
202
206
|
Rotate a log file that is currently being written to by a live
|
203
207
|
process. After the file is rotated, notify the live process to reopen
|
204
208
|
its log file.
|
@@ -230,7 +234,8 @@ its log file.
|
|
230
234
|
|
231
235
|
== REQUIREMENTS:
|
232
236
|
|
233
|
-
Hoe is required but only for running the tests.
|
237
|
+
* Hoe is required but only for running the tests.
|
238
|
+
* The unix command line tool tar is required if directories are to be rotated and zipped.
|
234
239
|
|
235
240
|
== INSTALL:
|
236
241
|
|
@@ -240,6 +245,7 @@ Hoe is required but only for running the tests.
|
|
240
245
|
=== Designing Patterns
|
241
246
|
* Homepage: http://www.designingpatterns.com
|
242
247
|
* Blogs: http://blogs.designingpatterns.com
|
248
|
+
* Twitter: http://www.twitter.com/TonyDesignP
|
243
249
|
|
244
250
|
== SUPPORT:
|
245
251
|
Please post questions, concerns, or requests for enhancement to the forums on
|
@@ -260,7 +266,7 @@ distribution.
|
|
260
266
|
|
261
267
|
This package is licensed with an MIT license:
|
262
268
|
|
263
|
-
Copyright (c) 2008 Designing Patterns
|
269
|
+
Copyright (c) 2008-2009 Designing Patterns
|
264
270
|
|
265
271
|
Permission is hereby granted, free of charge, to any person obtaining
|
266
272
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -2,8 +2,11 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'hoe'
|
4
4
|
|
5
|
-
|
6
|
-
|
5
|
+
PROJECT_NAME = 'logrotate'
|
6
|
+
|
7
|
+
Hoe.spec(PROJECT_NAME) do |p|
|
8
|
+
p.url = "http://#{PROJECT_NAME}.rubyforge.org"
|
9
|
+
p.version = "1.2.0"
|
7
10
|
p.developer('DesigningPatterns', 'technical.inquiries@designingpatterns.com')
|
8
11
|
end
|
9
12
|
|
data/lib/logrotate/impl.rb
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
require 'zlib'
|
4
4
|
require 'date'
|
5
|
+
require 'fileutils'
|
6
|
+
|
5
7
|
require 'logrotate/rotateinfo'
|
6
8
|
|
7
9
|
module LogRotate
|
@@ -249,6 +251,15 @@ module LogRotate
|
|
249
251
|
# error checking
|
250
252
|
if (!File.exist?(file)) then raise "File does not exist: #{file}." end
|
251
253
|
if (!File.readable?(file)) then raise "File is not readable: #{file}." end
|
254
|
+
|
255
|
+
is_directory = File.directory?(file)
|
256
|
+
if (is_directory && options[:gzip])
|
257
|
+
raise ArgumentError, "The gzip option is not supported for directories."
|
258
|
+
end
|
259
|
+
|
260
|
+
if (options[:directory])
|
261
|
+
FileUtils.mkdir_p(options[:directory])
|
262
|
+
end
|
252
263
|
|
253
264
|
if (options[:pre_rotate])
|
254
265
|
options[:pre_rotate].call()
|
@@ -308,7 +319,7 @@ module LogRotate
|
|
308
319
|
|
309
320
|
deleted_files = deleted_files_and_dates.map { |entry| entry[:file] }
|
310
321
|
if (deleted_files_and_dates.length() > 0)
|
311
|
-
|
322
|
+
FileUtils.rm_r(deleted_files)
|
312
323
|
end
|
313
324
|
|
314
325
|
# Rename the original file.
|
@@ -347,7 +358,7 @@ module LogRotate
|
|
347
358
|
count,
|
348
359
|
new_rotated_file)
|
349
360
|
deleted_files = deleted_files_and_counts.map {|entry| entry[:file] }
|
350
|
-
|
361
|
+
FileUtils.rm_r(deleted_files)
|
351
362
|
|
352
363
|
rotated_files_and_counts = rotated_files_and_counts - deleted_files_and_counts
|
353
364
|
|
@@ -396,7 +407,7 @@ module LogRotate
|
|
396
407
|
rescue => error
|
397
408
|
raise "Unable to zip file: #{file}. Error: #{error}\n"
|
398
409
|
end
|
399
|
-
|
410
|
+
FileUtils.rm_r(file)
|
400
411
|
|
401
412
|
return gzip_file
|
402
413
|
end
|
data/lib/logrotate/logrotate.rb
CHANGED
@@ -10,7 +10,8 @@ require 'logrotate/rotateinfo'
|
|
10
10
|
module LogRotate
|
11
11
|
|
12
12
|
#
|
13
|
-
# ====Description:
|
13
|
+
# ====Description:
|
14
|
+
# This method rotates the given files and/or directories.
|
14
15
|
#
|
15
16
|
# ====Parameters:
|
16
17
|
# [file(s)]
|
@@ -33,7 +34,7 @@ module LogRotate
|
|
33
34
|
|
34
35
|
#
|
35
36
|
# ====Description:
|
36
|
-
# This method rotates the given file
|
37
|
+
# This method rotates the given file or directory.
|
37
38
|
#
|
38
39
|
# There are 2 types of extensions that can be used: the default
|
39
40
|
# extension which is an integer value (".1", ".2", ..), and a
|
@@ -45,6 +46,9 @@ module LogRotate
|
|
45
46
|
# succeeds, the method will return normally, and an instance will be
|
46
47
|
# returned with information about the rotated files.
|
47
48
|
#
|
49
|
+
# Note: If a directory is passed in to be rotated, the gzip option
|
50
|
+
# does not apply / can not be used. In this case, please tar the
|
51
|
+
# directory and then call rotate_file on the tar'ed file.
|
48
52
|
# ====Parameters:
|
49
53
|
# [file]
|
50
54
|
# The file to be rotated.
|
@@ -68,7 +72,8 @@ module LogRotate
|
|
68
72
|
# +post_rotate+:: A +Proc+ to be executed after the rotation is finished,
|
69
73
|
# and before the newly rotated file is zipped.
|
70
74
|
# +gzip+:: Whether the newly rotated file should be gzipped. Possible
|
71
|
-
# values are: +true+, +false+.
|
75
|
+
# values are: +true+, +false+. Note: This option can not be
|
76
|
+
# used on directories.
|
72
77
|
#
|
73
78
|
# To see the default values of these options, see the DEFAULT_
|
74
79
|
# constants in LogRotate::Impl.
|
data/lib/logrotate/rotateinfo.rb
CHANGED
@@ -61,22 +61,22 @@ module LogRotate
|
|
61
61
|
|
62
62
|
def to_s()
|
63
63
|
os = StringIO.new
|
64
|
-
os.print "Rotated Files And Dates:\n"
|
64
|
+
os.print "Rotated Files/Directories And Dates:\n"
|
65
65
|
@rotated_files_and_dates.each do |element|
|
66
66
|
os.print " file -> ", element[:file], ", date_time -> ", element[:date_time], "\n"
|
67
67
|
end
|
68
68
|
|
69
|
-
os.print "Rotated Files: \n"
|
69
|
+
os.print "Rotated Files/Directories: \n"
|
70
70
|
if (@rotated_files.length() > 0)
|
71
71
|
os.print " ", @rotated_files.join("\n "), "\n"
|
72
72
|
end
|
73
73
|
|
74
|
-
os.print "Deleted Files:\n"
|
74
|
+
os.print "Deleted Files/Directories:\n"
|
75
75
|
if (@deleted_files.length() > 0)
|
76
76
|
os.print " ", @deleted_files.join("\n "), "\n"
|
77
77
|
end
|
78
78
|
|
79
|
-
os.print "
|
79
|
+
os.print "Newly Rotated File/Directory: \n"
|
80
80
|
os.print " ", @new_rotated_file, "\n"
|
81
81
|
|
82
82
|
return os.string()
|
@@ -140,22 +140,22 @@ module LogRotate
|
|
140
140
|
|
141
141
|
def to_s()
|
142
142
|
os = StringIO.new
|
143
|
-
os.print "Rotated Files And Counts:\n"
|
143
|
+
os.print "Rotated Files/Directories And Counts:\n"
|
144
144
|
@rotated_files_and_counts.each do |element|
|
145
145
|
os.print " file -> ", element[:file], ", index -> ", element[:index], "\n"
|
146
146
|
end
|
147
147
|
|
148
|
-
os.print "Rotated Files: \n"
|
148
|
+
os.print "Rotated Files/Directories: \n"
|
149
149
|
if (@rotated_files.length() > 0)
|
150
150
|
os.print " ", @rotated_files.join("\n "), "\n"
|
151
151
|
end
|
152
152
|
|
153
|
-
os.print "Deleted Files:\n"
|
153
|
+
os.print "Deleted Files/Directories:\n"
|
154
154
|
if (@deleted_files.length() > 0)
|
155
155
|
os.print " ", @deleted_files.join("\n "), "\n"
|
156
156
|
end
|
157
157
|
|
158
|
-
os.print "
|
158
|
+
os.print "Newly Rotated File/Directory: \n"
|
159
159
|
os.print " ", @new_rotated_file, "\n"
|
160
160
|
|
161
161
|
return os.string()
|
data/test/test_logrotate.rb
CHANGED
@@ -5,8 +5,13 @@ require 'logrotate/impl'
|
|
5
5
|
require 'logrotate/rotateinfo'
|
6
6
|
|
7
7
|
require 'fileutils'
|
8
|
-
|
9
8
|
require 'test/unit'
|
9
|
+
require 'English'
|
10
|
+
|
11
|
+
require 'rubygems'
|
12
|
+
require 'assertions'
|
13
|
+
|
14
|
+
print $LOAD_PATH.join("\n")
|
10
15
|
|
11
16
|
class LogRotateTest < Test::Unit::TestCase
|
12
17
|
TEMP_DIR = "./temp"
|
@@ -19,59 +24,207 @@ class LogRotateTest < Test::Unit::TestCase
|
|
19
24
|
FileUtils::rm_r(TEMP_DIR, :force => true)
|
20
25
|
end
|
21
26
|
|
22
|
-
def
|
23
|
-
|
24
|
-
|
27
|
+
def test_rotate_directory_and_gzip_error()
|
28
|
+
dir = "#{TEMP_DIR}/sample"
|
29
|
+
FileUtils.mkdir_p(dir)
|
30
|
+
|
31
|
+
options = {:gzip => true }
|
32
|
+
assert_raise_message("The gzip option is not supported for directories.", ArgumentError) do
|
33
|
+
result = LogRotate.rotate_file(dir, options)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_rotate_file_integer_extension()
|
38
|
+
output_dirs = [ nil, "#{TEMP_DIR}/archives" ]
|
25
39
|
|
26
|
-
|
27
|
-
|
28
|
-
|
40
|
+
output_dirs.each do |output_dir|
|
41
|
+
rotate_file_integer_extension(3, 10, gzip = false, output_dir)
|
42
|
+
rotate_file_integer_extension(3, 10, gzip = true, output_dir)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_rotate_directory_integer_extension()
|
47
|
+
output_dirs = [ nil, "#{TEMP_DIR}/archives" ]
|
48
|
+
|
49
|
+
output_dirs.each do |output_dir|
|
50
|
+
rotate_directory_integer_extension(3, 10, output_dir)
|
51
|
+
end
|
29
52
|
end
|
30
53
|
|
31
|
-
def
|
54
|
+
def test_rotate_file_date_extension()
|
32
55
|
date_time_format = "%F"
|
56
|
+
output_dirs = [ nil, "#{TEMP_DIR}/archives" ]
|
57
|
+
|
58
|
+
output_dirs.each do |output_dir|
|
59
|
+
rotate_file_date_extension(3, 10, gzip = false, output_dir)
|
60
|
+
rotate_file_date_extension(3, 10, gzip = true, output_dir)
|
61
|
+
rotate_file_date_extension(3, 10, gzip = true, output_dir, date_time_format)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_rotate_directory_date_extension()
|
66
|
+
output_dirs = [ nil, "#{TEMP_DIR}/archives" ]
|
33
67
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
68
|
+
output_dirs.each do |output_dir|
|
69
|
+
rotate_directory_date_extension(1, 5, output_dir)
|
70
|
+
rotate_directory_date_extension(3, 10, output_dir)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def resolve_output_dir(source_dir, output_dir)
|
75
|
+
if (is_output_dir = output_dir != nil)
|
76
|
+
else
|
77
|
+
output_dir = source_dir
|
78
|
+
end
|
38
79
|
|
39
|
-
|
40
|
-
rotate_date_extension(3, 10, gzip = false, output_dir)
|
41
|
-
rotate_date_extension(3, 10, gzip = true, output_dir)
|
42
|
-
rotate_date_extension(3, 10, gzip = true, output_dir, date_time_format)
|
80
|
+
return is_output_dir, output_dir
|
43
81
|
end
|
44
82
|
|
45
|
-
def
|
83
|
+
def rotate_directory_integer_extension(count, rotations, output_dir = nil)
|
84
|
+
|
85
|
+
source_dir = "#{TEMP_DIR}"
|
86
|
+
Dir.mkdir(source_dir)
|
87
|
+
|
88
|
+
# The directory we will be rotating
|
89
|
+
base_name = "folder"
|
90
|
+
directory = File.join(source_dir, base_name)
|
91
|
+
|
92
|
+
# The file we will be seeding the directory with
|
93
|
+
file_base = "file.dat"
|
94
|
+
file = "#{directory}/#{file_base}"
|
95
|
+
|
96
|
+
is_output_dir, output_dir = resolve_output_dir(source_dir, output_dir)
|
97
|
+
|
98
|
+
#rotate the file rotations times
|
99
|
+
1.upto(rotations) do |iteration|
|
100
|
+
data = rotations - iteration + 1
|
101
|
+
|
102
|
+
Dir.mkdir(directory)
|
103
|
+
|
104
|
+
File.open(file, "w") do |file_stream|
|
105
|
+
file_stream.write("#{data}")
|
106
|
+
end
|
107
|
+
|
108
|
+
options = {:count => count}
|
109
|
+
if (is_output_dir) then options[:directory] = output_dir end
|
110
|
+
|
111
|
+
result = LogRotate.rotate_file(directory, options)
|
112
|
+
|
113
|
+
expected_result = get_expected_results_integer_extension(iteration, base_name, count, false, output_dir, true)
|
114
|
+
assert_equal(expected_result, result)
|
115
|
+
end
|
116
|
+
|
117
|
+
#verify there are count rotated files.
|
118
|
+
entries = Dir.glob("#{output_dir}/*").map {|entry| File.split(entry)[1]}
|
119
|
+
assert_equal(entries.length, count)
|
120
|
+
|
121
|
+
# verify the names of the remaining directories
|
122
|
+
expected = (1..count).map { |i| "#{base_name}.#{i}" }
|
123
|
+
assert_equal(entries.sort, expected)
|
124
|
+
|
125
|
+
#verify the contents of the remaining directories.
|
126
|
+
(1..count).each do |i|
|
127
|
+
rotated_directory = "#{output_dir}/#{base_name}.#{i}"
|
128
|
+
assert_equal(IO.read("#{rotated_directory}/#{file_base}"), i.to_s())
|
129
|
+
end
|
130
|
+
|
131
|
+
teardown()
|
132
|
+
end
|
133
|
+
|
134
|
+
|
135
|
+
def rotate_directory_date_extension(count, rotations, output_dir = nil,
|
136
|
+
date_time_format = LogRotate::Impl::DEFAULT_DATE_TIME_FORMAT)
|
137
|
+
source_dir = "#{TEMP_DIR}"
|
138
|
+
Dir.mkdir(source_dir)
|
139
|
+
|
140
|
+
# The directory we will be rotating
|
141
|
+
base_name = "folder"
|
142
|
+
directory = File.join(source_dir, base_name)
|
143
|
+
|
144
|
+
# The file we will be seeding the directory with
|
145
|
+
file_base = "file.dat"
|
146
|
+
file = "#{directory}/#{file_base}"
|
147
|
+
|
148
|
+
is_output_dir, output_dir = resolve_output_dir(source_dir, output_dir)
|
149
|
+
|
150
|
+
#rotate the directory
|
151
|
+
now = DateTime.now
|
152
|
+
date_times = (0..(rotations - 1)).map { |i| now + i }
|
153
|
+
|
154
|
+
1.upto(rotations) do |iteration|
|
155
|
+
index = iteration - 1
|
156
|
+
date_time = date_times[index]
|
157
|
+
|
158
|
+
Dir.mkdir(directory)
|
159
|
+
|
160
|
+
File.open(file, "w") do |file_stream|
|
161
|
+
file_stream.write(date_time.strftime(date_time_format))
|
162
|
+
end
|
163
|
+
|
164
|
+
options = { :date_time_ext => true, :date_time => date_time, :count => count }
|
165
|
+
if (is_output_dir) then options[:directory] = output_dir end
|
166
|
+
if date_time_format then options[:date_time_format] = date_time_format end
|
167
|
+
|
168
|
+
result = LogRotate.rotate_file(directory, options)
|
169
|
+
|
170
|
+
expected_result = get_expected_result_date_extension(iteration, base_name, date_times, count,
|
171
|
+
date_time_format, false, output_dir, true)
|
172
|
+
assert_equal(expected_result, result)
|
173
|
+
end
|
174
|
+
|
175
|
+
#verify there are count rotated files.
|
176
|
+
entries = Dir.glob("#{output_dir}/*").map {|entry| File.split(entry)[1]}
|
177
|
+
assert_equal(count, entries.length)
|
178
|
+
|
179
|
+
#verify the correctness of the file names
|
180
|
+
date_times.slice!(0, date_times.length - count)
|
181
|
+
expected = date_times.map do |date_time|
|
182
|
+
"#{base_name}.#{date_time.strftime(date_time_format)}"
|
183
|
+
end
|
184
|
+
|
185
|
+
assert_equal(expected, entries.sort)
|
186
|
+
|
187
|
+
# verify the contents of the zipped/unzipped directories
|
188
|
+
date_times.each do |date_time|
|
189
|
+
rotated_directory = "#{output_dir}/#{base_name}.#{date_time.strftime(date_time_format)}"
|
190
|
+
assert_equal(IO.read("#{rotated_directory}/#{file_base}"), "#{date_time.strftime(date_time_format)}")
|
191
|
+
end
|
192
|
+
|
193
|
+
teardown()
|
194
|
+
end
|
195
|
+
|
196
|
+
def assert_exec(command)
|
197
|
+
system(command)
|
198
|
+
assert_equal($CHILD_STATUS.exitstatus, 0, "Command (#{command}) Failed! Exit Status: #{$CHILD_STATUS.inspect}")
|
199
|
+
end
|
200
|
+
|
201
|
+
def get_expected_results_integer_extension(iteration, base_name, count, gzip, output_dir, is_directory)
|
46
202
|
expected_result = LogRotate::RotateInfoIntegerExtension.new(nil, nil, nil, nil)
|
47
203
|
expected_result.rotated_files_and_counts = []
|
204
|
+
|
205
|
+
gzip_extension = gzip ? (is_directory ? ".tar.gz" : ".gz") : ""
|
48
206
|
|
49
207
|
1.upto(iteration > count ? count : iteration) do |i|
|
50
|
-
rotated_file = "#{output_dir}/#{base_name}.#{i}" +
|
208
|
+
rotated_file = "#{output_dir}/#{base_name}.#{i}" + gzip_extension
|
51
209
|
expected_result.rotated_files_and_counts.push({ :index => i, :file => rotated_file })
|
52
210
|
end
|
53
211
|
|
54
212
|
expected_result.rotated_files = expected_result.rotated_files_and_counts.map { |entry| entry[:file] }
|
55
|
-
expected_result.deleted_files = iteration > count ? [ "#{output_dir}/#{base_name}.#{count}" +
|
56
|
-
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.1" +
|
213
|
+
expected_result.deleted_files = iteration > count ? [ "#{output_dir}/#{base_name}.#{count}" + gzip_extension ] : []
|
214
|
+
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.1" + gzip_extension
|
57
215
|
|
58
216
|
return expected_result
|
59
217
|
end
|
60
218
|
|
61
|
-
def
|
219
|
+
def rotate_file_integer_extension(count, rotations, gzip, output_dir = nil)
|
62
220
|
source_dir = "#{TEMP_DIR}"
|
221
|
+
Dir.mkdir(source_dir)
|
222
|
+
|
223
|
+
# The file we will be rotating
|
63
224
|
base_name = "file.dat"
|
64
225
|
file = File.join(source_dir, base_name)
|
65
|
-
Dir.mkdir(source_dir)
|
66
|
-
|
67
|
-
is_output_dir = false
|
68
|
-
if (output_dir) then
|
69
|
-
Dir.mkdir(output_dir)
|
70
|
-
is_output_dir = true
|
71
|
-
else
|
72
|
-
output_dir = source_dir
|
73
|
-
end
|
74
226
|
|
227
|
+
is_output_dir, output_dir = resolve_output_dir(source_dir, output_dir)
|
75
228
|
manipulate_garbage_files(output_dir, base_name, :insert)
|
76
229
|
|
77
230
|
#rotate the file rotations times
|
@@ -87,7 +240,7 @@ class LogRotateTest < Test::Unit::TestCase
|
|
87
240
|
|
88
241
|
result = LogRotate.rotate_file(file, options)
|
89
242
|
|
90
|
-
expected_result = get_expected_results_integer_extension(iteration, base_name, count, gzip, output_dir)
|
243
|
+
expected_result = get_expected_results_integer_extension(iteration, base_name, count, gzip, output_dir, false)
|
91
244
|
assert_equal(expected_result, result)
|
92
245
|
end
|
93
246
|
|
@@ -116,6 +269,8 @@ class LogRotateTest < Test::Unit::TestCase
|
|
116
269
|
end
|
117
270
|
|
118
271
|
def manipulate_garbage_files(dir, base_name, operation)
|
272
|
+
FileUtils.mkdir_p(dir)
|
273
|
+
|
119
274
|
bogus_extensions = [ ".back", ".back123", ".2008-10-04foo", ".123x" ]
|
120
275
|
|
121
276
|
bogus_extensions.each do |bogus_extension|
|
@@ -131,8 +286,10 @@ class LogRotateTest < Test::Unit::TestCase
|
|
131
286
|
end
|
132
287
|
end
|
133
288
|
|
134
|
-
def get_expected_result_date_extension(iteration, base_name, date_times, count, date_time_format, gzip, output_dir)
|
289
|
+
def get_expected_result_date_extension(iteration, base_name, date_times, count, date_time_format, gzip, output_dir, is_directory)
|
135
290
|
index = iteration - 1
|
291
|
+
gzip_extension = gzip ? (is_directory ? ".tar.gz" : ".gz") : ""
|
292
|
+
|
136
293
|
|
137
294
|
expected_result = LogRotate::RotateInfoDateExtension.new(nil, nil, nil, nil)
|
138
295
|
expected_result.rotated_files_and_dates = []
|
@@ -141,7 +298,7 @@ class LogRotateTest < Test::Unit::TestCase
|
|
141
298
|
index.downto(oldest_existing_date_index) do |i|
|
142
299
|
str_date_time = date_times[i].strftime(date_time_format)
|
143
300
|
reconstructed_date_time = DateTime.strptime(str_date_time, date_time_format)
|
144
|
-
rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" +
|
301
|
+
rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" + gzip_extension
|
145
302
|
|
146
303
|
expected_result.rotated_files_and_dates.push({ :date_time => reconstructed_date_time,
|
147
304
|
:file => rotated_file })
|
@@ -151,34 +308,28 @@ class LogRotateTest < Test::Unit::TestCase
|
|
151
308
|
|
152
309
|
if (iteration > count)
|
153
310
|
str_date_time = date_times[index - count].strftime(date_time_format)
|
154
|
-
expected_result.deleted_files = [ "#{output_dir}/#{base_name}.#{str_date_time}" +
|
311
|
+
expected_result.deleted_files = [ "#{output_dir}/#{base_name}.#{str_date_time}" + gzip_extension ]
|
155
312
|
else
|
156
313
|
expected_result.deleted_files = []
|
157
314
|
end
|
158
315
|
|
159
316
|
str_date_time = date_times[index].strftime(date_time_format)
|
160
|
-
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" +
|
161
|
-
|
317
|
+
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" + gzip_extension
|
318
|
+
|
162
319
|
return expected_result
|
163
320
|
end
|
164
321
|
|
165
|
-
def
|
322
|
+
def rotate_file_date_extension(count, rotations, gzip,
|
166
323
|
output_dir = nil,
|
167
324
|
date_time_format = LogRotate::Impl::DEFAULT_DATE_TIME_FORMAT)
|
168
325
|
|
169
326
|
source_dir = "#{TEMP_DIR}"
|
327
|
+
Dir.mkdir(source_dir)
|
328
|
+
|
170
329
|
base_name = "file.dat"
|
171
330
|
file = File.join(source_dir, base_name)
|
172
|
-
Dir.mkdir(source_dir)
|
173
331
|
|
174
|
-
is_output_dir =
|
175
|
-
if (output_dir) then
|
176
|
-
Dir.mkdir(output_dir)
|
177
|
-
is_output_dir = true
|
178
|
-
else
|
179
|
-
output_dir = source_dir
|
180
|
-
end
|
181
|
-
|
332
|
+
is_output_dir, output_dir = resolve_output_dir(source_dir, output_dir)
|
182
333
|
manipulate_garbage_files(output_dir, base_name, :insert)
|
183
334
|
|
184
335
|
#rotate the file
|
@@ -201,7 +352,7 @@ class LogRotateTest < Test::Unit::TestCase
|
|
201
352
|
result = LogRotate.rotate_file(file, options)
|
202
353
|
|
203
354
|
expected_result = get_expected_result_date_extension(iteration, base_name, date_times, count,
|
204
|
-
date_time_format, gzip, output_dir)
|
355
|
+
date_time_format, gzip, output_dir, false)
|
205
356
|
assert_equal(expected_result, result)
|
206
357
|
end
|
207
358
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logrotate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DesigningPatterns
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-10-21 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,9 +20,25 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 2.3.3
|
24
24
|
version:
|
25
|
-
description:
|
25
|
+
description: |-
|
26
|
+
This package is a library of methods that perform log rotation on files and
|
27
|
+
directories. The log rotate methods allow the caller to specify options (via
|
28
|
+
parameters) such as how many rotated files to keep, what type of
|
29
|
+
extension to place on the rotated file (date or a simple count), and
|
30
|
+
whether to zip the rotated files. Live log files (currently being
|
31
|
+
written to by a live process) can be rotated as well. The post_rotate
|
32
|
+
option is useful in that context, as it can be used to send a HUP
|
33
|
+
signal to notify the live process to reopen its log file.
|
34
|
+
|
35
|
+
This package was inspired by the need to have a library version of the
|
36
|
+
unix logrotate tool. The unix logrotate tool requires the user to
|
37
|
+
specify options in a config file, and is usually invoked through cron.
|
38
|
+
|
39
|
+
Directories can be rotated with this library. However, the gzip option
|
40
|
+
does not work with directories. In this case, please zip/tar the directory
|
41
|
+
in question before invoking this library.
|
26
42
|
email:
|
27
43
|
- technical.inquiries@designingpatterns.com
|
28
44
|
executables: []
|
@@ -30,13 +46,13 @@ executables: []
|
|
30
46
|
extensions: []
|
31
47
|
|
32
48
|
extra_rdoc_files:
|
33
|
-
- Manifest.txt
|
34
49
|
- History.txt
|
50
|
+
- Manifest.txt
|
35
51
|
- README.txt
|
36
52
|
files:
|
37
|
-
- Manifest.txt
|
38
|
-
- LICENSE
|
39
53
|
- History.txt
|
54
|
+
- LICENSE
|
55
|
+
- Manifest.txt
|
40
56
|
- README.txt
|
41
57
|
- Rakefile
|
42
58
|
- lib/logrotate.rb
|
@@ -45,7 +61,9 @@ files:
|
|
45
61
|
- lib/logrotate/rotateinfo.rb
|
46
62
|
- test/test_logrotate.rb
|
47
63
|
has_rdoc: true
|
48
|
-
homepage:
|
64
|
+
homepage: http://logrotate.rubyforge.org
|
65
|
+
licenses: []
|
66
|
+
|
49
67
|
post_install_message:
|
50
68
|
rdoc_options:
|
51
69
|
- --main
|
@@ -67,9 +85,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
85
|
requirements: []
|
68
86
|
|
69
87
|
rubyforge_project: logrotate
|
70
|
-
rubygems_version: 1.
|
88
|
+
rubygems_version: 1.3.5
|
71
89
|
signing_key:
|
72
|
-
specification_version:
|
73
|
-
summary: This package is a library of methods that perform log rotation
|
90
|
+
specification_version: 3
|
91
|
+
summary: This package is a library of methods that perform log rotation on files and directories
|
74
92
|
test_files:
|
75
93
|
- test/test_logrotate.rb
|