From 0d4ffb51d30582bae27f5ec32a6400ffdb980d51 Mon Sep 17 00:00:00 2001 From: Alex Moinet Date: Mon, 26 Mar 2018 14:12:04 +0100 Subject: [PATCH] fearture(logging): Ensure logged messages are formatted correctly - 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' --- lib/bugsnag/configuration.rb | 16 +++++--- spec/configuration_spec.rb | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/lib/bugsnag/configuration.rb b/lib/bugsnag/configuration.rb index d95be3626..8bca16402 100644 --- a/lib/bugsnag/configuration.rb +++ b/lib/bugsnag/configuration.rb @@ -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 @@ -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 diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 4d5c56955..615c879d4 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -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