class SlackWebhookLogger::Formatter

Attributes

format[W]

Public Instance Methods

call(severity, time, progname, msg) click to toggle source
# File lib/slack_webhook_logger/formatter.rb, line 34
def call(severity, time, progname, msg)
  format.call(severity, time, progname, msg)
end
format() click to toggle source
# File lib/slack_webhook_logger/formatter.rb, line 7
    def format
      @format ||= proc do |severity, time, _progname, msg|
        heading = case severity
                  when 'FATAL'
                    "📛 *#{severity}*"
                  when 'ERROR'
                    "🛑 *#{severity}*"
                  when 'WARN'
                    "âš ī¸ *#{severity}*"
                  when 'INFO'
                    "â„šī¸ *#{severity}*"
                  when 'DEBUG'
                    "🐛 *#{severity}*"
                  else
                    'đŸĒĩ *Logger*'
                  end

        title = "#{heading} (#{time}) [#{ENV['RAILS_ENV']}]"

        text = <<~MSG
          #{msg2str(msg)}
        MSG

        slackify(title, text)
      end
    end
slackify(title, text) click to toggle source
# File lib/slack_webhook_logger/formatter.rb, line 38
def slackify(title, text)
  {
    text: [title, text].join("\n").to_s,
    blocks: [
      {
        type: 'section',
        text: {
          type: 'mrkdwn',
          text: title
        }
      },
      {
        type: 'divider'
      },
      {
        type: 'section',
        text: {
          type: 'plain_text',
          text: text
        }
      }
    ]
  }
end

Private Instance Methods

msg2str(msg) click to toggle source
# File lib/slack_webhook_logger/formatter.rb, line 63
        def msg2str(msg)
  case msg
  when ::String
    msg
  when ::Exception
    "#{msg.message} (#{msg.class})\n" <<
    (msg.backtrace || []).join("\n")
  else
    msg.inspect
  end
end