Sha256: fe86ff52452e9e8084ef2c10718e3484095eb485784b1e43cce3af653e34c18f

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require 'vedeu/configuration/configuration'

module Vedeu

  # Provides the ability to log anything to the Vedeu log file.
  #
  # @api private
  class Log

    class << self

      # Write a message to the Vedeu log file.
      #
      # @param message [String] The message you wish to emit to the log
      #   file, useful for debugging.
      # @param force   [Boolean] When evaluates to true will attempt to
      #   write to the log file regardless of the Configuration setting.
      #
      # @example
      #   Vedeu.log('A useful debugging message: Error!')
      #
      # @return [TrueClass]
      def log(message, force = false)
        colour = [:red, :green, :yellow, :blue, :magenta, :cyan, :white].sample

        logger.debug(Esc.send(colour) { message }) if enabled? || force
      end

      # @return [TrueClass]
      def logger
        Logger.new(log_file).tap do |log|
          log.formatter = proc do |_, time, _, message|
            formatted_message(message, time)
          end
        end
      end

      private

      # @return [String]
      def formatted_message(message, time = Time.now)
        [timestamp(time.utc.iso8601), message, "\n"].join
      end

      # @return [Boolean]
      def enabled?
        Vedeu::Configuration.debug?
      end

      # @return [String]
      def log_file
        Vedeu::Configuration.log
      end

      # Returns a formatted (red, underlined) UTC timestamp,
      # eg. 2014-10-24T12:34:56Z
      #
      # @return [String]
      def timestamp(utc_time)
        return '' if @last_seen == utc_time

        @last_seen = utc_time

        "\n\e[4m\e[31m" + utc_time + "\e[39m\e[24m\n"
      end

    end # Log eigenclass

  end # Log

end # Vedeu

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vedeu-0.3.4 lib/vedeu/support/log.rb
vedeu-0.3.3 lib/vedeu/support/log.rb
vedeu-0.3.2 lib/vedeu/support/log.rb
vedeu-0.3.1 lib/vedeu/support/log.rb
vedeu-0.3.0 lib/vedeu/support/log.rb