Module: Catcher

Defined in:
lib/catcher.rb,
lib/catcher/logger.rb,
lib/catcher/version.rb,
lib/catcher/prefixed_logger.rb

Defined Under Namespace

Modules: Logger Classes: PrefixedLogger

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.block(progname = nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/catcher.rb', line 7

def self.block(progname = nil)
  begin
    yield
  rescue Exception => e
    log_exception e, progname
  end
end

.log_exception(e, progname) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/catcher.rb', line 33

def self.log_exception(e, progname)
  text = progname ? "Exception raised by '#{progname}'\n\t"  : ""
  if e.kind_of? Exception
    text << "[#{e.class}] #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  else
    # This software is UFO ready
    text << "Some not very exceptional object raised o_O #{e.inspect} [#{e.class}]"
  end
  Catcher.logger.error text
end

.loggerObject



44
45
46
# File 'lib/catcher.rb', line 44

def self.logger
  @logger || setup_logger(STDOUT)
end

.logger=(logger) ⇒ Object



48
49
50
# File 'lib/catcher.rb', line 48

def self.logger=(logger)
  @logger = logger
end

.setup_logger(*args) ⇒ Object



52
53
54
55
56
57
# File 'lib/catcher.rb', line 52

def self.setup_logger(*args)
  @logger = ::Logger.new(*args)
  @logger.formatter = ::Logger::Formatter.new 
  @logger.datetime_format = "%Y-%m-%d %H:%M:%S "
  @logger
end

.thread(progname = nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/catcher.rb', line 15

def self.thread(progname = nil)
  Thread.new do
    block progname do
      yield
    end
  end
end

.thread_loop(progname = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/catcher.rb', line 23

def self.thread_loop(progname = nil)
  Thread.new do
    loop do
      block progname do
        yield
      end
    end
  end
end