Class: Telegram::LoggerBot::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/telegram/loggerbot/logger.rb

Constant Summary collapse

SEV_ICON =
[
    "\xF0\x9F\x93\x98",
    "\xF0\x9F\x93\x94",
    "\xF0\x9F\x93\x99",
    "\xF0\x9F\x93\x95",
    "\xF0\x9F\x93\x9A",
    "\xF0\x9F\x93\x93"
].each(&:freeze).freeze
TIME_ICON_FIRST_HALF =
[
    "\xF0\x9F\x95\x9B",
    "\xF0\x9F\x95\x90",
    "\xF0\x9F\x95\x91",
    "\xF0\x9F\x95\x92",
    "\xF0\x9F\x95\x93",
    "\xF0\x9F\x95\x94",
    "\xF0\x9F\x95\x95",
    "\xF0\x9F\x95\x96",
    "\xF0\x9F\x95\x97",
    "\xF0\x9F\x95\x98",
    "\xF0\x9F\x95\x99",
    "\xF0\x9F\x95\x9A"
]
TIME_ICON_SECOND_HALF =
[
    "\xF0\x9F\x95\xA7",
    "\xF0\x9F\x95\x9C",
    "\xF0\x9F\x95\x9D",
    "\xF0\x9F\x95\x9E",
    "\xF0\x9F\x95\x9F",
    "\xF0\x9F\x95\xA0",
    "\xF0\x9F\x95\xA1",
    "\xF0\x9F\x95\xA2",
    "\xF0\x9F\x95\xA3",
    "\xF0\x9F\x95\xA4",
    "\xF0\x9F\x95\xA5",
    "\xF0\x9F\x95\xA6"
]

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Logger

Returns a new instance of Logger.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/telegram/loggerbot/logger.rb', line 7

def initialize(args = {})
  @default_formatter = args[:default_formatter] || Formatter.new

  @level = args[:level] || Telegram::LoggerBot.configuration.level || DEBUG
  @chat_id = args[:chat_id] || Telegram::LoggerBot.configuration.chat_id || fail(ChatIdMissed)
  @token = args[:token] || Telegram::LoggerBot.configuration.token || fail(TokenMissed)
  @next_logger = args[:next_logger] || Telegram::LoggerBot.configuration.next_logger
  @api = args[:api] || Telegram::LoggerBot.configuration.api || Telegram::Bot::Api.new(@token)

  @enabled = args[:enabled] unless args[:enabled].nil?
  @enabled = Telegram::LoggerBot.configuration.enabled unless Telegram::LoggerBot.configuration.enabled.nil?
  @enabled = true if @enabled.nil?
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/telegram/loggerbot/logger.rb', line 79

def add(severity, message = nil, progname = nil, &block)
  severity ||= UNKNOWN

  if severity < @level
    if @next_logger
      return @next_logger.add(severity, message, progname, &block)
    else
      return true
    end
  end

  if message.nil?
    if block_given?
      message = block.dup.call
    else
      message = progname
      progname = @progname
    end
  end

  if @enabled
    time = Time.now

    text = "#{format_severity_icon(severity)}*#{format_severity(severity)}*"
    text << "   _#{clear_markdown(progname)}_" if progname
    text << "\n#{format_time_icon(time)}#{time}"

    @api.send_message(chat_id: @chat_id, text: text, parse_mode: 'Markdown')
    @api.send_message(chat_id: @chat_id, text: message)
  end

  if @next_logger
    @next_logger.add(severity, message, progname, &block)
  else
    true
  end
end

#clear_markdown(str) ⇒ Object



21
22
23
# File 'lib/telegram/loggerbot/logger.rb', line 21

def clear_markdown(str)
  str.gsub(/[^a-zA-Z0-9\<\>\s\n:+-]/, '')
end

#format_severity_icon(severity) ⇒ Object



34
35
36
# File 'lib/telegram/loggerbot/logger.rb', line 34

def format_severity_icon(severity)
  SEV_ICON[severity] || "\xF0\x9F\x93\x93"
end

#format_time_icon(time) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/telegram/loggerbot/logger.rb', line 68

def format_time_icon(time)
  hour12 = time.strftime('%I').to_i
  hour12 = 0 if hour12 == 12
  minute = time.strftime('%M').to_i
  if minute < 30
    TIME_ICON_FIRST_HALF[hour12]
  else
    TIME_ICON_SECOND_HALF[hour12]
  end
end