Sha256: 8bc46e37b503c8a7e051c15b81e06e18276c03f9da20984b54c6b04332ae5784

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

module Stenotype
  module Adapters
    #
    # An adapter implementing method {#publish} to send data to STDOUT
    #
    # @example
    #   class SomeClassWithEvents
    #     def method_emitting_enent
    #       result_of_calculations = collect_some_data
    #       # This will print the data to STDOUT by default
    #       stdout_adapter.publish(result_of_calculation, additional: :data, more: :data)
    #       result_of_calculations
    #     end
    #
    #     def stdout_adapter
    #       Stenotype::Adapters::StdoutAdapter.new
    #     end
    #   end
    #
    class StdoutAdapter < Base
      #
      # @param event_data {Sting} The data to be published to STDOUT
      #
      # @example Publishing to default client (STDOUT)
      #  adapter = Stenotype::Adapters::StdoutAdapter.new
      #  adapter.publish({ event: :data }, { additional: :data })
      #
      # @example Publishing to custom client (STDERR)
      #  adapter = Stenotype::Adapters::StdoutAdapter.new(client: STDERR)
      #  adapter.publish({ event: :data }, { additional: :data })
      #
      def publish(event_data, **additional_attrs)
        client.info("[Stenotype::Event] emitted with the following attributes") do
          "MESSAGE BODY: #{event_data}, MESSAGE ATTRIBUTES #{additional_attrs.to_json}"
        end
      end

      #
      # Does nothing
      #
      def flush!
        # noop
      end

      private

      def client
        @client ||= (config.logger || Logger.new(STDOUT))
      end

      def config
        Stenotype.config
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stenotype-0.1.19 lib/stenotype/adapters/stdout_adapter.rb
stenotype-0.1.17 lib/stenotype/adapters/stdout_adapter.rb
stenotype-0.1.16 lib/stenotype/adapters/stdout_adapter.rb
stenotype-0.1.15 lib/stenotype/adapters/stdout_adapter.rb
stenotype-0.1.13 lib/stenotype/adapters/stdout_adapter.rb
stenotype-0.1.12 lib/stenotype/adapters/stdout_adapter.rb