Class: Log2slack::Logger

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(output = STDOUT, max_files = 10, max_size = 1024 * 1024) ⇒ Logger

Initialize a new Logger instance

Parameters:

  • output (IO, String) (defaults to: STDOUT)

    The log output destination (STDOUT, file path, etc.)

  • max_files (Integer) (defaults to: 10)

    Maximum number of log files to keep

  • max_size (Integer) (defaults to: 1024 * 1024)

    Maximum size of each log file in bytes



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)
  @messages = []
  @status = LOG_LEVEL_INFO
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



12
13
14
# File 'lib/log2slack/logger.rb', line 12

def messages
  @messages
end

#statusObject (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

Parameters:

  • message (String)

    The message to log

  • notify (Boolean) (defaults to: false)

    Whether to store the message for Slack notification



64
65
66
# File 'lib/log2slack/logger.rb', line 64

def error(message, notify: false)
  log(message, level: :error, notify: notify)
end

#format_title(title) ⇒ String

Format the title based on the current status

Parameters:

  • title (String)

    The original title

Returns:

  • (String)

    The formatted title



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

Parameters:

  • message (String)

    The message to log

  • notify (Boolean) (defaults to: false)

    Whether to store the message for Slack notification



46
47
48
# File 'lib/log2slack/logger.rb', line 46

def info(message, notify: false)
  log(message, 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

Parameters:

  • message (String)

    The message to log

  • level (Symbol) (defaults to: :info)

    The log level (:info, :warn, :error)

  • notify (Boolean) (defaults to: false)

    Whether to store the message for Slack notification



33
34
35
36
37
38
39
# File 'lib/log2slack/logger.rb', line 33

def log(message, level: :info, notify: false)
  @logger.send(level, message)
  @messages.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

Parameters:

  • title (String)

    The title for the Slack message

Returns:

  • (Hash)

    The attachment payload



72
73
74
75
76
77
78
79
80
81
# File 'lib/log2slack/logger.rb', line 72

def make_attachments(title)
  { attachments: [
    {
      fallback: format_title(title),
      title: format_title(title),
      text: @messages.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

Parameters:

  • webhook_url (String)

    The Slack webhook URL

  • channel (String)

    The Slack channel to post to

  • user_name (String)

    The username to display in Slack

  • title (String)

    The title for the Slack message

Yields:

  • (optional)

    Block to provide custom payload

Raises:



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
           make_attachments(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_colorString

Determine the color based on the current status

Returns:

  • (String)

    The color code for Slack attachment



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

Parameters:

  • message (String)

    The message to log

  • notify (Boolean) (defaults to: false)

    Whether to store the message for Slack notification



55
56
57
# File 'lib/log2slack/logger.rb', line 55

def warn(message, notify: false)
  log(message, level: :warn, notify: notify)
end