summaryrefslogtreecommitdiff
path: root/lib/mixlib
diff options
context:
space:
mode:
authorMax Horbul <max@gorbul.net>2016-01-12 13:42:01 -0800
committerMax Horbul <max@gorbul.net>2016-03-18 10:02:20 -0700
commit32b53a2632d06f3bd6e75b15a24d74b60ced1fe9 (patch)
treefc315801d7b03e2d25772d6a386c25e9f3e4db27 /lib/mixlib
parente16d10faa2821fa7c0bdebb64a5fa556354878e3 (diff)
downloadmixlib-log-32b53a2632d06f3bd6e75b15a24d74b60ced1fe9.tar.gz
File log devices opened by mixlib-log should be closed.
Ruby does not have a destructor concept so the opened files should be closed explicitely. The ruby logger which is used as a default logger here does not close logdevices it opened until #close method is called. When #rest! happens the logger instance gets destroyed but the #close method is not triggered. This patch should help to solve the problem described here: https://github.com/chef/chef/issues/3435
Diffstat (limited to 'lib/mixlib')
-rw-r--r--lib/mixlib/log.rb22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb
index 5f216f9..1d0e302 100644
--- a/lib/mixlib/log.rb
+++ b/lib/mixlib/log.rb
@@ -29,6 +29,7 @@ module Mixlib
LEVEL_NAMES = LEVELS.invert.freeze
def reset!
+ close!
@logger, @loggers = nil, nil
end
@@ -156,5 +157,26 @@ module Mixlib
end
end
+ def all_loggers
+ [@logger, *@loggers].uniq
+ end
+
+ # select all loggers with File log devices
+ def loggers_to_close
+ loggers_to_close = []
+ all_loggers.each do |logger|
+ next unless logdev = logger.instance_variable_get(:"@logdev")
+ loggers_to_close << logger if logdev.filename
+ end
+ loggers_to_close
+ end
+
+ def close!
+ # try to close all file loggers
+ loggers_to_close.each do |l|
+ l.close rescue nil
+ end
+ end
+
end
end