Class: ZuoraObservability::Logger
- Inherits:
-
Ougai::Logger
- Object
- Ougai::Logger
- ZuoraObservability::Logger
- Defined in:
- lib/zuora_observability/logger.rb
Overview
A configurable logger that can be used for Rails and additional libraries
Class Method Summary collapse
Instance Method Summary collapse
- #create_formatter ⇒ Object
- #custom_data_formatter(data) ⇒ Object
- #custom_error_formatter(data) ⇒ Object
- #custom_message_formatter(severity, datetime, _progname, data) ⇒ Object
-
#initialize(logdev) ⇒ Logger
constructor
TODO(hartley): enable silence method for Rails 5.2 asset requests github.com/tilfin/ougai/wiki/Use-as-Rails-logger.
Constructor Details
#initialize(logdev) ⇒ Logger
TODO(hartley): enable silence method for Rails 5.2 asset requests github.com/tilfin/ougai/wiki/Use-as-Rails-logger
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/zuora_observability/logger.rb', line 13 def initialize(logdev, **) super # NOTE(hartley): the purpose for the original split between Ougai and # MonoLogger was that MonoLogger enables logging in a trap context # https://github.com/tilfin/ougai/issues/74 # By using our own Logger class, we can override the LogDevice created # by ruby with MonoLogger's, enabling logging in a trap context @logdev = MonoLogger::LocklessLogDevice.new(logdev) end |
Class Method Details
.custom_logger(name: '', level: Rails.logger.present? ? Rails.logger.level : MonoLogger::INFO, type: :ougai) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/zuora_observability/logger.rb', line 58 def self.custom_logger(name: '', level: Rails.logger.present? ? Rails.logger.level : MonoLogger::INFO, type: :ougai) if type == :ougai logger = new($stdout, level: level, progname: name) else ActiveSupport::Deprecation.warn(<<-MSG.squish) Creating a non-ougai custom_logger is deprecated and will be removed in a future version of zuora_observability. MSG require 'mono_logger' logger = MonoLogger.new(STDOUT) logger.level = level logger.formatter = proc do |serverity, datetime, progname, msg| begin msg = JSON.parse(msg) rescue JSON::ParserError => ex end if ZuoraObservability.configuration.json_logging require 'json' store = { name: name, level: serverity, timestamp: datetime.strftime('%FT%T.%6NZ'), pid: Process.pid, message: name == "ActionMailer" ? msg.strip : msg } JSON.dump(store) + "\n" else format('%s %s: %s', serverity.ljust(6), datetime, msg) + "\n" end end end logger end |
Instance Method Details
#create_formatter ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zuora_observability/logger.rb', line 24 def create_formatter return ZuoraObservability::Logging::Formatter.new if ZuoraObservability.configuration.json_logging formatter = Ougai::Formatters::Customizable.new( format_err: method(:custom_error_formatter), format_data: method(:custom_data_formatter), format_msg: method(:custom_message_formatter) ) formatter.datetime_format = '%FT%T.%3NZ' formatter end |
#custom_data_formatter(data) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/zuora_observability/logger.rb', line 41 def custom_data_formatter(data) data.delete_if do |k| %i[app_instance_id tenant_ids organization environment].include? k end return nil if data.blank? "#{'DATA'.ljust(6)} #{Time.current.strftime('%FT%T.%3NZ')}: #{data.to_json}" end |
#custom_error_formatter(data) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/zuora_observability/logger.rb', line 51 def custom_error_formatter(data) return nil unless data.key?(:err) err = data.delete(:err) " #{err[:name]} (#{err[:message]})\n #{err[:stack]}" end |
#custom_message_formatter(severity, datetime, _progname, data) ⇒ Object
36 37 38 39 |
# File 'lib/zuora_observability/logger.rb', line 36 def (severity, datetime, _progname, data) msg = data.delete(:msg) "#{severity.ljust(6)} #{datetime}: #{msg}" end |