Sha256: 695e7344bdae31b5588702399954018dac47ee4f1abe9de738e0535d2fb303f7
Contents?: true
Size: 1.8 KB
Versions: 1
Compression:
Stored size: 1.8 KB
Contents
# frozen_string_literal: true module SlackWebhookLogger class Formatter < ::Logger::Formatter attr_writer :format MAX_LENGTH = 3000 class << self def fetch_text(hash) hash[:blocks].last[:text][:text] end end 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} #{SlackWebhookLogger.application_name} [#{ENV.fetch("RAILS_ENV", nil)}] (#{time})" text = "```#{msg2str(msg)}" slackify(truncate(title), "#{truncate(text)}```") end end def call(severity, time, progname, msg) format.call(severity, time, progname, msg) end def slackify(title, text) { 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 private def truncate(string) # 3 for the ellipsis, 3 for the backticks string.length > MAX_LENGTH ? "#{string[0...MAX_LENGTH - 6]}..." : string end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
slack_webhook_logger-0.6.3 | lib/slack_webhook_logger/formatter.rb |