Skip to content

Added logger #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def index
end

def create

end

def show
@test = Test.find(id: params[:id])
end
end
2 changes: 1 addition & 1 deletion app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

<p><%= @time %></p>
</body>
</html>
</html>
12 changes: 12 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Show | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<p>Test id: <%= @test_id %></p>
</body>
</html>
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative 'config/environment'

use SimplerLogger
run Simpler.application
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative '../middleware/simpler_logger'
require_relative '../lib/simpler'

Simpler.application.bootstrap!
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Simpler.application.routes do
get '/tests', 'tests#index'
post '/tests', 'tests#create'
get '/tests/:id', 'tests#show'
end
Binary file modified db/test_guru.sqlite
Binary file not shown.
1 change: 0 additions & 1 deletion lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ def setup_database
def make_response(controller, action)
controller.make_response(action)
end

end
end
27 changes: 23 additions & 4 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'view'
require 'rack'

module Simpler
class Controller
Expand All @@ -9,6 +10,7 @@ def initialize(env)
@name = extract_name
@request = Rack::Request.new(env)
@response = Rack::Response.new
@route_params = env["simpler.route_params"] || {}
end

def make_response(action)
Expand Down Expand Up @@ -39,16 +41,33 @@ def write_response
end

def render_body
View.new(@request.env).render(binding)
if @request.env["simpler.plain_text"]
@response["Content-Type"] = "text/plain"
@request.env["simpler.plain_text"]
else
View.new(@request.env).render(binding)
end
end

def params
@request.params
@route_params || {}
@request.params.merge(@route_params)
end

def render(template)
@request.env['simpler.template'] = template
def render(template = nil, plain = nil)
if plain
@request.env['simpler.plain_text'] = plain
else
@request.env['simpler.template'] = template
end
end

def status(status_code)
@response.status = status_code
end

def headers
@response
end
end
end
1 change: 0 additions & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ def add_route(method, path, route_point)
def controller_from_string(controller_name)
Object.const_get("#{controller_name.capitalize}Controller")
end

end
end
14 changes: 13 additions & 1 deletion lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ def initialize(method, path, controller, action)
@path = path
@controller = controller
@action = action
@params = {}
@regexp_path = regexp_path
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && path.match(@regexp_path)
end

def add_params(path)
path_params = path.scan(%r{\w+/\d+}).join("/").split("/")
params[:id] = path_params.last.to_i
end

private

def regexp_path
Regexp.new("^#{@path.gsub(/:id/, '\d+')}$")
end
end
end
end
18 changes: 18 additions & 0 deletions log/app.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Request: GET /tests
Handler: NilClass#
Parameters: {}
Response: 200 OK [text/html] N/A
--------------------------------------
Request: GET /tests/101
Handler: NilClass#
Parameters: {}
Response: 200 OK [text/html] N/A
--------------------------------------
Request: GET /tests/101
Handler: NilClass#
Parameters: {}
Response: 200 OK [text/html] N/A
--------------------------------------
Request: GET /favicon.ico
Handler: NilClass#
Parameters: {}
44 changes: 44 additions & 0 deletions middleware/simpler_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'logger'

class SimplerLogger
def initialize(app)
@app = app
@logger = Logger.new('log/app.log')
@logger.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end
end

def call(env)
request = Rack::Request.new(env)
log_request(request)
status, headers, body = @app.call(env)
log_response(status, headers, env)
log_separator
[status, headers, body]
end

private

def log_request(request)
@logger.info "Request: #{request.request_method} #{request.path}"
@logger.info "Handler: #{env_to_handler(request.env)}"
@logger.info "Parameters: #{request.params}"
end

def log_response(status, headers, env)
response_content_type = headers['Content-Type']
template = env['simpler.template'] || 'N/A'
@logger.info "Response: #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]} [#{response_content_type}] #{template}"
end

def env_to_handler(env)
controller = env['simpler.controller']
action = env['simpler.action']
"#{controller.class}##{action}"
end

def log_separator
@logger.info "--------------------------------------"
end
end