Skip to content

Commit b254942

Browse files
committed
Moved global logger out of Configuration object.
1 parent 7763f11 commit b254942

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed

lib/concurrent/configuration.rb

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
module Concurrent
99
extend Logging
1010

11+
# Suppresses all output when used for logging.
12+
NULL_LOGGER = lambda { |level, progname, message = nil, &block| }
13+
1114
# initialize the global executors
1215
class << self
1316

1417
# @!visibility private
15-
@@auto_terminate_global_executors = Concurrent::AtomicBoolean.new(true)
18+
@@global_logger = Atomic.new(NULL_LOGGER)
19+
20+
# @!visibility private
21+
@@auto_terminate_global_executors = AtomicBoolean.new(true)
1622

1723
# @!visibility private
18-
@@auto_terminate_all_executors = Concurrent::AtomicBoolean.new(true)
24+
@@auto_terminate_all_executors = AtomicBoolean.new(true)
1925

2026
# @!visibility private
2127
@@global_fast_executor = LazyReference.new do
@@ -31,11 +37,18 @@ class << self
3137

3238
# @!visibility private
3339
@@global_timer_set = LazyReference.new do
34-
Concurrent::TimerSet.new(
35-
stop_on_exit: @@auto_terminate_global_executors.value)
40+
TimerSet.new(stop_on_exit: @@auto_terminate_global_executors.value)
3641
end
3742
end
3843

44+
def self.global_logger
45+
@@global_logger.value
46+
end
47+
48+
def self.global_logger=(value)
49+
@@global_logger.value = value
50+
end
51+
3952
# Defines if global executors should be auto-terminated with an
4053
# `at_exit` callback. When set to `false` it will be the application
4154
# programmer's responsibility to ensure that the global thread pools
@@ -155,15 +168,15 @@ def self.kill_global_executors
155168
end
156169

157170
def self.wait_for_global_executors_termination(timeout = nil)
158-
latch = Concurrent::CountDownLatch.new(3)
171+
latch = CountDownLatch.new(3)
159172
[ global_fast_executor, global_io_executor, global_timer_set ].each do |executor|
160173
Thread.new{ executor.wait_for_termination(timeout); latch.count_down }
161174
end
162175
latch.wait(timeout)
163176
end
164177

165178
def self.new_fast_executor(opts = {})
166-
Concurrent::FixedThreadPool.new(
179+
FixedThreadPool.new(
167180
[2, Concurrent.processor_count].max,
168181
stop_on_exit: opts.fetch(:stop_on_exit, true),
169182
idletime: 60, # 1 minute
@@ -173,7 +186,7 @@ def self.new_fast_executor(opts = {})
173186
end
174187

175188
def self.new_io_executor(opts = {})
176-
Concurrent::ThreadPoolExecutor.new(
189+
ThreadPoolExecutor.new(
177190
min_threads: [2, Concurrent.processor_count].max,
178191
max_threads: Concurrent.processor_count * 100,
179192
stop_on_exit: opts.fetch(:stop_on_exit, true),
@@ -186,18 +199,33 @@ def self.new_io_executor(opts = {})
186199
# A gem-level configuration object.
187200
class Configuration
188201

189-
# a proc defining how to log messages, its interface has to be:
190-
# lambda { |level, progname, message = nil, &block| _ }
191-
attr_accessor :logger
192-
193202
# Create a new configuration object.
194203
def initialize
195-
@logger = no_logger
196204
end
197205

198206
# if assigned to {#logger}, it will log nothing.
207+
# @deprecated Use Concurrent::NULL_LOGGER instead
199208
def no_logger
200-
lambda { |level, progname, message = nil, &block| }
209+
warn '[DEPRECATED] Use Concurrent::NULL_LOGGER instead'
210+
NULL_LOGGER
211+
end
212+
213+
# a proc defining how to log messages, its interface has to be:
214+
# lambda { |level, progname, message = nil, &block| _ }
215+
#
216+
# @deprecated Use Concurrent.global_logger instead
217+
def logger
218+
warn '[DEPRECATED] Use Concurrent.global_logger instead'
219+
Concurrent.global_logger.value
220+
end
221+
222+
# a proc defining how to log messages, its interface has to be:
223+
# lambda { |level, progname, message = nil, &block| _ }
224+
#
225+
# @deprecated Use Concurrent.global_logger instead
226+
def logger=(value)
227+
warn '[DEPRECATED] Use Concurrent.global_logger instead'
228+
Concurrent.global_logger = value
201229
end
202230

203231
# @deprecated Use Concurrent.global_io_executor instead

lib/concurrent/logging.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Logging
1111
# @param [String, nil] message when nil block is used to generate the message
1212
# @yieldreturn [String] a message
1313
def log(level, progname, message = nil, &block)
14-
(@logger || Concurrent.configuration.logger).call level, progname, message, &block
14+
(@logger || Concurrent.global_logger).call level, progname, message, &block
1515
rescue => error
1616
$stderr.puts "`Concurrent.configuration.logger` failed to log #{[level, progname, message, block]}\n" +
1717
"#{error.message} (#{error.class})\n#{error.backtrace.join "\n"}"

spec/spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
logger = Logger.new($stderr)
2626
logger.level = Logger::WARN
27-
Concurrent.configuration.logger = lambda do |level, progname, message = nil, &block|
27+
Concurrent.global_logger = lambda do |level, progname, message = nil, &block|
2828
logger.add level, message, progname, &block
2929
end
3030

0 commit comments

Comments
 (0)