Skip to content

Commit

Permalink
feat: adds Logger#with_destination to log blocks to custom destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Sep 16, 2024
1 parent 73e18bd commit ac6fe90
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ logger.info { "Hello world!" }
logger.info('Result of 1 + 1') { 1 + 1 } # Logs with a message of "Result: 2"
```

### Loggging exceptions
### Logging exceptions

Exceptions happen and when they do, you want to know about them. Klogger provides a helper method to log exceptions. These will automatically be logged with the `error` severity.

Expand Down Expand Up @@ -197,4 +197,9 @@ end
# Create a logger and add the destination
logger = Klogger.new(name)
logger.add_destination(GraylogDestination.new('graylog.example.com', 12201))

# If you only want to send certain data to another block, you can do so
logger.with_destination(other_destination) do
# ...
end
```
10 changes: 9 additions & 1 deletion lib/klogger/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def initialize(name = nil,
@destinations = []
@group_set = GroupSet.new
@silenced = Concurrent::ThreadLocalVar.new { false }
@block_destinations = Concurrent::ThreadLocalVar.new { [] }
@include_group_ids = include_group_ids
super(destination)
self.formatter = FORMATTERS[formatter].new(highlight: highlight)
Expand Down Expand Up @@ -98,6 +99,13 @@ def remove_destination(destination)
@destinations.delete(destination)
end

def with_destination(destination)
@block_destinations.value << destination
yield
ensure
@block_destinations.value.delete(destination)
end

def create_tagged_logger(**tags)
TaggedLogger.new(self, **tags)
end
Expand Down Expand Up @@ -142,7 +150,7 @@ def create_payload(severity, message, tags)
end

def call_destinations(payload, group_ids)
@destinations.each do |destination|
(@destinations + @block_destinations.value).each do |destination|
destination.call(self, payload.dup, group_ids)
rescue StandardError => e
# If something goes wrong in here, we don't want to break the application
Expand Down
15 changes: 15 additions & 0 deletions spec/helpers/fake_destination.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class FakeDestination

attr_reader :lines

def initialize
@lines = []
end

def call(logger, payload, group_ids)
@lines << [logger, payload, group_ids]
end

end
19 changes: 19 additions & 0 deletions spec/specs/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'spec_helper'
require 'klogger'
require 'klogger/logger'
require_relative '../helpers/fake_destination'

module Klogger

Expand Down Expand Up @@ -394,6 +395,24 @@ module Klogger
expect(logger.destinations).to_not include destination
end
end

describe '#with_destination' do
subject(:logger) { described_class.new('example', destination: output) }
let(:fake_destination) { FakeDestination.new }

it 'sends log output to the given destination for the duration of the block' do
logger.info 'line1'
logger.with_destination(fake_destination) do
logger.info 'line2'
logger.info 'line3'
end
logger.info 'line4'
expect(fake_destination.lines).to match [
[logger, hash_including(message: 'line2'), []],
[logger, hash_including(message: 'line3'), []]
]
end
end
end

end
Expand Down

0 comments on commit ac6fe90

Please sign in to comment.