Sha256: 343c3e544b91be7449d3c97dfcf57474457d48dd34364095ced8b06b468a03b9

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 KB

Contents

require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/module/aliasing'
require 'logger'

module CrashLog
  module Logging
    ANSI = {
      :red    => 31,
      :green  => 32,
      :yellow => 33,
      :cyan   => 36
    }

    def self.included(base)
      base.__send__(:include, ClassMethods)
    end

    module ClassMethods
      delegate :logger, :to => CrashLog

      [:fatal, :error, :warn, :info, :debug].each do |level|
        define_method(level) do |*args|
          message, options = *args
          message.chomp.split("\n").each do |line|
            logger.send(level, prefix(line))
          end
        end
      end

      def prefix(string)
        [CrashLog::LOG_PREFIX, string].join(' ')
      end

      def colorize(color, text)
        "\e[#{ANSI[color]}m#{text}\e[0m"
      end

      def log_exception(exception)
        logger.error("#{exception.class.name}: #{exception.message}")
        exception.backtrace.each { |line| logger.error(line) } if exception.backtrace
      rescue Exception => e
        puts '--- FATAL ---'
        puts 'an exception occured while logging an exception'
        puts e.message, e.backtrace
        puts exception.message, exception.backtrace
      end

    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
crashlog-1.0.3 lib/crash_log/logging.rb
crashlog-1.0.2.1 lib/crash_log/logging.rb
crashlog-1.0.2 lib/crash_log/logging.rb
crashlog-1.0.1 lib/crash_log/logging.rb
crashlog-1.0.0 lib/crash_log/logging.rb
crashlog-1.0.0.rc2 lib/crash_log/logging.rb
crashlog-1.0.0.rc1 lib/crash_log/logging.rb
crashlog-0.0.2 lib/crash_log/logging.rb