Class: Log2slack::Logger
- Inherits:
-
Object
- Object
- Log2slack::Logger
- Defined in:
- lib/log2slack/logger.rb
Constant Summary collapse
- LOG_LEVEL_INFO =
Log level constants
'INFO'- LOG_LEVEL_WARN =
'WARN'- LOG_LEVEL_ERROR =
'ERROR'
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#error(message, notify: false) ⇒ void
Log a message with ERROR level.
-
#format_title(title) ⇒ String
Format the title based on the current status.
-
#info(message, notify: false) ⇒ void
Log a message with INFO level.
-
#initialize(output = STDOUT, max_files = 10, max_size = 1024 * 1024) ⇒ Logger
constructor
Initialize a new Logger instance.
-
#log(message, level: :info, notify: false) ⇒ void
Log a message with the specified level.
-
#make_attachments(title) ⇒ Hash
Create attachment payload for Slack notification.
-
#notify_to_slack(webhook_url, channel, user_name, title) {|optional| ... } ⇒ void
Send notification to Slack.
-
#status_color ⇒ String
Determine the color based on the current status.
-
#warn(message, notify: false) ⇒ void
Log a message with WARN level.
Constructor Details
#initialize(output = STDOUT, max_files = 10, max_size = 1024 * 1024) ⇒ Logger
Initialize a new Logger instance
20 21 22 23 24 25 |
# File 'lib/log2slack/logger.rb', line 20 def initialize(output = STDOUT, max_files = 10, max_size = 1024 * 1024) # load logger module @logger = ::Logger.new(output, max_files, max_size) = [] @status = LOG_LEVEL_INFO end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
12 13 14 |
# File 'lib/log2slack/logger.rb', line 12 def end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
12 13 14 |
# File 'lib/log2slack/logger.rb', line 12 def status @status end |
Instance Method Details
#error(message, notify: false) ⇒ void
This method returns an undefined value.
Log a message with ERROR level
64 65 66 |
# File 'lib/log2slack/logger.rb', line 64 def error(, notify: false) log(, level: :error, notify: notify) end |
#format_title(title) ⇒ String
Format the title based on the current status
87 88 89 90 91 92 93 |
# File 'lib/log2slack/logger.rb', line 87 def format_title(title) if @status == LOG_LEVEL_ERROR || @status == LOG_LEVEL_WARN "<!channel> #{title}" else title end end |
#info(message, notify: false) ⇒ void
This method returns an undefined value.
Log a message with INFO level
46 47 48 |
# File 'lib/log2slack/logger.rb', line 46 def info(, notify: false) log(, level: :info, notify: notify) end |
#log(message, level: :info, notify: false) ⇒ void
This method returns an undefined value.
Log a message with the specified level
33 34 35 36 37 38 39 |
# File 'lib/log2slack/logger.rb', line 33 def log(, level: :info, notify: false) @logger.send(level, ) .push("[#{level.to_s.upcase}]#{message}") if notify @status = level.to_s.upcase if level == :error # Do not overwrite with warn in case of error @status = level.to_s.upcase if level == :warn && @status != LOG_LEVEL_ERROR end |
#make_attachments(title) ⇒ Hash
Create attachment payload for Slack notification
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/log2slack/logger.rb', line 72 def (title) { attachments: [ { fallback: format_title(title), title: format_title(title), text: .join("\n"), color: status_color } ] } end |
#notify_to_slack(webhook_url, channel, user_name, title) {|optional| ... } ⇒ void
This method returns an undefined value.
Send notification to Slack
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/log2slack/logger.rb', line 118 def notify_to_slack(webhook_url, channel, user_name, title) args = if block_given? yield() else (title) end if @notifier.nil? @notifier = Slack::Notifier.new( webhook_url, channel: channel, username: user_name ) end begin @notifier.post(args) rescue StandardError => e @logger.error("Failed to send notification to Slack: #{e.message}") raise Log2slack::Error, "Slack notification failed: #{e.message}" end end |
#status_color ⇒ String
Determine the color based on the current status
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/log2slack/logger.rb', line 98 def status_color case @status when LOG_LEVEL_ERROR 'danger' when LOG_LEVEL_WARN 'warning' else 'good' end end |
#warn(message, notify: false) ⇒ void
This method returns an undefined value.
Log a message with WARN level
55 56 57 |
# File 'lib/log2slack/logger.rb', line 55 def warn(, notify: false) log(, level: :warn, notify: notify) end |