diff --git a/catalog-info.yaml b/catalog-info.yaml index 5369821..9357d90 100644 --- a/catalog-info.yaml +++ b/catalog-info.yaml @@ -1,8 +1,11 @@ apiVersion: backstage.io/v1alpha1 kind: Component metadata: - name: twiglet + name: twiglet-ruby + title: Twiglet description: simple logging + annotations: + rubygems.org/name: "twiglet" spec: type: library lifecycle: production diff --git a/lib/twiglet/logger.rb b/lib/twiglet/logger.rb index faae95a..399930c 100644 --- a/lib/twiglet/logger.rb +++ b/lib/twiglet/logger.rb @@ -62,8 +62,14 @@ def warn(message_or_error = nil, &block) super(message, &block) end - def error(message = nil, error = nil, &block) - message = error_message(error, message) if error + def error(message_or_error = nil, error = nil, &block) + message = if error + error_message(error, message_or_error) + elsif message_or_error.is_a?(Exception) + error_message(message_or_error) + else + message_or_error + end super(message, &block) end diff --git a/lib/twiglet/version.rb b/lib/twiglet/version.rb index 8a546bf..86765e8 100644 --- a/lib/twiglet/version.rb +++ b/lib/twiglet/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Twiglet - VERSION = '3.9.1' + VERSION = '3.9.2' end diff --git a/test/logger_test.rb b/test/logger_test.rb index 80a73da..e4921db 100644 --- a/test/logger_test.rb +++ b/test/logger_test.rb @@ -335,8 +335,8 @@ actual_log = read_json(@buffer) assert_equal 'Artificially raised exception', actual_log[:message] - assert_equal 'divided by 0', actual_log[:error][:message] assert_equal 'ZeroDivisionError', actual_log[:error][:type] + assert_equal 'divided by 0', actual_log[:error][:message] assert_match 'test/logger_test.rb', actual_log[:error][:stack_trace].first end @@ -353,35 +353,54 @@ end it 'should log an error with string message' do - e = StandardError.new('Unknown error') + e = StandardError.new('Some error') @logger.error('Artificially raised exception with string message', e) actual_log = read_json(@buffer) assert_equal 'Artificially raised exception with string message', actual_log[:message] assert_equal 'StandardError', actual_log[:error][:type] - assert_equal 'Unknown error', actual_log[:error][:message] + assert_equal 'Some error', actual_log[:error][:message] end - it 'should log error type properly even when active_support/json is required' do - require 'active_support/json' - e = StandardError.new('Unknown error') - @logger.error('Artificially raised exception with string message', e) + it 'should log an error if no message is given' do + e = StandardError.new('Some error') + @logger.error(e) actual_log = read_json(@buffer) + assert_equal 'Some error', actual_log[:message] assert_equal 'StandardError', actual_log[:error][:type] + assert_equal 'Some error', actual_log[:error][:message] end - it 'can log just an error as "error", if no message is given' do - e = StandardError.new('some error') + it 'should log an error if nil message is given' do + e = StandardError.new('Some error') @logger.error(nil, e) actual_log = read_json(@buffer) - assert_equal 'some error', actual_log[:message] + assert_equal 'Some error', actual_log[:message] + assert_equal 'StandardError', actual_log[:error][:type] + assert_equal 'Some error', actual_log[:error][:message] + end + + it 'should log a string if no error is given' do + @logger.error('Some error') + + actual_log = read_json(@buffer) + + assert_equal 'Some error', actual_log[:message] + end + + it 'should log error type properly even when active_support/json is required' do + require 'active_support/json' + e = StandardError.new('Unknown error') + @logger.error('Artificially raised exception with string message', e) + + actual_log = read_json(@buffer) + assert_equal 'StandardError', actual_log[:error][:type] - assert_equal 'some error', actual_log[:error][:message] end [:debug, :info, :warn].each do |level|