heavylog 0.0.17 → 0.0.22

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
  SHA256:
3
- metadata.gz: a01b4798a9eccba2129d251ce94305a062a5ba47bef3a37d81674ef730ea0c8a
4
- data.tar.gz: a2e26f6f95305729667ba8bed2e2d739200c18ae972efe40a295e7e15163c174
3
+ metadata.gz: 97686d14f27208a6334ce6c76a20d11562fdbe2affdb5d3794b7baae2620c6c6
4
+ data.tar.gz: fee3b90c91fe906b10bcb7b4b109d90b328a166925b9fe4d82e7459089e6e593
5
5
  SHA512:
6
- metadata.gz: 45c91792eb8cd223555cebe0709501677ff73583bd72b60d88e4b364caa85d796e2c75d93a131745e246eb9509ade848207f74086765f30976b4228ba61a3daa
7
- data.tar.gz: 6a0eb6464ce38bd89294ed7be9d68b9ff2465ce2734e2904a88234ff1bb528b0a1f1e9741908cb5aca7334003ace4f79b06804468107d70804169ad91832c811
6
+ metadata.gz: 4b2949b2eb311aed8c768b8d4f1cf259c843d5efe2a200dfce835da0e87752f736a3a26e2cdb5853a7428178ba524e9af22260b9459de97ba09de95e5e4d0d0e
7
+ data.tar.gz: 8a492bc19d4b02ca5d3fb0ea9fff617cd556eb5a328f6e6cd8f8a573f12630463cb9a66e6c5570d7bf7b2aa3adc91ece833094a777434bea4205d9cbc389dd40
@@ -1,6 +1,6 @@
1
1
  name: Test
2
2
 
3
- on: [push, pull_request]
3
+ on: [push]
4
4
 
5
5
  jobs:
6
6
  test:
@@ -10,9 +10,9 @@ jobs:
10
10
  steps:
11
11
  - uses: actions/checkout@v1
12
12
  - name: Set up Ruby
13
- uses: actions/setup-ruby@v1
13
+ uses: ruby/setup-ruby@v1
14
14
  with:
15
- ruby-version: 2.6.5
15
+ ruby-version: 3.0.1
16
16
 
17
17
  - uses: actions/cache@v1
18
18
  with:
@@ -28,9 +28,6 @@ jobs:
28
28
  bundle install --jobs 4 --retry 3
29
29
 
30
30
  - name: Run tests
31
- uses: paambaati/codeclimate-action@v2.4.0
32
31
  env:
33
32
  CI: true
34
- CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
35
- with:
36
- coverageCommand: bundle exec rspec
33
+ run: bundle exec rspec
data/.rubocop.yml CHANGED
@@ -2,7 +2,7 @@ require:
2
2
  - rubocop-performance
3
3
 
4
4
  AllCops:
5
- TargetRubyVersion: 2.6
5
+ TargetRubyVersion: 3.0
6
6
 
7
7
  # # Commonly used screens these days easily fit more than 80 characters.
8
8
  Layout/LineLength:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- heavylog (0.0.17)
4
+ heavylog (0.0.22)
5
5
  actionpack (>= 5)
6
6
  activesupport (>= 5)
7
7
  railties (>= 5)
@@ -10,55 +10,58 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- actionpack (6.0.2.1)
14
- actionview (= 6.0.2.1)
15
- activesupport (= 6.0.2.1)
16
- rack (~> 2.0, >= 2.0.8)
13
+ actionpack (6.1.3.2)
14
+ actionview (= 6.1.3.2)
15
+ activesupport (= 6.1.3.2)
16
+ rack (~> 2.0, >= 2.0.9)
17
17
  rack-test (>= 0.6.3)
18
18
  rails-dom-testing (~> 2.0)
19
19
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
20
- actionview (6.0.2.1)
21
- activesupport (= 6.0.2.1)
20
+ actionview (6.1.3.2)
21
+ activesupport (= 6.1.3.2)
22
22
  builder (~> 3.1)
23
23
  erubi (~> 1.4)
24
24
  rails-dom-testing (~> 2.0)
25
25
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
26
- activesupport (6.0.2.1)
26
+ activesupport (6.1.3.2)
27
27
  concurrent-ruby (~> 1.0, >= 1.0.2)
28
- i18n (>= 0.7, < 2)
29
- minitest (~> 5.1)
30
- tzinfo (~> 1.1)
31
- zeitwerk (~> 2.2)
32
- ast (2.4.0)
28
+ i18n (>= 1.6, < 2)
29
+ minitest (>= 5.1)
30
+ tzinfo (~> 2.0)
31
+ zeitwerk (~> 2.3)
32
+ ast (2.4.2)
33
33
  backport (1.1.2)
34
- benchmark (0.1.0)
34
+ benchmark (0.1.1)
35
35
  builder (3.2.4)
36
- concurrent-ruby (1.1.6)
37
- connection_pool (2.2.2)
36
+ concurrent-ruby (1.1.8)
37
+ connection_pool (2.2.5)
38
38
  crass (1.0.6)
39
- diff-lcs (1.3)
40
- docile (1.3.2)
39
+ diff-lcs (1.4.4)
40
+ docile (1.3.5)
41
41
  e2mmap (0.1.0)
42
- erubi (1.9.0)
43
- i18n (1.8.2)
42
+ erubi (1.10.0)
43
+ i18n (1.8.10)
44
44
  concurrent-ruby (~> 1.0)
45
45
  jaro_winkler (1.5.4)
46
- json (2.3.0)
47
- loofah (2.4.0)
46
+ json (2.5.1)
47
+ kramdown (2.3.1)
48
+ rexml
49
+ kramdown-parser-gfm (1.1.0)
50
+ kramdown (~> 2.0)
51
+ loofah (2.9.1)
48
52
  crass (~> 1.0.2)
49
53
  nokogiri (>= 1.5.9)
50
- maruku (0.7.3)
51
- method_source (0.9.2)
52
- mini_portile2 (2.4.0)
53
- minitest (5.14.0)
54
- nokogiri (1.10.8)
55
- mini_portile2 (~> 2.4.0)
56
- parallel (1.19.1)
57
- parser (2.7.0.2)
58
- ast (~> 2.4.0)
59
- rack (2.2.2)
60
- rack-protection (2.0.8.1)
61
- rack
54
+ method_source (1.0.0)
55
+ mini_portile2 (2.5.1)
56
+ minitest (5.14.4)
57
+ nokogiri (1.11.3)
58
+ mini_portile2 (~> 2.5.0)
59
+ racc (~> 1.4)
60
+ parallel (1.20.1)
61
+ parser (3.0.1.0)
62
+ ast (~> 2.4.1)
63
+ racc (1.5.2)
64
+ rack (2.2.3)
62
65
  rack-test (1.1.0)
63
66
  rack (>= 1.0, < 3)
64
67
  rails-dom-testing (2.0.3)
@@ -66,76 +69,78 @@ GEM
66
69
  nokogiri (>= 1.6)
67
70
  rails-html-sanitizer (1.3.0)
68
71
  loofah (~> 2.3)
69
- railties (6.0.2.1)
70
- actionpack (= 6.0.2.1)
71
- activesupport (= 6.0.2.1)
72
+ railties (6.1.3.2)
73
+ actionpack (= 6.1.3.2)
74
+ activesupport (= 6.1.3.2)
72
75
  method_source
73
76
  rake (>= 0.8.7)
74
- thor (>= 0.20.3, < 2.0)
77
+ thor (~> 1.0)
75
78
  rainbow (3.0.0)
76
- rake (10.5.0)
77
- redis (4.1.3)
79
+ rake (13.0.3)
80
+ redis (4.2.5)
81
+ regexp_parser (2.1.1)
78
82
  request_store (1.5.0)
79
83
  rack (>= 1.4)
80
- reverse_markdown (1.4.0)
84
+ reverse_markdown (2.0.0)
81
85
  nokogiri
82
- rexml (3.2.4)
83
- rspec (3.9.0)
84
- rspec-core (~> 3.9.0)
85
- rspec-expectations (~> 3.9.0)
86
- rspec-mocks (~> 3.9.0)
87
- rspec-core (3.9.1)
88
- rspec-support (~> 3.9.1)
89
- rspec-expectations (3.9.0)
86
+ rexml (3.2.5)
87
+ rspec (3.10.0)
88
+ rspec-core (~> 3.10.0)
89
+ rspec-expectations (~> 3.10.0)
90
+ rspec-mocks (~> 3.10.0)
91
+ rspec-core (3.10.1)
92
+ rspec-support (~> 3.10.0)
93
+ rspec-expectations (3.10.1)
90
94
  diff-lcs (>= 1.2.0, < 2.0)
91
- rspec-support (~> 3.9.0)
92
- rspec-mocks (3.9.1)
95
+ rspec-support (~> 3.10.0)
96
+ rspec-mocks (3.10.2)
93
97
  diff-lcs (>= 1.2.0, < 2.0)
94
- rspec-support (~> 3.9.0)
95
- rspec-support (3.9.2)
96
- rubocop (0.80.0)
97
- jaro_winkler (~> 1.5.1)
98
+ rspec-support (~> 3.10.0)
99
+ rspec-support (3.10.2)
100
+ rubocop (0.93.1)
98
101
  parallel (~> 1.10)
99
- parser (>= 2.7.0.1)
102
+ parser (>= 2.7.1.5)
100
103
  rainbow (>= 2.2.2, < 4.0)
104
+ regexp_parser (>= 1.8)
101
105
  rexml
106
+ rubocop-ast (>= 0.6.0)
102
107
  ruby-progressbar (~> 1.7)
103
- unicode-display_width (>= 1.4.0, < 1.7)
108
+ unicode-display_width (>= 1.4.0, < 2.0)
109
+ rubocop-ast (1.4.1)
110
+ parser (>= 2.7.1.5)
104
111
  rubocop-performance (1.5.2)
105
112
  rubocop (>= 0.71.0)
106
- ruby-progressbar (1.10.1)
107
- sidekiq (6.0.5)
113
+ ruby-progressbar (1.11.0)
114
+ sidekiq (6.2.1)
108
115
  connection_pool (>= 2.2.2)
109
116
  rack (~> 2.0)
110
- rack-protection (>= 2.0.0)
111
- redis (>= 4.1.0)
117
+ redis (>= 4.2.0)
112
118
  simplecov (0.17.1)
113
119
  docile (~> 1.1)
114
120
  json (>= 1.8, < 3)
115
121
  simplecov-html (~> 0.10.0)
116
122
  simplecov-html (0.10.2)
117
- solargraph (0.38.5)
123
+ solargraph (0.40.4)
118
124
  backport (~> 1.1)
119
125
  benchmark
120
126
  bundler (>= 1.17.2)
121
127
  e2mmap
122
128
  jaro_winkler (~> 1.5)
123
- maruku (~> 0.7, >= 0.7.3)
124
- nokogiri (~> 1.9, >= 1.9.1)
125
- parser (~> 2.3)
126
- reverse_markdown (~> 1.0, >= 1.0.5)
127
- rubocop (~> 0.52)
129
+ kramdown (~> 2.3)
130
+ kramdown-parser-gfm (~> 1.1)
131
+ parser (~> 3.0)
132
+ reverse_markdown (>= 1.0.5, < 3)
133
+ rubocop (>= 0.52)
128
134
  thor (~> 1.0)
129
135
  tilt (~> 2.0)
130
- yard (~> 0.9)
131
- thor (1.0.1)
132
- thread_safe (0.3.6)
136
+ yard (~> 0.9, >= 0.9.24)
137
+ thor (1.1.0)
133
138
  tilt (2.0.10)
134
- tzinfo (1.2.6)
135
- thread_safe (~> 0.1)
136
- unicode-display_width (1.6.1)
137
- yard (0.9.24)
138
- zeitwerk (2.3.0)
139
+ tzinfo (2.0.4)
140
+ concurrent-ruby (~> 1.0)
141
+ unicode-display_width (1.7.0)
142
+ yard (0.9.26)
143
+ zeitwerk (2.4.2)
139
144
 
140
145
  PLATFORMS
141
146
  ruby
@@ -143,13 +148,13 @@ PLATFORMS
143
148
  DEPENDENCIES
144
149
  bundler (~> 2.0)
145
150
  heavylog!
146
- rake (~> 10.0)
151
+ rake (~> 13.0)
147
152
  rspec (~> 3.0)
148
153
  rubocop (~> 0.71)
149
154
  rubocop-performance (~> 1.5.2)
150
155
  sidekiq (>= 5.0)
151
156
  simplecov (~> 0.17.1)
152
- solargraph (~> 0.38.5)
157
+ solargraph
153
158
 
154
159
  BUNDLED WITH
155
- 2.0.2
160
+ 2.2.15
data/README.md CHANGED
@@ -1,39 +1,102 @@
1
- # Heavylog [![Build Status](https://travis-ci.org/krisrang/heavylog.svg?branch=master)](https://travis-ci.org/krisrang/heavylog)
1
+ # Heavylog [![Test](https://github.com/krisrang/heavylog/actions/workflows/test.yml/badge.svg)](https://github.com/krisrang/heavylog/actions/workflows/test.yml) [![Gem Version](https://badge.fury.io/rb/heavylog.svg)](https://badge.fury.io/rb/heavylog)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/heavylog`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Heavylog enables you to log all Rails requests to file as JSON or any other format you want.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Aside from metadata about requests full output is also included like SQL/Rails logging and `puts` statements.
6
+ Example request using the JSON formatter:
7
+ ```
8
+ {"request_id":"e2cdef0a-9851-4aab-b58f-60e607b4d1a9","request_start":"2021-04-25T15:37:20+00:00","ip":"52.52.52.52","messages":"Started GET \"/admin/info/sidekiq\" for 52.52.52.52 at 2021-04-25 15:37:20 +0000\nProcessing by Admin::InfoController#sidekiq_stats as */*\nRequested via apphost.com as */*\n Snippet Load (1.8ms) SELECT \"snippets\".* FROM \"snippets\" WHERE (locale = 'sv-SE' AND tag = 'information_notice_contact') ORDER BY \"snippets\".\"id\" ASC LIMIT $1 [[\"LIMIT\", 1]]\n Snippet Load (1.5ms) SELECT \"snippets\".* FROM \"snippets\" WHERE (locale = 'sv-SE' AND tag = 'contact_us_frame') ORDER BY \"snippets\".\"id\" ASC LIMIT $1 [[\"LIMIT\", 1]]\n Rendering text template\n Rendered text template (Duration: 0.1ms | Allocations: 16)\nCompleted 200 OK in 41ms (Views: 0.6ms | ActiveRecord: 3.3ms | Allocations: 10734)\n","method":"GET","path":"/admin/info/sidekiq","format":"*/*","controller":"Admin::InfoController","action":"sidekiq_stats","status":200,"duration":40.74,"view_runtime":0.56,"db_runtime":3.28,"user_id":null,"admin_id":null,"request_host":"apphost.com","ua":"curl/7.58.0","operation":null}
9
+ ```
6
10
 
11
+ Example use case is collecting the JSON files and shipping them to an Elastic/Kibana cluster for centralized logging.
7
12
  ## Installation
8
13
 
9
14
  Add this line to your application's Gemfile:
10
15
 
11
16
  ```ruby
12
- gem 'heavylog'
17
+ gem "heavylog"
13
18
  ```
14
19
 
15
20
  And then execute:
16
21
 
17
- $ bundle
22
+ ```bash
23
+ $ bundle
24
+ ```
18
25
 
19
- Or install it yourself as:
26
+ ## Usage
20
27
 
21
- $ gem install heavylog
28
+ Enable and configure in a Rails initializer `config/initializers/logger.rb`:
22
29
 
23
- ## Usage
30
+ ```rb
31
+ Rails.application.configure do
32
+ config.heavylog.enabled = true
33
+ config.heavylog.path = Rails.root.join("log/heavylog.log")
34
+ # Default formatter is Heavylog::Formatters::Raw which simply outputs the ruby hash as a string
35
+ config.heavylog.formatter = Heavylog::Formatters::Json.new
36
+ config.heavylog.log_sidekiq = true # Default is `false`, set to `true` to automatically log sidekiq job runs too.
37
+ end
38
+ ```
24
39
 
25
- TODO: Write usage instructions here
40
+ ## Configuration
41
+
42
+ | Option | Type | Description |
43
+ | :-------- | :------- | :------------------------- |
44
+ | `enabled` | `boolean` | Set to `true` to enable logging. Default: `false`. |
45
+ | `path` | `string` | Path to output file. Default: `log/heavylog.log`. |
46
+ | `message_limit` | `integer` | Request output will be truncated if longer than this limit. Default: `52428800` (50MB). |
47
+ | `log_sidekiq` | `boolean` | Set to `true` to automatically log sidekiq jobs too. Default: `false`. |
48
+ | `error_handler` | `lambda/proc` | Code to execute when formatter throws exception. Default: `->(e) { p "HeavyLog: Error writing to log: #{e.class}: #{e.message}\n #{e.backtrace.join("\n ")}" }`
49
+ | `custom_payload` | `block` | Block executed for every request that should return hash with extra fields you want to log. Default: `nil`. |
50
+
51
+ ### Custom payload
52
+
53
+ `custom_payload` accepts a block with a single argument, `controller` you can use to access any methods you normally would in a controller action.
54
+ It should return a hash with the extra fields you want to log.
55
+
56
+ ```rb
57
+ Rails.application.configure do
58
+ config.heavylog.custom_payload do |controller|
59
+ user_id = controller.respond_to?(:current_user) ? controller.current_user&.id : nil
60
+
61
+ {
62
+ user_id: user_id,
63
+ request_host: controller.request.host,
64
+ ua: controller.request.user_agent,
65
+ operation: controller.request.params[:operationName],
66
+ }
67
+ end
68
+ end
69
+ ```
26
70
 
27
- ## Development
71
+ ### Sidekiq logging
28
72
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
73
+ Set `log_sidekiq` to `true` if you want to automatically log Sidekiq job runs to the same file. Example with JSON formatter:
74
+ ```
75
+ {"request_id":"fb2c3798e2634011d670f753","request_start":"2021-04-25T16:00:53+00:00","ip":"127.0.0.1","messages":" Order Load (1.8ms) SELECT \"orders\".* FROM \"orders\" WHERE \"orders\".\"id\" = $1 LIMIT $2 [[\"id\", 109987473], [\"LIMIT\", 1]]\n Customer Load (1.7ms) SELECT \"customers\".* FROM \"customers\" WHERE \"customers\".\"id\" = $1 LIMIT $2 [[\"id\", 1027337], [\"LIMIT\", 1]]\n","controller":"SidekiqLogger","action":"MailPrepaidCheckupsJob","args":"[109987473]"}
76
+ ```
77
+
78
+ Sidekiq job runs go into the same file as regular request logs but have controller set to `SidekiqLogger` and action to the name of the Job.
30
79
 
31
- 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
80
+ ### JSON formatter
32
81
 
33
- ## Contributing
82
+ Every request results in a hash containing all info about the request. The default `Heavylog::Formatters::Raw` formatter simply outputs the hash as a string to the output file.
83
+ Use the `Heavylog::Formatters::Json` formatter to convert the hash to JSON. The resulting file will contain one JSON object per line for every request.
34
84
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/krisrang/heavylog.
85
+ ### Custom formatter
36
86
 
87
+ The formatter interface is simply a class with a `call` method that accepts a single argument which is the hash containing info about the request.
88
+ The method should return the final result you want to write to file. Heavylog writes one line per request.
89
+
90
+ JSON formatter for example:
91
+ ```rb
92
+ class Json
93
+ def call(data)
94
+ ::JSON.dump(data)
95
+ end
96
+ end
97
+ ```
37
98
  ## License
38
99
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
100
+ [MIT](https://choosealicense.com/licenses/mit/)
101
+
102
+
data/heavylog.gemspec CHANGED
@@ -23,13 +23,13 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ["lib"]
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 2.0"
26
- spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rake", "~> 13.0"
27
27
  spec.add_development_dependency "rspec", "~> 3.0"
28
28
  spec.add_development_dependency "rubocop", "~> 0.71"
29
29
  spec.add_development_dependency "rubocop-performance", "~> 1.5.2"
30
30
  spec.add_development_dependency "simplecov", "~> 0.17.1"
31
31
  spec.add_development_dependency "sidekiq", ">= 5.0"
32
- spec.add_development_dependency "solargraph", "~> 0.38.5"
32
+ spec.add_development_dependency "solargraph"
33
33
 
34
34
  spec.add_runtime_dependency "actionpack", ">= 5"
35
35
  spec.add_runtime_dependency "activesupport", ">= 5"
data/lib/heavylog.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "heavylog/version"
4
4
  require "heavylog/formatters/raw"
5
5
  require "heavylog/formatters/json"
6
+ require "heavylog/formatters/ecs"
6
7
  require "heavylog/log_subscriber"
7
8
  require "heavylog/middleware"
8
9
  require "heavylog/ordered_options"
@@ -136,8 +137,10 @@ module Heavylog
136
137
  messages: buffer.string.dup,
137
138
  }.merge(RequestStore.store[:heavylog_request_data] || {})
138
139
 
139
- formatted = Heavylog.formatter.call(request)
140
+ formatted = Heavylog.formatter.call(request.transform_keys(&:to_s))
140
141
  Heavylog.logger.send(Heavylog.log_level, formatted)
142
+ rescue StandardError => e
143
+ config.error_handler&.(e)
141
144
  end
142
145
 
143
146
  def finish_sidekiq
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Heavylog
6
+ module Formatters
7
+ class ECS
8
+ # mapping from heavylog standard keys to ECS https://www.elastic.co/guide/en/ecs/current/ecs-reference.html
9
+ ECS_MAP = {
10
+ "request_start" => "@timestamp",
11
+ "messages" => "message",
12
+ "request_id" => "http.request.id",
13
+ "method" => "http.request.method",
14
+ "referrer" => "http.request.referrer",
15
+ "format" => "http.response.format",
16
+ "status" => "http.response.status_code",
17
+ "location" => "http.response.location",
18
+ "ip" => "source.address",
19
+ "host" => "url.domain",
20
+ "path" => "url.original",
21
+ "user_agent" => "user_agent.original",
22
+ "controller" => "heavylog.controller",
23
+ "action" => "heavylog.action",
24
+ "unpermitted_params" => "heavylog.unpermitted_params",
25
+ "args" => "heavylog.args",
26
+ "duration" => "heavylog.duration",
27
+ "view_runtime" => "heavylog.view_runtime",
28
+ "db_runtime" => "heavylog.db_runtime",
29
+ }.freeze
30
+
31
+ def call(data)
32
+ ECS_MAP.each do |original, correct|
33
+ dig_set(data, correct.split("."), data.delete(original)) if data.key?(original)
34
+ end
35
+
36
+ dig_set(data, %w[event module], "heavylog")
37
+ dig_set(data, %w[event category], "web")
38
+
39
+ unless data.dig("event", "dataset")
40
+ value = data.dig("heavylog", "controller") == "SidekiqLogger" ? "heavylog.sidekiq" : "heavylog.rails"
41
+ dig_set(data, %w[event dataset], value)
42
+ end
43
+
44
+ if (code = data.dig("http", "response", "status_code"))
45
+ dig_set(data, %w[event outcome], (200..399).cover?(code) ? "success" : "failure")
46
+ end
47
+
48
+ dig_set(data, %w[source ip], data.dig("source", "address")) unless data.dig("source", "ip")
49
+ dig_set(data, %w[url path], data.dig("url", "original")) unless data.dig("url", "path")
50
+
51
+ ::JSON.dump(data)
52
+ end
53
+
54
+ private
55
+
56
+ def dig_set(obj, keys, value)
57
+ key = keys.first
58
+ if keys.length == 1
59
+ obj[key] = value
60
+ else
61
+ obj[key] = {} unless obj[key]
62
+ dig_set(obj[key], keys.slice(1..-1), value)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -31,6 +31,7 @@ module Heavylog
31
31
  data.merge!(extract_runtimes(event, payload))
32
32
  data.merge!(extract_location)
33
33
  data.merge!(extract_unpermitted_params)
34
+ data.merge!(extract_request_data(payload))
34
35
  data.merge!(custom_options(event))
35
36
  end
36
37
 
@@ -101,5 +102,19 @@ module Heavylog
101
102
  RequestStore.store[:heavylog_unpermitted_params] = nil
102
103
  { unpermitted_params: unpermitted_params }
103
104
  end
105
+
106
+ def extract_request_data(payload)
107
+ if payload[:request].is_a?(ActionDispatch::Request)
108
+ data = {}
109
+
110
+ data[:referrer] = payload[:request].referrer if payload[:request].referrer
111
+ data[:host] = payload[:request].host
112
+ data[:user_agent] = payload[:request].user_agent
113
+
114
+ return data
115
+ end
116
+
117
+ {}
118
+ end
104
119
  end
105
120
  end
@@ -7,6 +7,9 @@ module Heavylog
7
7
  config.heavylog.path = "log/heavylog.log"
8
8
  config.heavylog.message_limit = 1024 * 1024 * 50 # 50MB
9
9
  config.heavylog.log_sidekiq = false
10
+ config.heavylog.error_handler = lambda { |e|
11
+ Kernel.puts "HeavyLog: Error writing to log: #{e.class}: #{e.message}\n #{e.backtrace.join("\n ")}"
12
+ }
10
13
 
11
14
  initializer "heavylog.insert_middleware" do |app|
12
15
  app.config.middleware.insert_before Rails::Rack::Logger, Heavylog::Middleware
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Heavylog
4
- VERSION = "0.0.17"
4
+ VERSION = "0.0.22"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heavylog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristjan Rang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-17 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -112,16 +112,16 @@ dependencies:
112
112
  name: solargraph
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - "~>"
115
+ - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: 0.38.5
117
+ version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - "~>"
122
+ - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: 0.38.5
124
+ version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: actionpack
127
127
  requirement: !ruby/object:Gem::Requirement
@@ -199,6 +199,7 @@ files:
199
199
  - bin/setup
200
200
  - heavylog.gemspec
201
201
  - lib/heavylog.rb
202
+ - lib/heavylog/formatters/ecs.rb
202
203
  - lib/heavylog/formatters/json.rb
203
204
  - lib/heavylog/formatters/raw.rb
204
205
  - lib/heavylog/log_subscriber.rb
@@ -228,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
229
  - !ruby/object:Gem::Version
229
230
  version: '0'
230
231
  requirements: []
231
- rubygems_version: 3.1.2
232
+ rubygems_version: 3.2.15
232
233
  signing_key:
233
234
  specification_version: 4
234
235
  summary: Format all Rails logging per request