grape_logging 1.7.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a1ffd8bd8e43e3c62b4ea629ff034db37f60f4e5
4
- data.tar.gz: e57a2e7ee7937252d33c921ea97d8c820233889d
3
+ metadata.gz: cb31f81ee7d41a9964530d7cdadae6e24b2c1005
4
+ data.tar.gz: 7c61c14042a4c77da0b56c0e8f2922af7a0b48d0
5
5
  SHA512:
6
- metadata.gz: 79619111ac1fe3148fb631770e88da6e3b74f06447f15969194776dc7926107e3b32d1ae9feaa1c61b62e406771e9aece3c6a7d17e83d0a4c21f0d29e666318d
7
- data.tar.gz: 8d3777d6d64f1450de33795f6aebaddce650afb7d5d52fc2907a1090a268a38d38ba5c826417bd252289a9fc7b35be29a685500b6d6b252a857bd31778130529
6
+ metadata.gz: 1ed98f125bcb8df6908407bf820add0566c9e980e2e89d48c7c4c29378174edf5eae51d18e56d27fa90937cf7b8442306ff5cd2aa0a4d7f04fd70e2611b92329
7
+ data.tar.gz: 9d5091d60125bc91edfd4dca9b28a0a8ff83807d695b1987b9f564e784ba1a7f709f994563dd2d0d7a260fcebb5033a6a1a1f4cb0c4be27c1958070c3fda0f33
data/README.md CHANGED
@@ -33,11 +33,83 @@ use GrapeLogging::Middleware::RequestLogger, { logger: logger }
33
33
 
34
34
  ### Log Format
35
35
 
36
- With the default configuration you will get nice log message
36
+ There are formatters provided for you, or you can provide your own.
37
+
38
+ #### `GrapeLogging::Formatters::Default`
39
+
40
+ [2015-04-16 12:52:12 +0200] INFO -- 200 -- total=2.06 db=0.36 -- PATCH /api/endpoint params={"some_param"=>{"value_1"=>"123", "value_2"=>"456"}}
41
+
42
+ #### `GrapeLogging::Formatters::Json`
43
+
44
+ ```json
45
+ {
46
+ "date": "2015-04-16 12:52:12+0200",
47
+ "severity": "INFO",
48
+ "data": {
49
+ "status": 200,
50
+ "time": {
51
+ "total": 2.06,
52
+ "db": 0.36,
53
+ "view": 1.70
54
+ },
55
+ "method": "PATCH",
56
+ "path": "/api/endpoint",
57
+ "params": {
58
+ "value_1": "123",
59
+ "value_2": "456"
60
+ },
61
+ "host": "localhost"
62
+ }
63
+ }
64
+ ```
65
+
66
+ #### `GrapeLogging::Formatters::Lograge`
67
+
68
+ severity="INFO", duration=2.06, db=0.36, view=1.70, datetime="2015-04-16 12:52:12+0200", status=200, method="PATCH", path="/api/endpoint", params={}, host="localhost"
69
+
70
+ #### `GrapeLogging::Formatters::Logstash`
71
+
72
+ ```json
73
+ {
74
+ "@timestamp": "2015-04-16 12:52:12+0200",
75
+ "severity": "INFO",
76
+ "status": 200,
77
+ "time": {
78
+ "total": 2.06,
79
+ "db": 0.36,
80
+ "view": 1.70
81
+ },
82
+ "method": "PATCH",
83
+ "path": "/api/endpoint",
84
+ "params": {
85
+ "value_1": "123",
86
+ "value_2": "456"
87
+ },
88
+ "host": "localhost"
89
+ }
90
+ ```
37
91
 
38
- [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"}}
92
+ #### `GrapeLogging::Formatters::Rails`
39
93
 
40
- If you prefer some other format I strongly encourage you to do pull request with new formatter class ;)
94
+ Rails will print the "Started..." line:
95
+
96
+ Started GET "/api/endpoint" for ::1 at 2015-04-16 12:52:12 +0200
97
+ User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1
98
+ ...
99
+
100
+ The `Rails` formatter adds the last line of the request, like a standard Rails request:
101
+
102
+ Completed 200 OK in 349ms (Views: 250.1ms | DB: 98.63ms)
103
+
104
+ #### Custom
105
+
106
+ You can provide your own class that implements the `call` method returning a `String`:
107
+
108
+ ```ruby
109
+ def call(severity, datetime, _, data)
110
+ ...
111
+ end
112
+ ```
41
113
 
42
114
  You can change the formatter like so
43
115
  ```ruby
@@ -46,6 +118,8 @@ class MyAPI < Grape::API
46
118
  end
47
119
  ```
48
120
 
121
+ If you prefer some other format I strongly encourage you to do pull request with new formatter class ;)
122
+
49
123
  ### Customising What Is Logged
50
124
 
51
125
  You can include logging of other parts of the request / response cycle by including subclasses of `GrapeLogging::Loggers::Base`
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
 
22
22
  spec.add_dependency 'grape'
23
+ spec.add_dependency 'rack'
23
24
 
24
25
  spec.add_development_dependency 'bundler', '~> 1.8'
25
26
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -4,6 +4,7 @@ require 'grape_logging/formatters/default'
4
4
  require 'grape_logging/formatters/json'
5
5
  require 'grape_logging/formatters/lograge'
6
6
  require 'grape_logging/formatters/logstash'
7
+ require 'grape_logging/formatters/rails'
7
8
  require 'grape_logging/loggers/base'
8
9
  require 'grape_logging/loggers/response'
9
10
  require 'grape_logging/loggers/filter_parameters'
@@ -0,0 +1,59 @@
1
+ require 'rack/utils'
2
+
3
+ module GrapeLogging
4
+ module Formatters
5
+ class Rails
6
+
7
+ def call(severity, datetime, _, data)
8
+ if data.is_a?(String)
9
+ "#{severity[0..0]} [#{datetime}] #{severity} -- : #{data}\n"
10
+ elsif data.is_a?(Exception)
11
+ "#{severity[0..0]} [#{datetime}] #{severity} -- : #{format_exception(data)}\n"
12
+ elsif data.is_a?(Hash)
13
+ format_hash(data)
14
+ else
15
+ "#{data.inspect}\n"
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def format_exception(exception)
22
+ backtrace_array = (exception.backtrace || []).map { |line| "\t#{line}" }
23
+
24
+ [
25
+ "#{exception.message} (#{exception.class})",
26
+ backtrace_array.join("\n")
27
+ ].reject{|line| line == ""}.join("\n")
28
+ end
29
+
30
+ def format_hash(hash)
31
+ # Create Rails' single summary line at the end of every request, formatted like:
32
+ # Completed 200 OK in 958ms (Views: 951.1ms | ActiveRecord: 3.8ms)
33
+ # See: actionpack/lib/action_controller/log_subscriber.rb
34
+
35
+ message = ""
36
+ additions = []
37
+ status = hash.delete(:status)
38
+ params = hash.delete(:params)
39
+
40
+ total_time = hash[:time] && hash[:time][:total] && hash[:time][:total].round(2)
41
+ view_time = hash[:time] && hash[:time][:view] && hash[:time][:view].round(2)
42
+ db_time = hash[:time] && hash[:time][:db] && hash[:time][:db].round(2)
43
+
44
+ additions << "Views: #{view_time}ms" if view_time
45
+ additions << "DB: #{db_time}ms" if db_time
46
+
47
+ message << " Parameters: #{params.inspect}\n" if params
48
+
49
+ message << "Completed #{status} #{::Rack::Utils::HTTP_STATUS_CODES[status]} in #{total_time}ms"
50
+ message << " (#{additions.join(" | ".freeze)})" if additions.size > 0
51
+ message << "\n"
52
+ message << "\n" if defined?(::Rails.env) && ::Rails.env.development?
53
+
54
+ message
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -4,7 +4,7 @@ module GrapeLogging
4
4
  AD_PARAMS = 'action_dispatch.request.parameters'.freeze
5
5
 
6
6
  def initialize(filter_parameters = nil, replacement = nil, exceptions = %w(controller action format))
7
- @filter_parameters = filter_parameters || (defined?(Rails.application) ? Rails.application.config.filter_parameters : [])
7
+ @filter_parameters = filter_parameters || (defined?(::Rails.application) ? ::Rails.application.config.filter_parameters : [])
8
8
  @replacement = replacement || '[FILTERED]'
9
9
  @exceptions = exceptions
10
10
  end
@@ -17,7 +17,6 @@ module GrapeLogging
17
17
  { headers: headers }
18
18
  end
19
19
 
20
-
21
20
  end
22
21
  end
23
22
  end
@@ -1,4 +1,4 @@
1
- if defined?(Rails.application)
1
+ if defined?(::Rails.application)
2
2
  class ParameterFilter < ActionDispatch::Http::ParameterFilter
3
3
  def initialize(_replacement, filter_parameters)
4
4
  super(filter_parameters)
@@ -1,3 +1,3 @@
1
1
  module GrapeLogging
2
- VERSION = '1.7.0'
2
+ VERSION = '1.8.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_logging
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aserafin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-10 00:00:00.000000000 Z
11
+ date: 2018-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +117,7 @@ files:
103
117
  - lib/grape_logging/formatters/json.rb
104
118
  - lib/grape_logging/formatters/lograge.rb
105
119
  - lib/grape_logging/formatters/logstash.rb
120
+ - lib/grape_logging/formatters/rails.rb
106
121
  - lib/grape_logging/loggers/base.rb
107
122
  - lib/grape_logging/loggers/client_env.rb
108
123
  - lib/grape_logging/loggers/filter_parameters.rb