grape_logging 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/grape_logging.gemspec +26 -0
- data/lib/grape_logging/formatters/default.rb +31 -0
- data/lib/grape_logging/middleware/request_logger.rb +46 -0
- data/lib/grape_logging/multi_io.rb +15 -0
- data/lib/grape_logging/version.rb +3 -0
- data/lib/grape_logging.rb +4 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e39d824c49d16aa7b96e3dc5886a6808ffc484e7
|
4
|
+
data.tar.gz: 143b23df9350b9ef6e15491fb7e14aaf79efc524
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 07b2e707e3e1bc0ba946a15be38004ff39e2effd482084cc72f302279b6aa9d26b9009a0f385a0735a8347eb73c84956198d8aa34677d7e61b924fcf6f70d3c4
|
7
|
+
data.tar.gz: 4c4fa7aa76460e1a7abdbb553d5f5111499766ed284b3b703fb4baa9c7f4d6a91f193707fbc5934359feab7c87115e1d2b9d2db3c2b1dfd46de7d89b499b4625
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 aserafin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# grape_logging
|
2
|
+
|
3
|
+
[![Code Climate](https://codeclimate.com/github/aserafin/grape_logging/badges/gpa.svg)](https://codeclimate.com/github/aserafin/grape_logging)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'grape_logging'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install grape_logging
|
18
|
+
|
19
|
+
## Basic Usage
|
20
|
+
|
21
|
+
In your api file (somewhere on the top)
|
22
|
+
|
23
|
+
logger.formatter = GrapeLogging::Formatters::Default.new
|
24
|
+
use GrapeLogging::Middleware::RequestLogger, { logger: logger }
|
25
|
+
|
26
|
+
|
27
|
+
## Features
|
28
|
+
|
29
|
+
### Log Format
|
30
|
+
|
31
|
+
With the default configuration you will get nice log message
|
32
|
+
|
33
|
+
[2015-04-16 12:52:12 +0200] INFO -- 200 -- total=2.06 db=0.36 -- PATCH /your_app/endpoint params={"some_param"=>{"value_1"=>"123", "value_2"=>"456"}}
|
34
|
+
|
35
|
+
If you prefer some other format I strongly encourage you to do pull request with new formatter class ;)
|
36
|
+
|
37
|
+
### Logging to file and STDOUT
|
38
|
+
|
39
|
+
You can to file and STDOUT at the same time, you just need to assign new logger
|
40
|
+
|
41
|
+
logger Logger.new GrapeLogging::MultiIO.new(STDOUT, File.open('path/to/your/logfile.log'), 'a'))
|
42
|
+
|
43
|
+
### Logging exceptions
|
44
|
+
|
45
|
+
If you want to log exceptions you can do it like this
|
46
|
+
|
47
|
+
class MyAPI < Grape::API
|
48
|
+
rescue_from :all do |e|
|
49
|
+
MyAPI.logger.error e
|
50
|
+
#do here whatever you originally planned to do :)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
## Development
|
55
|
+
|
56
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
57
|
+
|
58
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it ( https://github.com/aserafin/grape_logging/fork )
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'grape_logging'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grape_logging/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'grape_logging'
|
8
|
+
spec.version = GrapeLogging::VERSION
|
9
|
+
spec.authors = ['aserafin']
|
10
|
+
spec.email = ['adrian@softmad.pl']
|
11
|
+
|
12
|
+
spec.summary = %q{Out of the box request logging for Grape!}
|
13
|
+
spec.description = %q{This gem provides simple request logging for Grape with just 2 lines of code you have to put in your project! In return you will get response codes, parameters, total response duration and time spent in db (if you are using ActiveRecord.)}
|
14
|
+
spec.homepage = 'http://github.com/aserafin/grape_logging'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'grape'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.8'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module GrapeLogging
|
2
|
+
module Formatters
|
3
|
+
class Default
|
4
|
+
def call(severity, datetime, _, data)
|
5
|
+
"[#{datetime}] #{severity} -- #{format(data)}\n"
|
6
|
+
end
|
7
|
+
|
8
|
+
def format(data)
|
9
|
+
if data.is_a?(String)
|
10
|
+
data
|
11
|
+
elsif data.is_a?(Exception)
|
12
|
+
format_exception(data)
|
13
|
+
elsif data.is_a?(Hash)
|
14
|
+
"#{data.delete(:status)} -- total=#{data.delete(:total)} db=#{data.delete(:db)} -- #{data.delete(:method)} #{data.delete(:path)} #{format_hash(data)}"
|
15
|
+
else
|
16
|
+
data.inspect
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
def format_hash(hash)
|
22
|
+
hash.keys.sort.map { |key| "#{key}=#{hash[key]}" }.join(' ')
|
23
|
+
end
|
24
|
+
|
25
|
+
def format_exception(exception)
|
26
|
+
backtrace_array = (exception.backtrace || []).map { |line| "\t#{line}" }
|
27
|
+
"#{exception.message}\n#{backtrace_array.join("\n")}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'grape/middleware/base'
|
2
|
+
|
3
|
+
module GrapeLogging
|
4
|
+
module Middleware
|
5
|
+
class RequestLogger < Grape::Middleware::Base
|
6
|
+
def before
|
7
|
+
env[:db_duration] = 0
|
8
|
+
|
9
|
+
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
|
10
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
11
|
+
env[:db_duration] += event.duration
|
12
|
+
end if defined?(ActiveRecord)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call!(env)
|
16
|
+
original_response = nil
|
17
|
+
|
18
|
+
duration = Benchmark.realtime { original_response = super(env) }
|
19
|
+
logger.info parameters(original_response, duration)
|
20
|
+
|
21
|
+
original_response
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
def parameters(response, duration)
|
26
|
+
{
|
27
|
+
path: request.path,
|
28
|
+
params: request.params,
|
29
|
+
method: request.request_method,
|
30
|
+
total: (duration * 1000).round(2),
|
31
|
+
db: request.env[:db_duration].round(2),
|
32
|
+
status: response.first
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def logger
|
38
|
+
@logger ||= @options[:logger] || Logger.new(STDOUT)
|
39
|
+
end
|
40
|
+
|
41
|
+
def request
|
42
|
+
@request ||= ::Rack::Request.new(env)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape_logging
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aserafin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: grape
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: This gem provides simple request logging for Grape with just 2 lines
|
56
|
+
of code you have to put in your project! In return you will get response codes,
|
57
|
+
parameters, total response duration and time spent in db (if you are using ActiveRecord.)
|
58
|
+
email:
|
59
|
+
- adrian@softmad.pl
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- grape_logging.gemspec
|
72
|
+
- lib/grape_logging.rb
|
73
|
+
- lib/grape_logging/formatters/default.rb
|
74
|
+
- lib/grape_logging/middleware/request_logger.rb
|
75
|
+
- lib/grape_logging/multi_io.rb
|
76
|
+
- lib/grape_logging/version.rb
|
77
|
+
homepage: http://github.com/aserafin/grape_logging
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Out of the box request logging for Grape!
|
101
|
+
test_files: []
|