Skip to content

Commit 6ec0519

Browse files
committed
Add Rack::Logger.
1 parent d5265bc commit 6ec0519

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/rack/contrib.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def self.release
3030
autoload :LazyConditionalGet, "rack/contrib/lazy_conditional_get"
3131
autoload :LighttpdScriptNameFix, "rack/contrib/lighttpd_script_name_fix"
3232
autoload :Locale, "rack/contrib/locale"
33+
autoload :Logger, "rack/contrib/logger"
3334
autoload :MailExceptions, "rack/contrib/mailexceptions"
3435
autoload :PostBodyContentTypeParser, "rack/contrib/post_body_content_type_parser"
3536
autoload :ProcTitle, "rack/contrib/proctitle"

lib/rack/contrib/logger.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'logger'
4+
5+
module Rack
6+
# Sets up rack.logger to write to rack.errors stream
7+
class Logger
8+
RACK_ERRORS = 'rack.errors'
9+
RACK_LOGGER = 'rack.logger'
10+
11+
def initialize(app, level = ::Logger::INFO)
12+
@app, @level = app, level
13+
end
14+
15+
def call(env)
16+
logger = ::Logger.new(env[RACK_ERRORS])
17+
logger.level = @level
18+
19+
env[RACK_LOGGER] = logger
20+
@app.call(env)
21+
end
22+
end
23+
end

test/spec_logger.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'minitest/autorun'
4+
require 'rack/contrib/logger'
5+
6+
describe Rack::Logger do
7+
app = lambda { |env|
8+
log = env['rack.logger']
9+
log.debug("Created logger")
10+
log.info("Program started")
11+
log.warn("Nothing to do!")
12+
13+
[200, { 'content-type' => 'text/plain' }, ["Hello, World!"]]
14+
}
15+
16+
it "conform to Rack::Lint" do
17+
errors = StringIO.new
18+
a = Rack::Lint.new(Rack::Logger.new(app))
19+
Rack::MockRequest.new(a).get('/', 'rack.errors' => errors)
20+
errors.string.must_match(/INFO -- : Program started/)
21+
errors.string.must_match(/WARN -- : Nothing to do/)
22+
end
23+
end

0 commit comments

Comments
 (0)