Class: SpiderGazelle::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/spider-gazelle/logger.rb

Constant Summary collapse

LEVEL =
{
    debug: 0,
    info: 1,
    warn: 2,
    error: 3,
    fatal: 4
}.freeze
DEFAULT_LEVEL =
1
LEVELS =
LEVEL.keys.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



23
24
25
26
# File 'lib/spider-gazelle/logger.rb', line 23

def initialize
    @thread = ::Libuv::Reactor.default
    @level = DEFAULT_LEVEL
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



9
10
11
# File 'lib/spider-gazelle/logger.rb', line 9

def formatter
  @formatter
end

#levelObject

Returns the value of attribute level.



8
9
10
# File 'lib/spider-gazelle/logger.rb', line 8

def level
  @level
end

#threadObject (readonly)

Returns the value of attribute thread.



8
9
10
# File 'lib/spider-gazelle/logger.rb', line 8

def thread
  @thread
end

Class Method Details

.log(data) ⇒ Object



29
30
31
# File 'lib/spider-gazelle/logger.rb', line 29

def self.log(data)
    STDOUT.write data
end

Instance Method Details

#debug(msg = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/spider-gazelle/logger.rb', line 41

def debug(msg = nil)
    if @level <= 0
        msg = yield if block_given?
        log(:debug, msg)
    end
end

#error(msg = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/spider-gazelle/logger.rb', line 62

def error(msg = nil)
    if @level <= 3
        msg = yield if block_given?
        log(:error, msg)
    end
end

#fatal(msg = nil) ⇒ Object



69
70
71
72
73
74
# File 'lib/spider-gazelle/logger.rb', line 69

def fatal(msg = nil)
    if @level <= 4
        msg = yield if block_given?
        log(:fatal, msg)
    end
end

#info(msg = nil) ⇒ Object



48
49
50
51
52
53
# File 'lib/spider-gazelle/logger.rb', line 48

def info(msg = nil)
    if @level <= 1
        msg = yield if block_given?
        log(:info, msg)
    end
end


87
88
89
90
91
92
93
# File 'lib/spider-gazelle/logger.rb', line 87

def print_error(e, msg = nil, trace = nil)
    message = String.new(msg || e.message)
    message << "\n#{e.message}" if msg
    message << "\n#{e.backtrace.join("\n")}" if e.respond_to?(:backtrace) && e.backtrace
    message << "\nCaller backtrace:\n#{trace.join("\n")}" if trace
    error(message)
end

#verbose(msg = nil) ⇒ Object



76
77
78
79
80
81
# File 'lib/spider-gazelle/logger.rb', line 76

def verbose(msg = nil)
    if @verbose
        msg = yield if block_given?
        STDOUT.write ">> #{msg}\n"
    end
end

#verbose!(enabled = true) ⇒ Object



37
38
39
# File 'lib/spider-gazelle/logger.rb', line 37

def verbose!(enabled = true)
    @verbose = enabled
end

#warn(msg = nil) ⇒ Object



55
56
57
58
59
60
# File 'lib/spider-gazelle/logger.rb', line 55

def warn(msg = nil)
    if @level <= 2
        msg = yield if block_given?
        log(:warn, msg)
    end
end

#write(msg) ⇒ Object



83
84
85
# File 'lib/spider-gazelle/logger.rb', line 83

def write(msg)
    STDOUT.write msg
end