Skip to content

Commit

Permalink
Bring back #disable! support
Browse files Browse the repository at this point in the history
As part of [#7](#7) we lost the
ability to totally disable the logger, across all threads. It's unclear
if this was intentional, but I am guessing it was not. This will restore
the behavior by returning a new `NullOutput` instance when trying to
write while disabled.

This might cause A LOT of object allocations, all with short lives.
Hopefully. If so, we could consider some form of memoization of the
instance.
  • Loading branch information
stevenharman committed Jul 26, 2021
1 parent e5e9e7e commit 557822d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,9 @@ logger.log foo: "hello", bar: nil # => foo=hello bar=null

Note that "null" is output in the `nil` case.

## Silence
## Silencing

There's a way to temporary silence the log emitter. This might be useful for
tests for example.
To temporary silence the log emitter, during tests, for example:

```ruby
logger.silence do
Expand All @@ -276,17 +275,23 @@ The typical setup for RSpec might look like this:
```ruby
RSpec.configure do |config|
config.around :each do |example|
MyLogger.silence &example
MyLogger.silence(&example)
end
end
```

Note that silence method will only suppress logging in the current thread.
It'll still produce output if you fire up a new thread. To silence it
completely, use `disable!` method. This will completely silence the logger
across all threads.
It'll still produce output if you fire up a new thread.
To silence it completely, use `#disable!` method.
This will completely silence the logger across all threads.

```ruby
# spec/spec_helper.rb
MyLogger.disable!
```

It can be re-enabled via `#enable!`

```ruby
MyLogger.enable!
```
19 changes: 18 additions & 1 deletion lib/l2meter/emitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Emitter

def initialize(configuration: Configuration.new)
@configuration = configuration
@enabled = true
@mutex = Mutex.new
end

def log(*args, &block)
Expand All @@ -28,6 +30,18 @@ def context(*context_data, &block)
end
end

def disable!
@mutex.synchronize do
@enabled = false
end
end

def enable!
@mutex.synchronize do
@enabled = true
end
end

def with_elapsed
context elapsed: elapse do
yield
Expand Down Expand Up @@ -268,7 +282,6 @@ def unwrap(args)
end

def thread_state
@mutex ||= Mutex.new
@mutex.synchronize do
@threads ||= {}

Expand All @@ -288,6 +301,10 @@ def dynamic_contexts
end

def output
@mutex.synchronize do
return NullOutput.new unless @enabled
end

thread_state[:output] ||= configuration.output
end

Expand Down
34 changes: 34 additions & 0 deletions spec/emitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -768,5 +768,39 @@
)
end
end

describe "#disable!" do
after do |example|
emitter.enable!
end

it "affects all threads" do
emitter.disable!

1_000.times do
emitter.log(thread: :main, line: 1)
end

thread_b = Thread.new {
1_000.times do
emitter.log(thread: :b, line: 2)
end
}

thread_c = Thread.new {
1_000.times do
emitter.log(thread: :c, line: 3)
end
}

[
thread_b,
thread_c
].each(&:join)

output.lines
expect(output.lines).to be_empty
end
end
end
end

0 comments on commit 557822d

Please sign in to comment.