Sha256: 5b3209e9eab89637d9427c0d2e9666df56da349b298c09498248faa39c0a1e7a

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module SlackWebhookLogger
  class Formatter < ::Logger::Formatter
    attr_writer :format

    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

    def call(severity, time, progname, msg)
      format.call(severity, time, progname, msg)
    end

    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 def msg2str(msg)
      case msg
      when ::String
        msg
      when ::Exception
        "#{msg.message} (#{msg.class})\n" <<
        (msg.backtrace || []).join("\n")
      else
        msg.inspect
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slack_webhook_logger-0.3.1 lib/slack_webhook_logger/formatter.rb
slack_webhook_logger-0.3.0 lib/slack_webhook_logger/formatter.rb