Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for shared context and tagged log entries #10

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/dry/logger/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Dispatcher
# @api private
attr_reader :id

# @since 1.0.0
# @api public
attr_reader :context

# @since 1.0.0
# @api private
attr_reader :backends
Expand All @@ -25,6 +29,10 @@ class Dispatcher
# @api private
attr_reader :options

# @since 1.0.0
# @api private
attr_reader :mutex

# Set up a dispatcher
#
# @since 1.0.0
Expand All @@ -42,10 +50,18 @@ def self.setup(id, **options)

# @since 1.0.0
# @api private
def initialize(id, backends: [], **options)
def self.default_context
Thread.current[:__dry_logger__] ||= {}
end

# @since 1.0.0
# @api private
def initialize(id, backends: [], context: self.class.default_context, **options)
@id = id
@backends = backends
@options = {**options, progname: id}
@mutex = Mutex.new
@context = context
end

# Log an entry with UNKNOWN severity
Expand Down Expand Up @@ -130,14 +146,28 @@ def log(severity, message = nil, **payload)
case message
when Hash then log(severity, nil, **message)
else
entry = Entry.new(progname: id, severity: severity, message: message, payload: payload)
entry = Entry.new(
progname: id,
severity: severity,
message: message,
payload: {**context, **payload}
)

each_backend { |backend| backend.__send__(severity, entry) if backend.log?(entry) }
end

true
end

# @since 1.0.0
# @api public
def tagged(tag)
context[:tag] = tag
yield
ensure
context.delete(:tag)
end

# Add a new backend to an existing dispatcher
#
# @since 1.0.0
Expand All @@ -153,7 +183,9 @@ def add_backend(instance = nil, **backend_options)
# @since 1.0.0
# @api private
def each_backend(*_args, &block)
backends.each(&block)
mutex.synchronize do
backends.each(&block)
end
end

# Pass logging to all configured backends
Expand Down
28 changes: 28 additions & 0 deletions spec/dry/logger/dispatcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,32 @@
expect(backend_two).to have_received(:close)
end
end

describe "#tagged" do
subject(:logger) { Dry.Logger(:test, stream: stream, template: "%<message>s", context: {}) }

it "sets tags in log entries" do
logger.tagged(:metrics) do
logger.info("test 1")
logger.info("test 2")
end

expect(stream).to include("test 1 tag=:metrics")
expect(stream).to include("test 2 tag=:metrics")
end
end

describe "#context" do
subject(:logger) { Dry.Logger(:test, stream: stream, template: "%<message>s", context: {}) }

it "allows set pre-defined payload data" do
logger.context[:component] = "test"

logger.info("test 1")
logger.info("test 2")

expect(stream).to include('test 1 component="test"')
expect(stream).to include('test 2 component="test"')
end
end
end