Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle json Encoding::UndefinedConversionError #165

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add error logger to formatter class
  • Loading branch information
marcoadkins committed Sep 16, 2020
commit 3e180b5cab35ae838b302258ac488f77bab15acc
8 changes: 4 additions & 4 deletions lib/logstash-logger/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ module Formatter
autoload :Cee, 'logstash-logger/formatter/cee'
autoload :CeeSyslog, 'logstash-logger/formatter/cee_syslog'

def self.new(formatter_type, customize_event: nil)
build_formatter(formatter_type, customize_event)
def self.new(formatter_type, customize_event: nil, error_logger: LogStashLogger.configuration.default_error_logger)
build_formatter(formatter_type, customize_event, error_logger)
end

def self.build_formatter(formatter_type, customize_event)
def self.build_formatter(formatter_type, customize_event, error_logger)
formatter_type ||= DEFAULT_FORMATTER

formatter = if custom_formatter_instance?(formatter_type)
formatter_type
elsif custom_formatter_class?(formatter_type)
formatter_type.new
else
formatter_klass(formatter_type).new(customize_event: customize_event)
formatter_klass(formatter_type).new(customize_event: customize_event, error_logger: error_logger)
end

formatter.send(:extend, ::LogStashLogger::TaggedLogging::Formatter)
Expand Down
8 changes: 7 additions & 1 deletion lib/logstash-logger/formatter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ module Formatter
HOST = ::Socket.gethostname

class Base < ::Logger::Formatter
attr_accessor :error_logger
include ::LogStashLogger::TaggedLogging::Formatter

def initialize(customize_event: nil)
def initialize(customize_event: nil, error_logger: LogStashLogger.configuration.default_error_logger)
@customize_event = customize_event
@error_logger = error_logger
super()
end

Expand Down Expand Up @@ -74,6 +76,10 @@ def force_utf8_encoding(event)
event.message = original_message.force_encoding('UTF-8')
event
end

def log_error(e)
error_logger.error "[#{self.class}] #{e.class} - #{e.message}"
end
end
end
end