Skip to content

Commit

Permalink
fearture(logging): Ensure logged messages are formatted correctly
Browse files Browse the repository at this point in the history
- Ensures logged messages have the correct bugsnag formatting
- Ensures custom loggers receive the formatted message rather than the raw message
- Adds a new public method - 'format_message'
  • Loading branch information
Cawllec committed Mar 26, 2018
1 parent 7b7f249 commit 0d4ffb5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/bugsnag/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ def initialize
# Set up logging
self.logger = Logger.new(STDOUT)
self.logger.level = Logger::INFO
self.logger.formatter = proc do |severity, datetime, progname, msg|
"** [Bugsnag] #{datetime}: #{msg}\n"
end

# Configure the bugsnag middleware stack
self.internal_middleware = Bugsnag::MiddlewareStack.new
Expand Down Expand Up @@ -169,19 +166,26 @@ def clear_request_data
##
# Logs an info level message
def info(message)
logger.info(message)
logger.info(format_message(message))
end

##
# Logs a warning level message
def warn(message)
logger.warn(message)
logger.warn(format_message(message))
end

##
# Logs a debug level message
def debug(message)
logger.debug(message)
logger.debug(format_message(message))
end

##
# Formats a message being logged by Bugsnag
def format_message(message)
datetime = Time.new.to_s
"** [Bugsnag] #{datetime}: #{message.to_s}\n"
end

private
Expand Down
75 changes: 75 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,81 @@
end
end

describe "logger" do
class TestLogger
attr_accessor :logs

def initialize
@logs = []
end

def log(level, message)
@logs << {
:level => level,
:message => message
}
end

def info(msg)
log('info', msg)
end

def warn(msg)
log('warning', msg)
end

def debug(msg)
log('debug', msg)
end
end

before do
@logger = TestLogger.new
@string_regex = /\*\* \[Bugsnag\] (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (\+\d{4})?): (Info|Warning|Debug) message\n$/
Bugsnag.configure do |bugsnag|
bugsnag.logger = @logger
end
end

it "should format a message correctly" do
formatted_msg = /\*\* \[Bugsnag\] (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (\+\d{4})?): message\n$/
expect(Bugsnag.configuration.format_message("message")).to match(formatted_msg)
end

it "should log info messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.info("Info message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log[:level]).to eq('info')
expect(log[:message]).to match(@string_regex)
end

it "should log warning messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.warn("Warning message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log[:level]).to eq('warning')
expect(log[:message]).to match(@string_regex)
end

it "should log debug messages to the set logger" do
expect(@logger.logs.size).to eq(0)
Bugsnag.configuration.debug("Debug message")
expect(@logger.logs.size).to eq(1)
log = @logger.logs.first
expect(log[:level]).to eq('debug')
expect(log[:message]).to match(@string_regex)
end

after do
Bugsnag.configure do |bugsnag|
bugsnag.logger = Logger.new(StringIO.new)
end
end
end

it "should have exit exception classes ignored by default" do
expect(subject.ignore_classes).to eq(Set.new([SystemExit, Interrupt]))
end
Expand Down

0 comments on commit 0d4ffb5

Please sign in to comment.