socketry / async

An awesome asynchronous event-driven reactor for Ruby.
MIT License
2.04k stars 85 forks source link

Configure log level specificly for Async #279

Closed ahx closed 9 months ago

ahx commented 9 months ago

I would like to be able to change the log level specificly for Async in tests. If I understand things correctly I can set the log level for all gems that are using Console, but not specificly for Async.

Could this be achieved by moving from Console.logger to Async.logger?

ioquatix commented 9 months ago

What log messages are you trying to hide?

ahx commented 9 months ago

What log messages are you trying to hide?

That is a very good question!

This is the warning:

  0.0s     warn: Async::Task [oid=0x1658] [ec=0x166c] [pid=17608] [2023-09-14 11:03:22 +0200]
               | Task may have ended with unhandled exception.
               |   StandardError: Boom
               |   → spec/multi_call_spec.rb:54 in `block (5 levels) in <top (required)>'

This my test that produces the warning. It's a bit verbose, but it helped me to learn how to use Async:

    it 'does not call the other procs' do
      other_proc = instance_double('Proc', call: nil)
      expect do
        described_class.call(
          [
            proc do
              # sleep 0.1 # With this line running the warning is not printed
              raise StandardError, 'Boom'
            end,
            proc do
              sleep 0.2
              other_proc.call
            end,
          ],
        )
      end.to raise_error(StandardError, 'Boom')
      expect(other_proc).not_to have_received(:call)
    end
ioquatix commented 9 months ago

You can disable logging for that particular task by doing the following:

# To disable:
Console.logger.disable(Async::Task)

# To clear the previous disable:
Console.logger.clear(Async::Task)
ahx commented 9 months ago

Oh that looks nice. Thanks.