diff --git a/.changesets/add-default-attributes-to-logger.md b/.changesets/add-default-attributes-to-logger.md index f5a0e8d5..fa0e4df6 100644 --- a/.changesets/add-default-attributes-to-logger.md +++ b/.changesets/add-default-attributes-to-logger.md @@ -3,4 +3,10 @@ bump: patch type: add --- -Add default attributes to Logger +Allow for default attributes to be given when initialising a `Logger` instance: + +```ruby +order_logger = Appsignal::Logger.new("app", attributes: { order_id: 123 }) +``` + +All log lines reported by this logger will contain the given attribute. Attributes given when reporting the log line will be merged with the default attributes for the logger, with those in the log line taking priority. diff --git a/spec/lib/appsignal/logger_spec.rb b/spec/lib/appsignal/logger_spec.rb index 752b8b86..56af7bdb 100644 --- a/spec/lib/appsignal/logger_spec.rb +++ b/spec/lib/appsignal/logger_spec.rb @@ -201,6 +201,15 @@ expect(default_attributes).to eq({ :some_key => "some_value" }) expect(line_attributes).to eq({ :other_key => "other_value" }) end + + it "prioritises line attributes over default attributes" do + logger = Appsignal::Logger.new("group", :attributes => { :some_key => "some_value" }) + + expect(Appsignal::Extension).to receive(:log).with("group", 6, 0, "Some message", + Appsignal::Utils::Data.generate({ :some_key => "other_value" })) + + logger.error("Some message", { :some_key => "other_value" }) + end end describe "#error with exception object" do