Sha256: d5c0da03a756920e447f7afbac3ab9f41eacacaddce7a948e4855df9a1d0f293

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'logger'
require 'logglier'
require 'java'

# Avoid OpenSSL failures
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

# Logger extensions
class Logger

  attr_accessor :config

  def self.sound_alarm beeps = 3
    beeps.times do
      java.awt.Toolkit.getDefaultToolkit.beep
      sleep 0.5
    end
  end

  def sound_alarm beeps = @config[:beeps]
    self.class.sound_alarm beeps
  end

  alias old_fatal fatal

  def fatal *args
    if @config && @config[:fatal]
      entry = args.first
      case entry
        when Exception
          @config[:fatal].fatal "#{entry.class}: '#{entry.message}' at #{caller.join(', ')}"
        else
          @config[:fatal].info *args
      end
    end
    old_fatal *args
  end

  def configure config, &formatter
    @config = config.dup
    @config[:beeps] ||= 0
    @config[:time_format] ||= @config[:time] || '%M:%S.%N'
    @config[:input] ||= 'b9a22025-b148-4dfe-8355-ce5dba40b6a2' #non-default
    @config[:domain] ||= 'logs.loggly.com'
    unless @config[:ignore]
      @config[:fatal] ||= Logglier.new "https://#{@config[:domain]}/inputs/#{@config[:input]}"
    end

    self.level = @config[:level] || Logger::INFO
    self.formatter = formatter || proc do |level, time, prog, msg|
      "#{time.strftime(@config[:time_format])} #{msg}\n"
    end
  end
end

# Object#raise extensions

alias old_raise raise

# Sound alarm before raising error
def raise error
  ex = error.kind_of?(String) ? RuntimeError.new(error) : error.exception

  if defined?(log) && log.config
    log.sound_alarm
    log.fatal ex
  else
    Logger.sound_alarm
  end
  old_raise ex #, ex.message, caller #ex.backtrace
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
releaseable-0.0.4 lib/releaseable/extensions.rb