Skip to content

Commit d7ce956

Browse files
committed
Remove dependency on logger
* To fix the Ruby 3.3.5 warnings: #1061 * concurrent-ruby only uses 7 constants from Logger, so just copy those over.
1 parent 044020f commit d7ce956

File tree

11 files changed

+40
-41
lines changed

11 files changed

+40
-41
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ To use the tools in the Edge gem it must be required separately:
284284
require 'concurrent-edge'
285285
```
286286

287-
If the library does not behave as expected, `Concurrent.use_stdlib_logger(Logger::DEBUG)` could
287+
If the library does not behave as expected, `Concurrent.use_simple_logger(:DEBUG)` could
288288
help to reveal the problem.
289289

290290
## Installation

docs-source/actor/celluloid_benchmark.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@
77
# require 'stackprof'
88
# require 'profiler'
99

10-
logger = Logger.new($stderr)
11-
logger.level = Logger::INFO
12-
Concurrent.configuration.logger = lambda do |level, progname, message = nil, &block|
13-
logger.add level, message, progname, &block
14-
end
10+
Concurrent.use_simple_logger(:INFO)
1511

1612
scale = 1
1713
ADD_TO = (100 * scale).to_i

docs-source/actor/io.in.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'concurrent'
22

3-
# logger = Logger.new(STDOUT)
4-
# Concurrent.configuration.logger = logger.method(:add)
3+
# Concurrent.use_simple_logger(:WARN, STDOUT)
54

65
# First option is to use operation pool
76

docs-source/actor/io.out.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'concurrent' # => false
22

3-
# logger = Logger.new(STDOUT)
4-
# Concurrent.configuration.logger = logger.method(:add)
3+
# Concurrent.use_simple_logger(:WARN, STDOUT)
54

65
# First option is to use operation pool
76

docs-source/actor/main.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ Spawned actor cannot be garbage-collected until it's terminated. There is a refe
124124

125125
Actors are running on shared thread poll which allows user to create many actors cheaply.
126126
Downside is that these actors cannot be directly used to do IO or other blocking operations.
127-
Blocking operations could starve the `default_task_pool`. However there are two options:
127+
Blocking operations could starve the `global_fast_executor`. However there are two options:
128128

129-
- Create an regular actor which will schedule blocking operations in `global_operation_pool`
129+
- Create an regular actor which will schedule blocking operations in `global_io_executor`
130130
(which is intended for blocking operations) sending results back to self in messages.
131-
- Create an actor using `global_operation_pool` instead of `global_task_pool`, e.g.
132-
`AnIOActor.spawn name: :blocking, executor: Concurrent.configuration.global_operation_pool`.
131+
- Create an actor using `global_io_executor` instead of `global_fast_executor`, e.g.
132+
`AnIOActor.spawn name: :blocking, executor: Concurrent.global_io_executor`.
133133

134134
### Example
135135

examples/init.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ def do_stuff(*args)
44
:stuff
55
end
66

7-
Concurrent.use_simple_logger Logger::DEBUG
7+
Concurrent.use_simple_logger :DEBUG

lib/concurrent-ruby-edge/concurrent/actor/context.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def default_reference_class
8484
Reference
8585
end
8686

87-
# override to se different default executor, e.g. to change it to global_operation_pool
87+
# override to se different default executor, e.g. to change it to global_fast_executor
8888
# @return [Executor]
8989
def default_executor
9090
Concurrent.global_io_executor
@@ -109,7 +109,7 @@ def ask(message)
109109
# @example by option hash
110110
# inc2 = AdHoc.spawn(name: 'increment by 2',
111111
# args: [2],
112-
# executor: Concurrent.configuration.global_task_pool) do |increment_by|
112+
# executor: Concurrent.global_fast_executor) do |increment_by|
113113
# lambda { |number| number + increment_by }
114114
# end
115115
# inc2.ask!(2) # => 4

lib/concurrent-ruby-edge/concurrent/actor/internal_delegations.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
require 'logger'
1+
require 'concurrent/concern/logging'
22
require 'concurrent/actor/public_delegations'
33

44
module Concurrent
55
module Actor
66
module InternalDelegations
77
include PublicDelegations
8-
include Logger::Severity
8+
include Concurrent::Concern::Logging
99

1010
# @see Core#children
1111
def children

lib/concurrent-ruby-edge/concurrent/edge/erlang_actor.rb

+9-9
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ def initialize(mailbox, environment, name, executor)
674674
end
675675

676676
def tell_op(message)
677-
log Logger::DEBUG, @Pid, told: message
677+
log DEBUG, @Pid, told: message
678678
if (mailbox = @Mailbox)
679679
mailbox.push_op(message).then { @Pid }
680680
else
@@ -683,7 +683,7 @@ def tell_op(message)
683683
end
684684

685685
def tell(message, timeout = nil)
686-
log Logger::DEBUG, @Pid, told: message
686+
log DEBUG, @Pid, told: message
687687
if (mailbox = @Mailbox)
688688
timed_out = mailbox.push message, timeout
689689
timeout ? timed_out : @Pid
@@ -693,7 +693,7 @@ def tell(message, timeout = nil)
693693
end
694694

695695
def ask(message, timeout, timeout_value)
696-
log Logger::DEBUG, @Pid, asked: message
696+
log DEBUG, @Pid, asked: message
697697
if @Terminated.resolved?
698698
raise NoActor.new(@Pid)
699699
else
@@ -724,7 +724,7 @@ def ask(message, timeout, timeout_value)
724724
end
725725

726726
def ask_op(message, probe)
727-
log Logger::DEBUG, @Pid, asked: message
727+
log DEBUG, @Pid, asked: message
728728
if @Terminated.resolved?
729729
probe.reject NoActor.new(@Pid), false
730730
else
@@ -1029,7 +1029,7 @@ def terminate_self(reason, value)
10291029
end
10301030

10311031
def after_termination(final_reason)
1032-
log Logger::DEBUG, @Pid, terminated: final_reason
1032+
log DEBUG, @Pid, terminated: final_reason
10331033
clean_reply NoActor.new(@Pid)
10341034
while true
10351035
message = @Mailbox.try_pop NOTHING
@@ -1071,7 +1071,7 @@ def run(*args, &body)
10711071
inner_run(*args, &body).
10721072
run(Run::TEST).
10731073
then(&method(:after_termination)).
1074-
rescue { |e| log Logger::ERROR, e }
1074+
rescue { |e| log ERROR, e }
10751075
end
10761076

10771077
def receive(*rules, timeout: nil, timeout_value: nil, keep: false, &given_block)
@@ -1163,7 +1163,7 @@ def internal_receive
11631163
end
11641164

11651165
message_future.then(start, self) do |message, s, _actor|
1166-
log Logger::DEBUG, pid, got: message
1166+
log DEBUG, pid, got: message
11671167
catch(JUMP) do
11681168
if (message = consume_signal(message)) == NOTHING
11691169
@timeout = [@timeout + s - Concurrent.monotonic_time, 0].max if s
@@ -1230,7 +1230,7 @@ def receive(*rules, timeout: nil, timeout_value: nil, &given_block)
12301230
matcher = -> m { m.is_a?(Ask) ? rules_matcher === m.message : rules_matcher === m }
12311231
while true
12321232
message = @Mailbox.pop_matching(matcher, timeout, TIMEOUT)
1233-
log Logger::DEBUG, pid, got: message
1233+
log DEBUG, pid, got: message
12341234
unless (message = consume_signal(message)) == NOTHING
12351235
rules.each do |rule, job|
12361236
return eval_task(message, job) if rule === message
@@ -1535,7 +1535,7 @@ class NoReply < Error
15351535
def self.create(type, channel, environment, name, executor)
15361536
actor = KLASS_MAP.fetch(type).new(channel, environment, name, executor)
15371537
ensure
1538-
log Logger::DEBUG, actor.pid, created: caller[1] if actor
1538+
log Concern::Logging::DEBUG, actor.pid, created: caller[1] if actor
15391539
end
15401540

15411541
KLASS_MAP = {

lib/concurrent-ruby/concurrent/concern/logging.rb

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'logger'
21
require 'concurrent/atomic/atomic_reference'
32

43
module Concurrent
@@ -8,10 +7,12 @@ module Concern
87
#
98
# @!visibility private
109
module Logging
11-
include Logger::Severity
10+
# The same as Logger::Severity but we copy it here to avoid a dependency on the logger gem just for these 7 constants
11+
DEBUG, INFO, WARN, ERROR, FATAL, UNKNOWN = 0, 1, 2, 3, 4, 5
12+
SEV_LABEL = %w[DEBUG INFO WARN ERROR FATAL ANY].freeze
1213

1314
# Logs through {Concurrent.global_logger}, it can be overridden by setting @logger
14-
# @param [Integer] level one of Logger::Severity constants
15+
# @param [Integer] level one of Concurrent::Concern::Logging constants
1516
# @param [String] progname e.g. a path of an Actor
1617
# @param [String, nil] message when nil block is used to generate the message
1718
# @yieldreturn [String] a message
@@ -23,7 +24,7 @@ def log(level, progname, message = nil, &block)
2324
end
2425
logger.call level, progname, message, &block
2526
rescue => error
26-
$stderr.puts "`Concurrent.configuration.logger` failed to log #{[level, progname, message, block]}\n" +
27+
$stderr.puts "`Concurrent.global_logger` failed to log #{[level, progname, message, block]}\n" +
2728
"#{error.message} (#{error.class})\n#{error.backtrace.join "\n"}"
2829
end
2930
end
@@ -33,8 +34,10 @@ def log(level, progname, message = nil, &block)
3334
module Concurrent
3435
extend Concern::Logging
3536

36-
# @return [Logger] Logger with provided level and output.
37-
def self.create_simple_logger(level = Logger::FATAL, output = $stderr)
37+
# Create a simple logger with provided level and output.
38+
def self.create_simple_logger(level = :FATAL, output = $stderr)
39+
level = Concern::Logging.const_get(level) unless level.is_a?(Integer)
40+
3841
# TODO (pitr-ch 24-Dec-2016): figure out why it had to be replaced, stdlogger was deadlocking
3942
lambda do |severity, progname, message = nil, &block|
4043
return false if severity < level
@@ -52,21 +55,23 @@ def self.create_simple_logger(level = Logger::FATAL, output = $stderr)
5255

5356
output.print format "[%s] %5s -- %s: %s\n",
5457
Time.now.strftime('%Y-%m-%d %H:%M:%S.%L'),
55-
Logger::SEV_LABEL[severity],
58+
Concern::Logging::SEV_LABEL[severity],
5659
progname,
5760
formatted_message
5861
true
5962
end
6063
end
6164

6265
# Use logger created by #create_simple_logger to log concurrent-ruby messages.
63-
def self.use_simple_logger(level = Logger::FATAL, output = $stderr)
66+
def self.use_simple_logger(level = :FATAL, output = $stderr)
6467
Concurrent.global_logger = create_simple_logger level, output
6568
end
6669

67-
# @return [Logger] Logger with provided level and output.
70+
# Create a stdlib logger with provided level and output.
71+
# If you use this deprecated method you might need to add logger to your Gemfile to avoid warnings from Ruby 3.3.5+.
6872
# @deprecated
69-
def self.create_stdlib_logger(level = Logger::FATAL, output = $stderr)
73+
def self.create_stdlib_logger(level = :FATAL, output = $stderr)
74+
require 'logger'
7075
logger = Logger.new(output)
7176
logger.level = level
7277
logger.formatter = lambda do |severity, datetime, progname, msg|
@@ -93,7 +98,7 @@ def self.create_stdlib_logger(level = Logger::FATAL, output = $stderr)
9398

9499
# Use logger created by #create_stdlib_logger to log concurrent-ruby messages.
95100
# @deprecated
96-
def self.use_stdlib_logger(level = Logger::FATAL, output = $stderr)
101+
def self.use_stdlib_logger(level = :FATAL, output = $stderr)
97102
Concurrent.global_logger = create_stdlib_logger level, output
98103
end
99104

@@ -103,7 +108,7 @@ def self.use_stdlib_logger(level = Logger::FATAL, output = $stderr)
103108
NULL_LOGGER = lambda { |level, progname, message = nil, &block| }
104109

105110
# @!visibility private
106-
GLOBAL_LOGGER = AtomicReference.new(create_simple_logger(Logger::WARN))
111+
GLOBAL_LOGGER = AtomicReference.new(create_simple_logger(:WARN))
107112
private_constant :GLOBAL_LOGGER
108113

109114
def self.global_logger

spec/spec_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def requires=(paths)
4242
config.before :all do
4343
# Only configure logging if it has been required, to make sure the necessary require's are in place
4444
if Concurrent.respond_to? :use_simple_logger
45-
Concurrent.use_simple_logger Logger::FATAL
45+
Concurrent.use_simple_logger :FATAL
4646
end
4747
end
4848

0 commit comments

Comments
 (0)