Class: PostHog::Rails::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/posthog/rails/railtie.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.broadcast_rails_logger(appender) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Attach the appender to Rails.logger, supporting both the Rails 7.1+ BroadcastLogger and the older ActiveSupport::Logger.broadcast mechanism.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/posthog/rails/railtie.rb', line 231

def self.broadcast_rails_logger(appender)
  logger = ::Rails.logger
  return unless logger

  if logger.respond_to?(:broadcast_to)
    logger.broadcast_to(appender)
  elsif defined?(ActiveSupport::Logger) && ActiveSupport::Logger.respond_to?(:broadcast)
    logger.extend(ActiveSupport::Logger.broadcast(appender))
  else
    PostHog::Logging.logger.warn(
      'PostHog Logs could not broadcast Rails.logger; no compatible broadcast mechanism found.'
    )
  end
end

.install_posthog_logsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Build the PostHog Logs pipeline and broadcast Rails.logger into it.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/posthog/rails/railtie.rb', line 202

def self.install_posthog_logs
  unless PostHog.initialized?
    # logs_enabled is an explicit opt-in, so leave a breadcrumb instead
    # of silently skipping when PostHog.init never ran.
    PostHog::Logging.logger.warn(
      'PostHog Logs is enabled but PostHog.init has not been called; ' \
      'skipping log forwarding. Call PostHog.init in your initializer.'
    )
    return
  end

  # Mirror the core client: when it is disabled (missing/blank api_key)
  # every capture no-ops, so log forwarding should stay off too. The
  # client already logs its own missing-api_key error, so skip quietly.
  return unless PostHog.client.enabled?

  appender = PostHog::Rails::Logs::Setup.install
  return if appender.nil?

  broadcast_rails_logger(appender) if PostHog::Rails.config&.logs_forward_rails_logger
rescue StandardError => e
  PostHog::Logging.logger.warn("Failed to set up PostHog Logs: #{e.message}")
end

.rails_version_above_7?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


260
261
262
# File 'lib/posthog/rails/railtie.rb', line 260

def self.rails_version_above_7?
  ::Rails.version.to_f >= 7.0
end

.register_error_subscribervoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



248
249
250
251
252
253
254
255
256
# File 'lib/posthog/rails/railtie.rb', line 248

def self.register_error_subscriber
  return unless PostHog::Rails.config&.auto_capture_exceptions

  subscriber = PostHog::Rails::ErrorSubscriber.new
  ::Rails.error.subscribe(subscriber)
rescue StandardError => e
  PostHog::Logging.logger.warn("Failed to register error subscriber: #{e.message}")
  PostHog::Logging.logger.warn("Backtrace: #{e.backtrace&.first(5)&.join("\n")}")
end

Instance Method Details

#insert_middleware_after(app, target, middleware) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



90
91
92
# File 'lib/posthog/rails/railtie.rb', line 90

def insert_middleware_after(app, target, middleware)
  insert_middleware(app.config.middleware, :after, target, middleware)
end

#insert_middleware_before(app, target, middleware) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



96
97
98
# File 'lib/posthog/rails/railtie.rb', line 96

def insert_middleware_before(app, target, middleware)
  insert_middleware(app.config.middleware, :before, target, middleware)
end