Class: Telegram::LoggerBot::Logger
- Inherits:
-
Logger
- Object
- Logger
- Telegram::LoggerBot::Logger
- 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
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
- #clear_markdown(str) ⇒ Object
- #format_severity_icon(severity) ⇒ Object
- #format_time_icon(time) ⇒ Object
-
#initialize(args = {}) ⇒ Logger
constructor
A new instance of Logger.
Constructor Details
#initialize(args = {}) ⇒ Logger
Returns a new instance of Logger.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/telegram/loggerbot/logger.rb', line 8 def initialize(args = {}) args = args.dup @default_formatter = args.delete(:default_formatter) || Formatter.new @level = args.delete(:level) || Telegram::LoggerBot.configuration.level || DEBUG @chat_id = args.delete(:chat_id) || Telegram::LoggerBot.configuration.chat_id || fail(ChatIdMissed) @token = args.delete(:token) || Telegram::LoggerBot.configuration.token || fail(TokenMissed) @next_logger = args.delete(:next_logger) || Telegram::LoggerBot.configuration.next_logger @api = args.delete(:api) || Telegram::LoggerBot.configuration.api || Telegram::Bot::Api.new(@token) @enabled = if args.key?(:enabled) value = args.delete(:enabled) case value when true, false value else true end elsif not Telegram::LoggerBot.configuration.enabled.nil? !!Telegram::LoggerBot.configuration.enabled else true end fail(UnknownParams, args) if args.keys.size > 0 end |
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/telegram/loggerbot/logger.rb', line 94 def add(severity, = nil, progname = nil, &block) severity ||= UNKNOWN if severity < @level if @next_logger return @next_logger.add(severity, , progname, &block) else return true end end if .nil? if block_given? = block.dup.call else = 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.(chat_id: @chat_id, text: text, parse_mode: 'Markdown') @api.(chat_id: @chat_id, text: ) end if @next_logger @next_logger.add(severity, , progname, &block) else true end end |
#clear_markdown(str) ⇒ Object
36 37 38 |
# File 'lib/telegram/loggerbot/logger.rb', line 36 def clear_markdown(str) str.gsub(/[^a-zA-Z0-9\<\>\s\n:+-]/, '') end |
#format_severity_icon(severity) ⇒ Object
49 50 51 |
# File 'lib/telegram/loggerbot/logger.rb', line 49 def format_severity_icon(severity) SEV_ICON[severity] || "\xF0\x9F\x93\x93" end |
#format_time_icon(time) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/telegram/loggerbot/logger.rb', line 83 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 |