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

Logger local named tags [WIP] #301

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion lib/semantic_logger/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,15 @@ def tags
end

def named_tags
SemanticLogger.named_tags
SemanticLogger.named_tags.merge(@named_tags || {})
end

# Sets named tags which are local to the logger instance.
#
# Local named tags are merged into the log entry along with the thread-level ones
# and those set via `#tagged` or `#with_tags`.
def named_tags=(tags)
(@named_tags ||= {}).merge!(tags)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it just me or this feels a little strange?
I'm thinking the expected behavior would be for the whole value to be overridden, right?

end

# Returns the list of tags pushed after flattening them out and removing blanks
Expand Down Expand Up @@ -327,6 +335,10 @@ def log_internal(level, index, message = nil, payload = nil, exception = nil)
end

log = Log.new(name, level, index)

# Apply the named tags to the log entry
log.named_tags = named_tags
Copy link
Author

@theocodes theocodes Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this was the best way to handle this. Thought of maybe passing the tags to the log initializer but was conscious that might break the API. 🤔


should_log =
if exception.nil? && payload.nil? && message.is_a?(Hash)
# All arguments as a hash in the message.
Expand Down
56 changes: 56 additions & 0 deletions test/logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ def self.call(log)
end
end

describe "#named_tags=" do
it "adds local named tags to logger" do
logger.named_tags = {test: "123"}
assert_equal({test: "123"}, logger.named_tags)
assert_equal({}, SemanticLogger.named_tags)
end
end

describe "Compatibility" do
# Ensure that any log level can be logged
Logger::Severity.constants.each do |level|
Expand Down Expand Up @@ -152,6 +160,54 @@ def self.call(log)
assert_equal logger.object_id, yielded_logger.object_id
end
end

it "adds logger-local named tags to log entries" do
logger.named_tags = {test: "123"}

logger.tagged do
logger.info("test")
end

assert log = logger.events.first
assert_equal({test: "123"}, log.named_tags)
end

it "does not change local tags" do
logger.named_tags = {test: "123"}

logger.tagged(foo: "bar") do
logger.info("test")
end

assert_equal({test: "123"}, logger.named_tags)
assert_equal({}, SemanticLogger.named_tags)
assert_equal({test: "123", foo: "bar"}, logger.events.first.named_tags)
end

it "does not override global tags" do
SemanticLogger.push_named_tags(global: true)
logger.named_tags = {test: "123"}

logger.info("test")

assert_equal({global: true, test: "123"}, logger.events.first.named_tags)
assert_equal({global: true}, SemanticLogger.named_tags)

SemanticLogger.pop_named_tags
end

it "does no add local named tags to other loggers in block" do
another_logger = SemanticLogger::Test::CaptureLogEvents.new
logger.named_tags = {test: "123"}

logger.tagged(foo: "bar") do
logger.info("has local tags")
another_logger.info("does not have local tags")
end

assert_equal({test: "123", foo: "bar"}, logger.events.first.named_tags)
assert_equal({foo: "bar"}, another_logger.events.first.named_tags)
end
end
end
end