Sha256: d082a0e1e674a1a96d6923d06c8d0bdd453de0b1e8464f01d6264bb884eae392

Contents?: true

Size: 1 KB

Versions: 11

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Truemail
  class Logger
    require 'logger'

    attr_reader :event, :stdout, :file

    def initialize(event, error_stdout, log_absolute_path)
      @event = event
      @stdout = error_stdout
      @file = log_absolute_path
    end

    def push(validator_instance)
      current_event = Truemail::Log::Event.new(event, validator_instance)
      return unless current_event.valid?
      create_logs(current_event.log_level, Truemail::Log::Serializer::ValidatorText.call(validator_instance))
    end

    private

    def init_log_file
      output_file = Pathname(file)
      return output_file if output_file.exist?
      output_file.parent.mkpath && FileUtils.touch(output_file)
      output_file
    end

    def create_logs(log_level, serialized_object)
      %i[stdout file].each do |output_type|
        next unless public_send(output_type)
        ::Logger.new(output_type.eql?(:stdout) ? $stdout : init_log_file).add(log_level) { serialized_object }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
truemail-2.2.3 lib/truemail/logger.rb
truemail-2.2.2 lib/truemail/logger.rb
truemail-2.2.1 lib/truemail/logger.rb
truemail-2.2.0 lib/truemail/logger.rb
truemail-2.1.0 lib/truemail/logger.rb
truemail-2.0.2 lib/truemail/logger.rb
truemail-2.0.1 lib/truemail/logger.rb
truemail-2.0.0 lib/truemail/logger.rb
truemail-1.9.2 lib/truemail/logger.rb
truemail-1.9.1 lib/truemail/logger.rb
truemail-1.9.0 lib/truemail/logger.rb