/
log_writer.rb
147 lines (121 loc) · 3.63 KB
/
log_writer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true
require_relative 'null_io'
require_relative 'error_logger'
require 'stringio'
require 'io/wait' unless Puma::HAS_NATIVE_IO_WAIT
module Puma
# Handles logging concerns for both standard messages
# (+stdout+) and errors (+stderr+).
class LogWriter
class DefaultFormatter
def call(str)
str
end
end
class PidFormatter
def call(str)
"[#{$$}] #{str}"
end
end
LOG_QUEUE = Queue.new
attr_reader :stdout,
:stderr
attr_accessor :formatter, :custom_logger
# Create a LogWriter that prints to +stdout+ and +stderr+.
def initialize(stdout, stderr)
@formatter = DefaultFormatter.new
@custom_logger = nil
@stdout = stdout
@stderr = stderr
@debug = ENV.key?('PUMA_DEBUG')
@error_logger = ErrorLogger.new(@stderr)
end
DEFAULT = new(STDOUT, STDERR)
# Returns an LogWriter object which writes its status to
# two StringIO objects.
def self.strings
LogWriter.new(StringIO.new, StringIO.new)
end
def self.stdio
LogWriter.new($stdout, $stderr)
end
def self.null
n = NullIO.new
LogWriter.new(n, n)
end
# Write +str+ to +@stdout+
def log(str)
if @custom_logger&.respond_to?(:write)
@custom_logger.write(format(str))
else
internal_write "#{@formatter.call str}\n"
end
end
def write(str)
internal_write @formatter.call(str)
end
def internal_write(str)
LOG_QUEUE << str
while (w_str = LOG_QUEUE.pop(true)) do
begin
@stdout.is_a?(IO) and @stdout.wait_writable(1)
@stdout.write w_str
@stdout.flush unless @stdout.sync
rescue Errno::EPIPE, Errno::EBADF, IOError, Errno::EINVAL
# 'Invalid argument' (Errno::EINVAL) may be raised by flush
end
end
rescue ThreadError
end
private :internal_write
def debug?
@debug
end
def debug(str)
log("% #{str}") if @debug
end
# Write +str+ to +@stderr+
def error(str)
@error_logger.info(text: @formatter.call("ERROR: #{str}"))
exit 1
end
def format(str)
formatter.call(str)
end
# An HTTP connection error has occurred.
# +error+ a connection exception, +req+ the request,
# and +text+ additional info
# @version 5.0.0
def connection_error(error, req, text="HTTP connection error")
@error_logger.info(error: error, req: req, text: text)
end
# An HTTP parse error has occurred.
# +error+ a parsing exception,
# and +req+ the request.
def parse_error(error, req)
@error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
end
# An SSL error has occurred.
# @param error <Puma::MiniSSL::SSLError>
# @param ssl_socket <Puma::MiniSSL::Socket>
def ssl_error(error, ssl_socket)
peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
peercert = ssl_socket.peercert
subject = peercert&.subject
@error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
end
# An unknown error has occurred.
# +error+ an exception object, +req+ the request,
# and +text+ additional info
def unknown_error(error, req=nil, text="Unknown error")
@error_logger.info(error: error, req: req, text: text)
end
# Log occurred error debug dump.
# +error+ an exception object, +req+ the request,
# and +text+ additional info
# @version 5.0.0
def debug_error(error, req=nil, text="")
@error_logger.debug(error: error, req: req, text: text)
end
end
end