heavylog 0.0.19 → 0.0.20

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: 817f124071c11b74c359b8ce78ec24cc8778bfab7c3d3ee79da64847e30f1f66
4
- data.tar.gz: 80ecb337b3e34dc5a50419f31d3a9bb20fdddbe7da23ffefc52b9b2197eee757
3
+ metadata.gz: 0d938340a9fc57747dc46c35aadb0a9b92c08da6466f3d333479e7b0c4de63d4
4
+ data.tar.gz: f31b3c1ec5566d8de8c6199b21dfae45b00b22c5710dd1eecb07e676a55b3a2e
5
5
  SHA512:
6
- metadata.gz: 54e8d27f4dd9c4e69bd4e2a8103a085e246f0e9067bdd056c328efd5edf73d6c9158626bd68245bc7ace3bedfe58ef6a8c821de034d3737d0670231f2cbf47e1
7
- data.tar.gz: ade91b07e69e29c288b5cda82b3dd00717248099bbf7da75e9f375e08732d53a506f9345f2565ea349853eff66a1c1de65219160c70949fba532e6a49323d463
6
+ metadata.gz: 44fa9815cb453c895d70db9912558f9b16cf775caaa83d1d723fde9252ced6a6c4a07072d0b0db8b352236a11701330af9ec0adf7de8bc94c41a89e96c6e5dfa
7
+ data.tar.gz: 993f3cb991245997f89827c4bd6581060b35eadd64d4c229419c5dbe6cbc1f8467319e66514c91af9d445bd683231144d8393419729b4ab513f7a7996ffe1854
@@ -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: 3.0.0
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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- heavylog (0.0.19)
4
+ heavylog (0.0.20)
5
5
  actionpack (>= 5)
6
6
  activesupport (>= 5)
7
7
  railties (>= 5)
@@ -10,20 +10,20 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- actionpack (6.1.3.1)
14
- actionview (= 6.1.3.1)
15
- activesupport (= 6.1.3.1)
13
+ actionpack (6.1.3.2)
14
+ actionview (= 6.1.3.2)
15
+ activesupport (= 6.1.3.2)
16
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.1.3.1)
21
- activesupport (= 6.1.3.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.1.3.1)
26
+ activesupport (6.1.3.2)
27
27
  concurrent-ruby (~> 1.0, >= 1.0.2)
28
28
  i18n (>= 1.6, < 2)
29
29
  minitest (>= 5.1)
@@ -34,7 +34,7 @@ GEM
34
34
  benchmark (0.1.1)
35
35
  builder (3.2.4)
36
36
  concurrent-ruby (1.1.8)
37
- connection_pool (2.2.3)
37
+ connection_pool (2.2.5)
38
38
  crass (1.0.6)
39
39
  diff-lcs (1.4.4)
40
40
  docile (1.3.5)
@@ -52,13 +52,13 @@ GEM
52
52
  crass (~> 1.0.2)
53
53
  nokogiri (>= 1.5.9)
54
54
  method_source (1.0.0)
55
- mini_portile2 (2.5.0)
55
+ mini_portile2 (2.5.1)
56
56
  minitest (5.14.4)
57
- nokogiri (1.11.2)
57
+ nokogiri (1.11.3)
58
58
  mini_portile2 (~> 2.5.0)
59
59
  racc (~> 1.4)
60
60
  parallel (1.20.1)
61
- parser (3.0.0.0)
61
+ parser (3.0.1.0)
62
62
  ast (~> 2.4.1)
63
63
  racc (1.5.2)
64
64
  rack (2.2.3)
@@ -69,9 +69,9 @@ GEM
69
69
  nokogiri (>= 1.6)
70
70
  rails-html-sanitizer (1.3.0)
71
71
  loofah (~> 2.3)
72
- railties (6.1.3.1)
73
- actionpack (= 6.1.3.1)
74
- activesupport (= 6.1.3.1)
72
+ railties (6.1.3.2)
73
+ actionpack (= 6.1.3.2)
74
+ activesupport (= 6.1.3.2)
75
75
  method_source
76
76
  rake (>= 0.8.7)
77
77
  thor (~> 1.0)
@@ -83,7 +83,7 @@ GEM
83
83
  rack (>= 1.4)
84
84
  reverse_markdown (2.0.0)
85
85
  nokogiri
86
- rexml (3.2.4)
86
+ rexml (3.2.5)
87
87
  rspec (3.10.0)
88
88
  rspec-core (~> 3.10.0)
89
89
  rspec-expectations (~> 3.10.0)
@@ -111,7 +111,7 @@ GEM
111
111
  rubocop-performance (1.5.2)
112
112
  rubocop (>= 0.71.0)
113
113
  ruby-progressbar (1.11.0)
114
- sidekiq (6.2.0)
114
+ sidekiq (6.2.1)
115
115
  connection_pool (>= 2.2.2)
116
116
  rack (~> 2.0)
117
117
  redis (>= 4.2.0)
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/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,7 +137,7 @@ 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)
141
142
  rescue StandardError => e
142
143
  config.error_handler&.(e)
@@ -0,0 +1,48 @@
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
+ "format" => "http.request.mime_type",
15
+ "status" => "http.response.status_code",
16
+ "location" => "http.response.location",
17
+ "ip" => "source.address",
18
+ "path" => "url.original",
19
+ "controller" => "heavylog.controller",
20
+ "action" => "heavylog.action",
21
+ "unpermitted_params" => "heavylog.unpermitted_params",
22
+ "args" => "heavylog.args",
23
+ "duration" => "heavylog.duration",
24
+ "view_runtime" => "heavylog.view_runtime",
25
+ "db_runtime" => "heavylog.db_runtime",
26
+ }.freeze
27
+
28
+ def call(data)
29
+ ECS_MAP.each do |original, correct|
30
+ data[correct] = data.delete(original) if data[original]
31
+ end
32
+
33
+ data["event.module"] = "heavylog"
34
+ data["event.category"] = "web"
35
+ data["event.dataset"] ||= data["heavylog.controller"] == "SidekiqLogger" ? "heavylog.sidekiq" : "heavylog.rails"
36
+
37
+ if data["http.response.status_code"]
38
+ data["event.outcome"] = (200..399).cover?(data["http.response.status_code"]) ? "success" : "failure"
39
+ end
40
+
41
+ data["source.ip"] ||= data["source.address"]
42
+ data["url.path"] ||= data["url.original"]
43
+
44
+ ::JSON.dump(data)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Heavylog
4
- VERSION = "0.0.19"
4
+ VERSION = "0.0.20"
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.19
4
+ version: 0.0.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristjan Rang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-25 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -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