forked from psylone/simpler
-
Notifications
You must be signed in to change notification settings - Fork 0
add logger #1
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
Franerial
wants to merge
2
commits into
master
Choose a base branch
from
dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add logger #1
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Ignore all logfiles | ||
| /log/* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,21 @@ | ||
| class TestsController < Simpler::Controller | ||
| def index | ||
| @time = Time.now | ||
| render "tests/index" | ||
| end | ||
|
|
||
| def create | ||
| status 201 | ||
| render plain: "Create method in action!" | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. тут ведь ни чего не создаётся |
||
|
|
||
| def plain | ||
| render plain: "plain_option_example" | ||
| status 201 | ||
| render plain: "Plain text responce example" | ||
| status 200 | ||
| end | ||
|
|
||
| def show | ||
| @id = params[:id] | ||
| render plain: "This is an object (show tests method) with id #{@id}" | ||
| end | ||
|
|
||
| def question | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Index | Simpler application</title> | ||
| </head> | ||
| <body> | ||
| <h1> This is the test object (show tests method) with id <%= @id %> </h1> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| require_relative 'config/environment' | ||
| require_relative "config/environment" | ||
| require_relative "lib/simpler/middleware/logger" | ||
|
|
||
| use Simpler::AppLogger, logdev: File.expand_path("log/app.log", Simpler.root) | ||
| run Simpler.application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require "logger" | ||
|
|
||
| module Simpler | ||
| class AppLogger | ||
| def initialize(app, **options) | ||
| @logger = Logger.new(options[:logdev] || STDOUT) | ||
| @app = app | ||
| end | ||
|
|
||
| def call(env) | ||
| @status, @header, @body = @app.call(env) | ||
| @request = Rack::Request.new(env) | ||
|
|
||
| @logger.info(create_log) | ||
|
|
||
| Rack::Response.new(@body, @status, @header).finish | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_log | ||
| if @status == 404 | ||
| %Q( | ||
| Request: #{@request.request_method} #{@request.fullpath} | ||
| Handler: Nil | ||
| Parameters: #{@request.params} | ||
| Response: #{full_status} | ||
| ).delete!("\n") | ||
| else | ||
| %Q( | ||
| Request: #{@request.request_method} #{@request.fullpath} | ||
| Handler: #{controller}##{action} | ||
| Parameters: #{@request.params} | ||
| Response: #{full_status} \[#{@header["Content-Type"]}\] #{view} | ||
| ).delete!("\n") | ||
| end | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. так как лог файл может быть беконечно большим, то лог каждого запроса лучше писать в 1 строчку |
||
|
|
||
| def controller | ||
| @request.env["simpler.controller"].class.name | ||
| end | ||
|
|
||
| def action | ||
| @request.env["simpler.action"] | ||
| end | ||
|
|
||
| def full_status | ||
| "#{@status}" + " #{Rack::Utils::HTTP_STATUS_CODES[@status]}" | ||
| end | ||
|
|
||
| def view | ||
| "#{@request.env["simpler.template"] || [controller, action].join("/")}.html.erb" unless @request.env["simpler.text_plain"] | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,8 +16,13 @@ def initialize(method, path, controller, action) | |
| end | ||
|
|
||
| def match?(method, path) | ||
| define_params(path) | ||
| edited_path = define_edited_path(path) || path | ||
| @params = define_params(path) | ||
|
|
||
| edited_path = if @params.values.all? && ([email protected]?) | ||
| define_edited_path(path) | ||
| else | ||
| path | ||
| end | ||
|
|
||
| @method == method && edited_path == @path | ||
| end | ||
|
|
@@ -31,16 +36,15 @@ def define_params(path) | |
| path.scan(ROUTE_PARAM_VALUE_REGEXP).each { |param| route_params_values << (param.delete "/") } | ||
| @path.scan(ROUTE_PARAM_NAME_REGEXP).each { |param| route_params_names << param.delete(":").delete("/").to_sym } | ||
|
|
||
| @params = Hash[route_params_names.zip route_params_values] | ||
| Hash[route_params_names.zip route_params_values] | ||
| end | ||
|
|
||
| def define_edited_path(path) | ||
| str = path | ||
| @params.each do |param_name, param_value| | ||
| str = str.gsub param_value, ":#{param_name.to_s}" | ||
| end | ||
| edited_path = path | ||
|
|
||
| @params.each { |param_name, param_value| edited_path = edited_path.gsub param_value, ":#{param_name.to_s}" } | ||
|
|
||
| str | ||
| edited_path | ||
| end | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| I, [2021-07-20T21:29:48.157677 #288091] INFO -- : Request: GET /tests/2 Handler: TestsController#show Parameters: {:id=>"2"} Response: 200 OK [text/html] TestsController/show.html.erb | ||
| I, [2021-07-20T21:30:01.670387 #288091] INFO -- : Request: GET /tests/6 Handler: Nil Parameters: {} Response: 404 Not Found | ||
| I, [2021-07-20T21:30:14.369278 #288091] INFO -- : Request: GET /tests/3/question/25?one=1 Handler: Nil Parameters: {"one"=>"1"} Response: 404 Not Found | ||
| I, [2021-07-20T21:30:24.195654 #288091] INFO -- : Request: GET /tests Handler: TestsController#index Parameters: {} Response: 200 OK [text/html] tests/index.html.erb | ||
| I, [2021-07-20T21:32:27.196093 #288333] INFO -- : Request: POST /tests Handler: TestsController#create Parameters: {} Response: 201 Created [text/plain] | ||
| I, [2021-07-20T21:35:00.585388 #288333] INFO -- : Request: POST /tests Handler: TestsController#create Parameters: {} Response: 201 Created [text/plain] | ||
| I, [2021-07-20T21:35:02.896432 #288333] INFO -- : Request: GET /tests Handler: TestsController#index Parameters: {} Response: 200 OK [text/html] tests/index.html.erb | ||
| I, [2021-07-20T21:35:07.109826 #288333] INFO -- : Request: GET /tests/3/question/25?one=1 Handler: Nil Parameters: {"one"=>"1"} Response: 404 Not Found | ||
| I, [2021-07-20T21:35:10.289813 #288333] INFO -- : Request: GET /tests/6 Handler: Nil Parameters: {} Response: 404 Not Found | ||
| I, [2021-07-20T21:35:13.920833 #288333] INFO -- : Request: GET /tests/2 Handler: TestsController#show Parameters: {:id=>"2"} Response: 200 OK [text/html] TestsController/show.html.erb | ||
| I, [2021-07-20T21:35:20.181146 #288333] INFO -- : Request: GET /tests/plain Handler: TestsController#plain Parameters: {} Response: 200 OK [text/plain] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
согласно REST данный экшен должен отдавать список тестов, а не время