Sha256: 0e6ea20815e9848db562f4a24f36a7043d94ff55b0391b5e7f168c9e9fad86f8

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

require 'logger'
require 'thread'

module MultiSync
  module Logging
    MUTEX = Mutex.new

    def logger
      @logger || initialize_logger
    end

    def logger=(new_logger)
      @logger = new_logger ? new_logger : Logger.new('/dev/null')
    end

    def status_logger
      @status_logger || initialize_status_logger
    end

    def status_logger=(new_status_logger)
      @status_logger = new_status_logger ? new_status_logger : nil
    end

    def say_status(status, message, log_status = true)
      return if status_logger.nil?

      if defined?(Thor) && status_logger.is_a?(Thor)
        MUTEX.synchronize do
          status_logger.say_status status, message, log_status
        end
      end
    end

    def log(message, level = :debug)
      # We're not in verbose mode so disable all non-info logs
      return if !MultiSync.verbose && level != :info
      MUTEX.synchronize do
        logger.send(level, message)
      end
    end

    # Create methods for the different shorthand log methods
    [:info, :debug, :warn, :error].each do |log_method|
      # Shorthand log method
      define_method log_method do |message|
        send(:log, message, log_method)
      end
    end

    private

    def initialize_logger
      @logger = ::Logger.new(STDOUT)
    end

    def initialize_status_logger
      @status_logger = nil
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
multi_sync-0.0.2 lib/multi_sync/logging.rb