-
Notifications
You must be signed in to change notification settings - Fork 1k
/
app_logger.rb
64 lines (51 loc) 路 1.7 KB
/
app_logger.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
require "logger"
module Postal
class AppLogger < Logger
def initialize(log_name, *args)
@log_name = log_name
super(*args)
self.formatter = LogFormatter.new
end
def add(severity, message = nil, progname = nil)
super
if severity >= @level && n = self.class.graylog_notifier
begin
if message.nil?
message = block_given? ? yield : progname
end
message = message.to_s.force_encoding("UTF-8").scrub
message_without_ansi = begin
message.gsub(/\e\[([\d;]+)?m/, "")
rescue StandardError
message
end
n.notify!(short_message: message_without_ansi, log_name: @log_name, facility: "postal", application_name: "postal", process_name: ENV.fetch("PROC_NAME", nil), pid: Process.pid)
rescue StandardError
# Can't log this to GELF. Soz.
end
end
true
end
def self.graylog?
!!Postal.config.logging.graylog&.host
end
def self.graylog_notifier
@graylog_notifier ||= graylog? ? GELF::Notifier.new(Postal.config.logging.graylog.host, Postal.config.logging.graylog.port) : nil
end
end
class LogFormatter
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S.%3N".freeze
COLORS = [32, 34, 35, 31, 32, 33]
def call(severity, datetime, progname, msg)
time = datetime.strftime(TIME_FORMAT)
if number = ENV["PROC_NAME"]
id = number.split(".").last.to_i
proc_text = "\e[#{COLORS[id % COLORS.size]}m[#{ENV['PROC_NAME']}:#{Process.pid}]\e[0m"
else
proc_text = "[#{Process.pid}]"
end
"#{proc_text} [#{time}] #{severity} -- : #{msg}\n"
end
end
end