Class: WeChat::Bot::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/bot/logger.rb

Constant Summary collapse

LEVELS =
[:verbose, :debug, :info, :warn, :error, :fatal]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, bot) ⇒ Logger

Returns a new instance of Logger.



16
17
18
19
20
21
# File 'lib/wechat/bot/logger.rb', line 16

def initialize(output, bot)
  @output = output
  @bot = bot
  @mutex = Mutex.new
  @level = :info
end

Instance Attribute Details

#levelSymbol

Returns:

  • (Symbol)


8
9
10
# File 'lib/wechat/bot/logger.rb', line 8

def level
  @level
end

#mutexMutex (readonly)

Returns:

  • (Mutex)


11
12
13
# File 'lib/wechat/bot/logger.rb', line 11

def mutex
  @mutex
end

#outputIO (readonly)

Returns:

  • (IO)


14
15
16
# File 'lib/wechat/bot/logger.rb', line 14

def output
  @output
end

Instance Method Details

#debug(message) ⇒ Object



27
28
29
# File 'lib/wechat/bot/logger.rb', line 27

def debug(message)
  log(:debug, message)
end

#error(message) ⇒ Object



39
40
41
# File 'lib/wechat/bot/logger.rb', line 39

def error(message)
  log(:error, message)
end

#fatal(exception) ⇒ Object



43
44
45
46
47
# File 'lib/wechat/bot/logger.rb', line 43

def fatal(exception)
  message = ["#{exception.backtrace.first}: #{exception.message} (#{exception.class})"]
  message.concat(exception.backtrace[1..-1].map {|s| "\t" + s})
  log(:fatal, message.join("\n"))
end

#info(message) ⇒ Object



31
32
33
# File 'lib/wechat/bot/logger.rb', line 31

def info(message)
  log(:info, message)
end

#log(level, message) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/wechat/bot/logger.rb', line 49

def log(level, message)
  return unless can_log?(level)
  return if message.to_s.empty?

  @mutex.synchronize do
    message = format_message(format_general(message), level)
    @output.puts message
  end
end

#verbose(message) ⇒ Object



23
24
25
# File 'lib/wechat/bot/logger.rb', line 23

def verbose(message)
  log(:verbose, message)
end

#warn(message) ⇒ Object



35
36
37
# File 'lib/wechat/bot/logger.rb', line 35

def warn(message)
  log(:warn, message)
end