-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable log rotate for log #1235
Merged
+220
−11
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf85d04
Add two arguments `--log-rotate-age` and `--log-rotate-size`
ganmacs c8575e5
Pass `log_rotate_age` and `log_rotate_size` option to `ServerEngine::…
ganmacs ae21535
Add logdevio tests
ganmacs b4c6101
Fix failed tests
ganmacs 761be6d
Use strings as a constant variable and specify a rescue type
ganmacs 7846fa4
Use a keyword argument to add optoins.
ganmacs 4b86ef8
Use a timezone helper method
ganmacs 7bf4ab4
Add a test to assert rotate size
ganmacs b5513d1
SIZE -> BYTES
ganmacs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,14 @@ | |
require 'fluent/engine' | ||
require 'fluent/log' | ||
require 'timecop' | ||
require 'logger' | ||
|
||
class LogTest < Test::Unit::TestCase | ||
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/log/#{ENV['TEST_ENV_NUMBER']}") | ||
|
||
def setup | ||
FileUtils.rm_rf(TMP_DIR) | ||
FileUtils.mkdir_p(TMP_DIR) | ||
@log_device = Fluent::Test::DummyLogDevice.new | ||
@timestamp = Time.parse("2016-04-21 11:58:41 +0900") | ||
@timestamp_str = @timestamp.strftime("%Y-%m-%d %H:%M:%S %z") | ||
|
@@ -404,6 +409,84 @@ def test_level_reload | |
# check fluentd log side level is also changed | ||
assert_equal(Fluent::Log::LEVEL_DEBUG, log.level) | ||
end | ||
|
||
DAY_SEC = 60 * 60 * 24 | ||
data( | ||
rotate_daily_age: ['daily', 100000, DAY_SEC + 1], | ||
rotate_weekly_age: ['weekly', 100000, DAY_SEC * 7 + 1], | ||
rotate_monthly_age: ['monthly', 100000, DAY_SEC * 31 + 1], | ||
rotate_size: [1, 100, 0, '0'], | ||
) | ||
def test_log_with_logdevio(expected) | ||
with_timezone('utc') do | ||
@timestamp = Time.parse("2016-04-21 00:00:00 +0000") | ||
@timestamp_str = @timestamp.strftime("%Y-%m-%d %H:%M:%S %z") | ||
Timecop.freeze(@timestamp) | ||
|
||
rotate_age, rotate_size, travel_term = expected | ||
path = "#{TMP_DIR}/log-dev-io-#{rotate_size}-#{rotate_age}" | ||
|
||
logdev = Fluent::LogDeviceIO.new(path, shift_age: rotate_age, shift_size: rotate_size) | ||
logger = ServerEngine::DaemonLogger.new(logdev) | ||
log = Fluent::Log.new(logger) | ||
|
||
msg = 'a' * 101 | ||
log.info msg | ||
assert_match msg, File.read(path) | ||
|
||
Timecop.freeze(@timestamp + travel_term) | ||
|
||
msg2 = 'b' * 101 | ||
log.info msg2 | ||
c = File.read(path) | ||
|
||
assert_match msg2, c | ||
assert_not_equal msg, c | ||
end | ||
end | ||
|
||
def test_log_rotates_specifed_size_with_logdevio | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should check '.1' file is NOT created, as a test for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added. 7bf4ab4 |
||
with_timezone('utc') do | ||
rotate_age = 2 | ||
rotate_size = 100 | ||
path = "#{TMP_DIR}/log-dev-io-#{rotate_size}-#{rotate_age}" | ||
path0 = path + '.0' | ||
path1 = path + '.1' | ||
|
||
logdev = Fluent::LogDeviceIO.new(path, shift_age: rotate_age, shift_size: rotate_size) | ||
logger = ServerEngine::DaemonLogger.new(logdev) | ||
log = Fluent::Log.new(logger) | ||
|
||
msg = 'a' * 101 | ||
log.info msg | ||
assert_match msg, File.read(path) | ||
assert_true File.exist?(path) | ||
assert_true !File.exist?(path0) | ||
assert_true !File.exist?(path1) | ||
|
||
# create log.0 | ||
msg2 = 'b' * 101 | ||
log.info msg2 | ||
c = File.read(path) | ||
c0 = File.read(path0) | ||
assert_match msg2, c | ||
assert_match msg, c0 | ||
assert_true File.exist?(path) | ||
assert_true File.exist?(path0) | ||
assert_true !File.exist?(path1) | ||
|
||
# rotate | ||
msg3 = 'c' * 101 | ||
log.info msg3 | ||
c = File.read(path) | ||
c0 = File.read(path0) | ||
assert_match msg3, c | ||
assert_match msg2, c0 | ||
assert_true File.exist?(path) | ||
assert_true File.exist?(path0) | ||
assert_true !File.exist?(path1) | ||
end | ||
end | ||
end | ||
|
||
class PluginLoggerTest < Test::Unit::TestCase | ||
|
@@ -570,3 +653,41 @@ def test_terminate | |
plugin.terminate | ||
end | ||
end | ||
|
||
class LogDeviceIOTest < Test::Unit::TestCase | ||
test 'flush' do | ||
io = StringIO.new | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_equal io, logdev.flush | ||
|
||
io.instance_eval { undef :flush } | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_raise NoMethodError do | ||
logdev.flush | ||
end | ||
end | ||
|
||
test 'tty?' do | ||
io = StringIO.new | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_equal io.tty?, logdev.tty? | ||
|
||
io.instance_eval { undef :tty? } | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_raise NoMethodError do | ||
logdev.tty? | ||
end | ||
end | ||
|
||
test 'sync=' do | ||
io = StringIO.new | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_true logdev.sync = true | ||
|
||
io.instance_eval { undef :sync= } | ||
logdev = Fluent::LogDeviceIO.new(io) | ||
assert_raise NoMethodError do | ||
logdev.sync = true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any reason to use
Integer()
instead ofto_i
?IMO
to_i
is widely used and normal for most ruby programmer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I have a reason to use
Integer()
here.I want to raise an error when it was passed a value which can't be changed to an integer.