-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Windows: Fix a bug that the wrong log file is reopened with log rotate setting when flushing or graceful reloading #4054
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
Merged
ashie
merged 9 commits into
fluent:master
from
daipom:fix-reopen-log-on-windows-with-rotate
Feb 16, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7b285a0
Windows: Apply the modified log path for rotation to reopen
daipom 560c1bb
Test: Make sure to close LogDevice
daipom abbcc64
Test: Reset tmp files at setup
daipom 1305157
Test: Make sure LoggerInitializerTest doesn't change the global logger
daipom 83041ee
Test: Add tests for rotate and reopening
daipom 6107d00
Test: Refactor LoggerInitializerTest
daipom 0d9581c
Test: Remove needless code
daipom ade4857
Test: Make sure not to leave tmp files
daipom 60c94ad
Test: Refactor logic of recovering global logger
daipom 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,46 +1,120 @@ | ||
require_relative 'helper' | ||
require 'fluent/supervisor' | ||
require 'fileutils' | ||
require 'pathname' | ||
|
||
class LoggerInitializerTest < ::Test::Unit::TestCase | ||
TMP_DIR = File.expand_path(File.dirname(__FILE__) + "/tmp/logger_initializer#{ENV['TEST_ENV_NUMBER']}") | ||
|
||
teardown do | ||
begin | ||
FileUtils.rm_rf(TMP_DIR) | ||
rescue => _ | ||
def setup | ||
@stored_global_logger = $log | ||
Dir.mktmpdir do |tmp_dir| | ||
@tmp_dir = Pathname(tmp_dir) | ||
yield | ||
end | ||
end | ||
|
||
test 'when path is given' do | ||
path = File.join(TMP_DIR, 'fluent_with_path.log') | ||
def teardown | ||
$log = @stored_global_logger | ||
end | ||
|
||
assert_false File.exist?(TMP_DIR) | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) | ||
mock.proxy(File).chmod(0o777, TMP_DIR).never | ||
test 'when path is given' do | ||
path = @tmp_dir + 'log' + 'fluent_with_path.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) | ||
mock.proxy(File).chmod(0o777, path.parent.to_s).never | ||
|
||
assert_nothing_raised do | ||
logger.init(:supervisor, 0) | ||
end | ||
$log.out.close | ||
|
||
assert_true File.exist?(TMP_DIR) | ||
assert { path.parent.exist? } | ||
end | ||
|
||
test 'apply_options with log_dir_perm' do | ||
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows? | ||
|
||
path = File.join(TMP_DIR, 'fluent_with_path.log') | ||
|
||
assert_false File.exist?(TMP_DIR) | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) | ||
mock.proxy(File).chmod(0o777, TMP_DIR).once | ||
path = @tmp_dir + 'log' + 'fluent_with_path.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) | ||
mock.proxy(File).chmod(0o777, path.parent.to_s).once | ||
|
||
assert_nothing_raised do | ||
logger.init(:supervisor, 0) | ||
end | ||
|
||
logger.apply_options(log_dir_perm: 0o777) | ||
assert_true File.exist?(TMP_DIR) | ||
assert_equal 0o777, (File.stat(TMP_DIR).mode & 0xFFF) | ||
$log.out.close | ||
|
||
assert { path.parent.exist? } | ||
assert_equal 0o777, (File.stat(path.parent).mode & 0xFFF) | ||
end | ||
|
||
test 'rotate' do | ||
path = @tmp_dir + 'log' + 'fluent.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500) | ||
logger.init(:supervisor, 0) | ||
begin | ||
10.times.each do | ||
$log.info "This is test message. This is test message. This is test message." | ||
end | ||
ensure | ||
$log.out.close | ||
end | ||
|
||
assert { path.parent.entries.size > 3 } # [".", "..", "logfile.log", ...] | ||
end | ||
|
||
test 'rotate to max age' do | ||
path = @tmp_dir + 'log' + 'fluent.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5, log_rotate_size: 500) | ||
logger.init(:supervisor, 0) | ||
begin | ||
100.times.each do | ||
$log.info "This is test message. This is test message. This is test message." | ||
end | ||
ensure | ||
$log.out.close | ||
end | ||
|
||
assert { path.parent.entries.size == 7 } # [".", "..", "logfile.log", ...] | ||
end | ||
|
||
test 'files for each process with rotate on Windows' do | ||
omit "Only for Windows." unless Fluent.windows? | ||
|
||
path = @tmp_dir + 'log' + 'fluent.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) | ||
logger.init(:supervisor, 0) | ||
$log.out.close | ||
|
||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) | ||
logger.init(:worker0, 0) | ||
$log.out.close | ||
|
||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) | ||
logger.init(:workers, 1) | ||
$log.out.close | ||
|
||
assert { path.parent.entries.size == 5 } # [".", "..", "logfile.log", ...] | ||
end | ||
|
||
test 'reopen!' do | ||
path = @tmp_dir + 'log' + 'fluent.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}) | ||
logger.init(:supervisor, 0) | ||
message = "This is test message." | ||
$log.info message | ||
logger.reopen! | ||
$log.info message | ||
$log.out.close | ||
|
||
assert { path.read.lines.select{ |line| line.include?(message) }.size == 2 } | ||
end | ||
|
||
test 'reopen! with rotate reopens the same file' do | ||
path = @tmp_dir + 'log' + 'fluent.log' | ||
logger = Fluent::Supervisor::LoggerInitializer.new(path.to_s, Fluent::Log::LEVEL_DEBUG, nil, nil, {}, log_rotate_age: 5) | ||
logger.init(:supervisor, 0) | ||
logger.reopen! | ||
$log.out.close | ||
|
||
assert { path.parent.entries.size == 3 } # [".", "..", "logfile.log", ...] | ||
end | ||
end |
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.
Uh oh!
There was an error while loading. Please reload this page.