Class: Puma::LogWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/log_writer.rb

Overview

Handles logging concerns for both standard messages (stdout) and errors (stderr).

Defined Under Namespace

Classes: DefaultFormatter, PidFormatter

Constant Summary collapse

LOG_QUEUE =
Queue.new
DEFAULT =
new(STDOUT, STDERR)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, stderr, env: ENV) ⇒ LogWriter

Create a LogWriter that prints to stdout and stderr.



34
35
36
37
38
39
40
41
42
# File 'lib/puma/log_writer.rb', line 34

def initialize(stdout, stderr, env: ENV)
  @formatter = DefaultFormatter.new
  @custom_logger = nil
  @stdout = stdout
  @stderr = stderr

  @debug = env.key?('PUMA_DEBUG')
  @error_logger = ErrorLogger.new(@stderr, env: env)
end

Instance Attribute Details

#custom_loggerObject

Returns the value of attribute custom_logger.



31
32
33
# File 'lib/puma/log_writer.rb', line 31

def custom_logger
  @custom_logger
end

#formatterObject

Returns the value of attribute formatter.



31
32
33
# File 'lib/puma/log_writer.rb', line 31

def formatter
  @formatter
end

#stderrObject (readonly)

Returns the value of attribute stderr.



28
29
30
# File 'lib/puma/log_writer.rb', line 28

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



28
29
30
# File 'lib/puma/log_writer.rb', line 28

def stdout
  @stdout
end

Class Method Details

.null(env: ENV) ⇒ Object



56
57
58
59
# File 'lib/puma/log_writer.rb', line 56

def self.null(env: ENV)
  n = NullIO.new
  LogWriter.new(n, n, env: env)
end

.stdio(env: ENV) ⇒ Object



52
53
54
# File 'lib/puma/log_writer.rb', line 52

def self.stdio(env: ENV)
  LogWriter.new($stdout, $stderr, env: env)
end

.strings(env: ENV) ⇒ Object

Returns an LogWriter object which writes its status to two StringIO objects.



48
49
50
# File 'lib/puma/log_writer.rb', line 48

def self.strings(env: ENV)
  LogWriter.new(StringIO.new, StringIO.new, env: env)
end

Instance Method Details

#connection_error(error, req, text = "HTTP connection error") ⇒ Object

An HTTP connection error has occurred. error a connection exception, req the request, and text additional info

Version:

  • 5.0.0



111
112
113
# File 'lib/puma/log_writer.rb', line 111

def connection_error(error, req, text="HTTP connection error")
  @error_logger.info(error: error, req: req, text: text)
end

#debug(str) ⇒ Object



93
94
95
# File 'lib/puma/log_writer.rb', line 93

def debug(str)
  log("% #{str}") if @debug
end

#debug?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/puma/log_writer.rb', line 89

def debug?
  @debug
end

#debug_error(error, req = nil, text = "") ⇒ Object

Log occurred error debug dump. error an exception object, req the request, and text additional info

Version:

  • 5.0.0



143
144
145
# File 'lib/puma/log_writer.rb', line 143

def debug_error(error, req=nil, text="")
  @error_logger.debug(error: error, req: req, text: text)
end

#error(str) ⇒ Object

Write str to @stderr



98
99
100
101
# File 'lib/puma/log_writer.rb', line 98

def error(str)
  @error_logger.info(text: @formatter.call("ERROR: #{str}"))
  exit 1
end

#format(str) ⇒ Object



103
104
105
# File 'lib/puma/log_writer.rb', line 103

def format(str)
  formatter.call(str)
end

#log(str) ⇒ Object

Write str to @stdout



62
63
64
65
66
67
68
# File 'lib/puma/log_writer.rb', line 62

def log(str)
  if @custom_logger&.respond_to?(:write)
    @custom_logger.write(format(str))
  else
    internal_write "#{@formatter.call str}\n"
  end
end

#parse_error(error, req) ⇒ Object

An HTTP parse error has occurred. error a parsing exception, and req the request.



118
119
120
# File 'lib/puma/log_writer.rb', line 118

def parse_error(error, req)
  @error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
end

#ssl_error(error, ssl_socket) ⇒ Object

An SSL error has occurred.

Parameters:



125
126
127
128
129
130
# File 'lib/puma/log_writer.rb', line 125

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

#unknown_error(error, req = nil, text = "Unknown error") ⇒ Object

An unknown error has occurred. error an exception object, req the request, and text additional info



135
136
137
# File 'lib/puma/log_writer.rb', line 135

def unknown_error(error, req=nil, text="Unknown error")
  @error_logger.info(error: error, req: req, text: text)
end

#write(str) ⇒ Object



70
71
72
# File 'lib/puma/log_writer.rb', line 70

def write(str)
  internal_write @formatter.call(str)
end