Class: Logtail::Logger

Inherits:
Logger
  • Object
show all
Includes:
ActiveSupport::LoggerSilence, ActiveSupport::LoggerThreadSafeLevel, LoggerSilence
Defined in:
lib/logtail-rails/logger.rb

Overview

The Logtail Logger behaves exactly like the standard Ruby ‘::Logger`, except that it supports a transparent API for logging structured data and events.

Examples:

Basic logging

logger.info "Payment rejected for customer #{customer_id}"

Logging an event

logger.info "Payment rejected", payment_rejected: {customer_id: customer_id, amount: 100}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_default_logger(source_token) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/logtail-rails/logger.rb', line 58

def self.create_default_logger(source_token)
  if ENV['LOGTAIL_SKIP_LOGS'].blank? && !Rails.env.test?
    io_device = Logtail::LogDevices::HTTP.new(source_token)
  else
    io_device = STDOUT
  end

  logger = self.create_logger(io_device)

  if defined?(Sidekiq)
    require "logtail-rails/sidekiq"

    Sidekiq.configure_server do |config|
      logger = self.create_logger(io_device, config.logger) if config.logger.class == Sidekiq::Logger
      config.logger = logger
    end
  end

  logger
end

.create_logger(*io_devices_and_loggers) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/logtail-rails/logger.rb', line 49

def self.create_logger(*io_devices_and_loggers)
  logger = Logtail::Logger.new(*io_devices_and_loggers)

  tagged_logging_supported = Rails::VERSION::MAJOR >= 7 || Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR >= 1
  logger = ::ActiveSupport::TaggedLogging.new(logger) if tagged_logging_supported

  logger
end

Instance Method Details

#broadcast_to(*io_devices_and_loggers) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/logtail-rails/logger.rb', line 31

def broadcast_to(*io_devices_and_loggers)
  io_devices_and_loggers.each do |io_device_or_logger|
    extra_logger = is_a_logger?(io_device_or_logger) ? io_device_or_logger : self.class.new(io_device_or_logger)

    @extra_loggers << extra_logger
  end
end

#broadcastsObject



27
28
29
# File 'lib/logtail-rails/logger.rb', line 27

def broadcasts
  [self] + @extra_loggers
end

#kind_of?(clazz) ⇒ Boolean Also known as: is_a?

Logtail::Logger also works as ActiveSupport::BroadcastLogger

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/logtail-rails/logger.rb', line 20

def kind_of?(clazz)
  return true if defined?(::ActiveSupport::BroadcastLogger) && clazz == ::ActiveSupport::BroadcastLogger

  super(clazz)
end

#stop_broadcasting_to(io_device_or_logger) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/logtail-rails/logger.rb', line 39

def stop_broadcasting_to(io_device_or_logger)
  if is_a_logger?(io_device_or_logger)
    @extra_loggers.delete(logger)

    return
  end

  @extra_loggers = @extra_loggers.reject { |logger| ::ActiveSupport::Logger.logger_outputs_to?(logger, io_device_or_logger) }
end