Sha256: dfbb81490d5fc2c1e3673bc7d3592e9fd515cf6be9c8756a477628ebe8de1602

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require 'logger'
require 'fileutils'

module Soaspec

  # Handles logs of API requests and responses
  class SpecLogger
    @traffic_folder = 'logs' # Folder to put API traffic logs

    class << self
      # Folder to put API traffic logs
      attr_accessor :traffic_folder

      # @return [String] Traffic file to create logs at
      def traffic_file
        filename = "traffic_#{Time.now.strftime('%Y-%m-%d_%H_%M_%S')}.log"
        File.join(traffic_folder, filename)
      end

      # Create new log file if necessary and setup logging level
      # @return [Logger] Logger class to record API traffic
      def create
        unless File.exist?(traffic_file)
          FileUtils.mkdir_p traffic_folder
          FileUtils.touch traffic_file
        end
        @logger = Logger.new(traffic_file) # Where request and responses of APIs are stored
        @logger.level = Logger::DEBUG
        @logger
      end

      # Log a message using Soaspec logger
      # @param [String] message The message to add to the logger
      def info(message)
        return unless Soaspec.log_api_traffic?
        if message.respond_to? :each
          message.each do |message_item|
            @logger.info(message_item)
          end
        else
          @logger.info(message)
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
soaspec-0.2.8 lib/soaspec/spec_logger.rb
soaspec-0.2.7 lib/soaspec/spec_logger.rb