heavylog 0.0.8 → 0.0.9

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: bebc8d293109a91a0667703a5b242d93d68c9a2bce37ea7a641b163b1058257f
4
- data.tar.gz: be5e75a379ab306cd05c679f9bd69435e28fa06ed1907199bbd879d472cfccc2
3
+ metadata.gz: 4aa084da3eb852633de1b48ab303da16e6f5177cf56995b135f483b92484159e
4
+ data.tar.gz: 86fc4f5d4892b4c18719c74a7f7d626ea536360811a5ce2a15bcd761eae91a33
5
5
  SHA512:
6
- metadata.gz: 0a4dc4c47f334abde1326b0c643ca3059d33a58c4b05f211597c1ef45e656ba81e6e59e9d858613fa9f2d03a8c1062c1fb48f44e47ba5ae830e8db3e3e95f5f4
7
- data.tar.gz: 9f78e3841a815d772cdae8789e95bd150aada4ba3137b8ac9be238ca1e918a65e712dc226ece4e6514804a3abd64eff6f3c6fb44a8a1396a5712c0d18597984c
6
+ metadata.gz: b904c1a842315c5a2aefea6e553c07b26b9055d3e5a72cc7a7cd0b0b4c129e6423161ff554284cdaa4a03d25f93247d7dc83587c719bdfcb4c62e51a4a10330b
7
+ data.tar.gz: bf51e06cb88c89fd027c86152441e137742729210130d127320fd7c5a56a92785876645fd99777816d242f12355fdf530b2f0d5ae537204381b304ca93465c0c
@@ -5,6 +5,7 @@ module Heavylog
5
5
  config.heavylog.enabled = false
6
6
  config.heavylog.path = 'log/heavylog.log'
7
7
  config.heavylog.message_limit = 1024 * 1024 * 50 # 50MB
8
+ config.heavylog.log_sidekiq = false
8
9
 
9
10
  initializer "heavylog.insert_middleware" do |app|
10
11
  app.config.middleware.insert_before Rails::Rack::Logger, Heavylog::Middleware
@@ -0,0 +1,10 @@
1
+ module Heavylog
2
+ class SidekiqExceptionHandler
3
+ def call(ex, ctxHash)
4
+ Heavylog.log(:warn, Sidekiq.dump_json(ctxHash)) if !ctxHash.empty?
5
+ Heavylog.log(:warn, "#{ex.class.name}: #{ex.message}")
6
+ Heavylog.log(:warn, ex.backtrace.join("\n")) unless ex.backtrace.nil?
7
+ Heavylog.finish_sidekiq
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module Heavylog
2
+ class SidekiqLogger
3
+ def call(item, queue)
4
+ # item = {"class"=>"SuspiciousJob", "args"=>[12754545, [3858890], "invoice"], "retry"=>true, "queue"=>"default", "jid"=>"5ec968571e358497d70a3cf2", "created_at"=>1540484817.3950138, "enqueued_at"=>1540484817.395076}
5
+
6
+ Heavylog.log_sidekiq(item["jid"], item["class"], item["args"])
7
+
8
+ begin
9
+ start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
10
+ logger.info("start")
11
+ yield
12
+ logger.info("done: #{elapsed(start)} sec")
13
+ Heavylog.finish_sidekiq
14
+ rescue Exception
15
+ logger.info("fail: #{elapsed(start)} sec")
16
+ raise
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def elapsed(start)
23
+ (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start).round(3)
24
+ end
25
+
26
+ def logger
27
+ Sidekiq.logger
28
+ end
29
+ end
30
+ end
@@ -1,3 +1,3 @@
1
1
  module Heavylog
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/lib/heavylog.rb CHANGED
@@ -6,6 +6,8 @@ require 'heavylog/log_subscriber'
6
6
  require 'heavylog/middleware'
7
7
  require 'heavylog/ordered_options'
8
8
  require 'heavylog/request_logger'
9
+ require 'heavylog/sidekiq_logger'
10
+ require 'heavylog/sidekiq_exception_handler'
9
11
 
10
12
  module Heavylog
11
13
  module_function
@@ -18,6 +20,7 @@ module Heavylog
18
20
  self.application = app
19
21
  patch_loggers
20
22
  attach_to_action_controller
23
+ attach_to_sidekiq
21
24
  setup_custom_payload
22
25
  set_options
23
26
  end
@@ -40,6 +43,16 @@ module Heavylog
40
43
  Heavylog::LogSubscriber.attach_to :action_controller
41
44
  end
42
45
 
46
+ def attach_to_sidekiq
47
+ return if !config.log_sidekiq
48
+
49
+ Sidekiq.configure_server do |config|
50
+ config.options[:job_logger] = SidekiqLogger
51
+ end
52
+
53
+ Sidekiq.error_handlers << SidekiqExceptionHandler.new
54
+ end
55
+
43
56
  def setup_custom_payload
44
57
  return unless config.custom_payload_method.respond_to?(:call)
45
58
 
@@ -85,6 +98,22 @@ module Heavylog
85
98
  end
86
99
  end
87
100
 
101
+ def log_sidekiq(jid, klass, args)
102
+ return if !config.enabled
103
+
104
+ RequestStore.store[:heavylog_request_id] = jid
105
+ RequestStore.store[:heavylog_request_start] = Time.now.iso8601
106
+ RequestStore.store[:heavylog_request_ip] = "127.0.0.1"
107
+
108
+ RequestStore.store[:heavylog_request_data] = {
109
+ controller: "SidekiqLogger",
110
+ action: klass,
111
+ args: args.to_s,
112
+ }
113
+
114
+ RequestStore.store[:heavylog_buffer] ||= StringIO.new
115
+ end
116
+
88
117
  def finish
89
118
  return if !config.enabled
90
119
 
@@ -102,6 +131,11 @@ module Heavylog
102
131
  Heavylog.logger.send(Heavylog.log_level, formatted)
103
132
  end
104
133
 
134
+ def finish_sidekiq
135
+ finish
136
+ RequestStore.store[:heavylog_buffer] = nil
137
+ end
138
+
105
139
  def config
106
140
  return OrderedOptions.new if !application
107
141
  application.config.heavylog
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.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristjan Rang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-21 00:00:00.000000000 Z
11
+ date: 2018-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -134,6 +134,8 @@ files:
134
134
  - lib/heavylog/ordered_options.rb
135
135
  - lib/heavylog/railtie.rb
136
136
  - lib/heavylog/request_logger.rb
137
+ - lib/heavylog/sidekiq_exception_handler.rb
138
+ - lib/heavylog/sidekiq_logger.rb
137
139
  - lib/heavylog/version.rb
138
140
  homepage: https://github.com/krisrang/heavylog
139
141
  licenses: