Skip to content

Commit

Permalink
Fix ActiveSupport Logger broadcast usage
Browse files Browse the repository at this point in the history
As the severity methods of the `Appsignal::Logger` didn't call the
`#add` method, the `ActiveSupport::Logger.broadcast` feature wasn't
working.

The severity methods now call `#add` instead of directly sending the
message to the extension.
  • Loading branch information
luismiramirez committed Mar 31, 2023
1 parent 9ac500c commit 4838947
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 43 deletions.
6 changes: 6 additions & 0 deletions .changesets/fix-activesupport-logger-broadcast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: "patch"
type: "fix"
---

The `Appsignal::Logger` is now compatible with `ActiveSupport::Logger.broadcast`.
51 changes: 8 additions & 43 deletions lib/appsignal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module Appsignal
# Logger that flushes logs to the AppSignal logging service
class Logger < ::Logger # rubocop:disable Metrics/ClassLength
class Logger < ::Logger
PLAINTEXT = 0
LOGFMT = 1
JSON = 2
Expand All @@ -32,7 +32,7 @@ def initialize(group, level: INFO, format: PLAINTEXT)
# We support the various methods in the Ruby
# logger class by supplying this method.
# @api private
def add(severity, message = nil, group = nil)
def add(severity, message = nil, group = nil, attributes = {})
severity ||= UNKNOWN
return true if severity < level
group = @group if group.nil?
Expand All @@ -52,7 +52,7 @@ def add(severity, message = nil, group = nil)
SEVERITY_MAP.fetch(severity, 0),
@format,
message,
Appsignal::Utils::Data.generate({})
Appsignal::Utils::Data.generate(attributes)
)
end
alias log add
Expand All @@ -65,14 +65,7 @@ def debug(message = nil, attributes = {})
return if DEBUG < level
message = yield if message.nil? && block_given?
return if message.nil?
message = formatter.call(DEBUG, Time.now, @group, message) if formatter
Appsignal::Extension.log(
@group,
2,
@format,
message,
Appsignal::Utils::Data.generate(attributes)
)
add(DEBUG, message, @group, attributes)
end

# Log an info level message
Expand All @@ -83,14 +76,7 @@ def info(message = nil, attributes = {})
return if INFO < level
message = yield if message.nil? && block_given?
return if message.nil?
message = formatter.call(INFO, Time.now, @group, message) if formatter
Appsignal::Extension.log(
@group,
3,
@format,
message,
Appsignal::Utils::Data.generate(attributes)
)
add(INFO, message, @group, attributes)
end

# Log a warn level message
Expand All @@ -101,14 +87,7 @@ def warn(message = nil, attributes = {})
return if WARN < level
message = yield if message.nil? && block_given?
return if message.nil?
message = formatter.call(WARN, Time.now, @group, message) if formatter
Appsignal::Extension.log(
@group,
5,
@format,
message,
Appsignal::Utils::Data.generate(attributes)
)
add(WARN, message, @group, attributes)
end

# Log an error level message
Expand All @@ -119,14 +98,7 @@ def error(message = nil, attributes = {})
return if ERROR < level
message = yield if message.nil? && block_given?
return if message.nil?
message = formatter.call(ERROR, Time.now, @group, message) if formatter
Appsignal::Extension.log(
@group,
6,
@format,
message,
Appsignal::Utils::Data.generate(attributes)
)
add(ERROR, message, @group, attributes)
end

# Log a fatal level message
Expand All @@ -137,14 +109,7 @@ def fatal(message = nil, attributes = {})
return if FATAL < level
message = yield if message.nil? && block_given?
return if message.nil?
message = formatter.call(FATAL, Time.now, @group, message) if formatter
Appsignal::Extension.log(
@group,
7,
@format,
message,
Appsignal::Utils::Data.generate(attributes)
)
add(FATAL, message, @group, attributes)
end
end
end

0 comments on commit 4838947

Please sign in to comment.