diff options
author | Thom May <thom@chef.io> | 2016-08-03 17:21:27 +0100 |
---|---|---|
committer | Thom May <thom@may.lt> | 2016-08-03 17:21:27 +0100 |
commit | c784525063a9aefe7f6782331d5bcc31ce8eeab0 (patch) | |
tree | 09b82ed528aec61d2b1eff4095547ed43819f091 | |
parent | e16d10faa2821fa7c0bdebb64a5fa556354878e3 (diff) | |
download | mixlib-log-tm/close_log_devices.tar.gz |
Ensure that log devices are closed when the logger is resettm/close_log_devices
Fixes: chef/chef#3435, Fixes: chef/chef#3436
Signed-off-by: Thom May <thom@may.lt>
-rw-r--r-- | lib/mixlib/log.rb | 12 | ||||
-rw-r--r-- | spec/mixlib/log_spec.rb | 41 |
2 files changed, 47 insertions, 6 deletions
diff --git a/lib/mixlib/log.rb b/lib/mixlib/log.rb index 5f216f9..55332e0 100644 --- a/lib/mixlib/log.rb +++ b/lib/mixlib/log.rb @@ -29,9 +29,21 @@ module Mixlib LEVEL_NAMES = LEVELS.invert.freeze def reset! + if @loggers + @loggers.each { |l| close(l) } + else + close(@logger) unless @logger.nil? + end + ensure @logger, @loggers = nil, nil end + def close(logger) + unless logger.instance_variable_get(:"@logdev").dev == STDOUT + logger.close + end + end + # An Array of log devices that will be logged to. Defaults to just the default # @logger log device, but you can push to this array to add more devices. def loggers diff --git a/spec/mixlib/log_spec.rb b/spec/mixlib/log_spec.rb index 22b3758..4565240 100644 --- a/spec/mixlib/log_spec.rb +++ b/spec/mixlib/log_spec.rb @@ -19,12 +19,18 @@ require "tempfile" require "stringio" +require "ostruct" require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper")) class LoggerLike attr_accessor :level attr_reader :messages - def initialize + def initialize(dev = STDOUT) + @messages = "" + @logdev = OpenStruct.new(:dev => dev) + end + + def close @messages = "" end @@ -85,7 +91,7 @@ describe Mixlib::Log do :info => Logger::INFO, :warn => Logger::WARN, :error => Logger::ERROR, - :fatal => Logger::FATAL + :fatal => Logger::FATAL, } levels.each do |symbol, constant| Logit.level = symbol @@ -107,7 +113,7 @@ describe Mixlib::Log do :info => Logger::INFO, :warn => Logger::WARN, :error => Logger::ERROR, - :fatal => Logger::FATAL + :fatal => Logger::FATAL, } levels.each do |symbol, constant| Logit.level(symbol) @@ -139,15 +145,38 @@ describe Mixlib::Log do end it "should default to STDOUT if init is called with no arguments" do - logger_mock = Struct.new(:formatter, :level).new - expect(Logger).to receive(:new).with(STDOUT).and_return(logger_mock) + expect(Logger).to receive(:new).with(STDOUT).and_call_original Logit.init end it "should have by default a base log level of warn" do - logger_mock = Struct.new(:formatter, :level).new Logit.init expect(Logit.level).to eq(:warn) end + describe "closes old loggers" do + it "should not try to close the STDOUT logger" do + log = LoggerLike.new(STDOUT) + Logit.init(log) + expect(log).to_not receive(:close) + Logit.reset! + end + + it "should close other loggers" do + log = LoggerLike.new(StringIO.new) + Logit.init(log) + expect(log).to receive(:close) + Logit.reset! + end + + it "should close all loggers" do + log1 = LoggerLike.new(StringIO.new) + log2 = LoggerLike.new(StringIO.new) + Logit.init + Logit.use_log_devices([log1, log2]) + expect(log1).to receive(:close) + expect(log2).to receive(:close) + Logit.reset! + end + end end |