Sha256: b9a8fb77161b70a2775b2d196857024f2f145d3f3f51c5390380012bc208bcc6

Contents?: true

Size: 1 KB

Versions: 46

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

46 entries across 46 versions & 1 rubygems

Version Path
truemail-3.1.2 lib/truemail/logger.rb
truemail-3.1.1 lib/truemail/logger.rb
truemail-3.1.0 lib/truemail/logger.rb
truemail-3.0.9 lib/truemail/logger.rb
truemail-3.0.8 lib/truemail/logger.rb
truemail-3.0.7 lib/truemail/logger.rb
truemail-3.0.6 lib/truemail/logger.rb
truemail-3.0.5 lib/truemail/logger.rb
truemail-3.0.4 lib/truemail/logger.rb
truemail-3.0.3 lib/truemail/logger.rb
truemail-3.0.2 lib/truemail/logger.rb
truemail-3.0.1 lib/truemail/logger.rb
truemail-3.0.0 lib/truemail/logger.rb
truemail-2.7.5 lib/truemail/logger.rb
truemail-2.7.4 lib/truemail/logger.rb
truemail-2.7.3 lib/truemail/logger.rb
truemail-2.7.2 lib/truemail/logger.rb
truemail-2.7.1 lib/truemail/logger.rb
truemail-2.7.0 lib/truemail/logger.rb
truemail-2.6.6 lib/truemail/logger.rb