applicaster-logger 0.6.7 → 0.7.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: cfb0e5868ce7eebc1be17ad406fd1ae407041afe
4
- data.tar.gz: 5cc7d9abaa15b4684c596917e515d2d13297e239
3
+ metadata.gz: 14eb9fb38934bed703677d907edb9a25dbf3c6fa
4
+ data.tar.gz: a99a2cb2212f1d8d3846fb3790b254d030b9afbc
5
5
  SHA512:
6
- metadata.gz: d733913840fdc1a3ea3b5cf88db2c17ba46e6c676c9551b0283b0bf0b9897248e13aaf20dcd2eb5b4e0fb46280d54b5e5bdcbbea1585be922fb8085d00c05373
7
- data.tar.gz: 172c7c97467d0d898938bffa9d470cab4a840733a369630fd71435d3000bd05d4aeb636c8a63ea9123384d182dff71a5b64cb4703adde41ade4dc4371b7eb34f
6
+ metadata.gz: 7240f155356278285e8fd0a7c6c2523acf49dfeb78eab01ffaf66a27c4fa5484b6f32946e902e07f17754780ed2d34bd8d17c85e93d503c0531f4b20873afb43
7
+ data.tar.gz: 89a300fbbf6c3eb294c0408a7d64178443354061d718ca37e35cfa2f571a9c76266ccc1d17f30e18980df90b04fa38af194e7c3445f6ec7b78c9264e14e20477
@@ -1,3 +1,5 @@
1
1
  require 'applicaster/logger'
2
2
  require "applicaster/rack/request_data"
3
3
  require "applicaster/sidekiq/middleware"
4
+ require "applicaster/sidekiq/job_logger"
5
+ require "applicaster/sidekiq/exception_logger"
@@ -38,12 +38,22 @@ module Applicaster
38
38
  end
39
39
 
40
40
  if defined?(::Sidekiq)
41
- require 'sidekiq/api'
42
-
43
41
  ::Sidekiq.configure_server do |config|
44
- config.server_middleware do |chain|
45
- chain.remove ::Sidekiq::Middleware::Server::Logging
46
- chain.add Applicaster::Sidekiq::Middleware::Server::LogstashLogging
42
+ config.error_handlers.delete_if { |h| h.is_a?(::Sidekiq::ExceptionHandler::Logger) }
43
+ ::Sidekiq.error_handlers << Applicaster::Sidekiq::ExceptionLogger.new
44
+
45
+ if Gem::Version.new(::Sidekiq::VERSION) < Gem::Version.new("5.0")
46
+ require 'sidekiq/api'
47
+ config.server_middleware do |chain|
48
+ chain.remove ::Sidekiq::Middleware::Server::Logging
49
+ chain.add Applicaster::Sidekiq::Middleware::Server::LogstashLogging
50
+ end
51
+ else
52
+ logger = LogStashLogger.new(logstash_config)
53
+ logger.level = app.config.applicaster_logger.level
54
+ logger.formatter = Applicaster::Logger::Formatter.new(facility: "sidekiq")
55
+ ::Sidekiq::Logging.logger = logger
56
+ ::Sidekiq.options[:job_logger] = ::Applicaster::Sidekiq::JobLogger
47
57
  end
48
58
  end
49
59
  end
@@ -68,5 +78,37 @@ module Applicaster
68
78
  def self.current_thread_data
69
79
  Thread.current[:logger_thread_data] || {}
70
80
  end
81
+
82
+ # Truncates +text+ to at most <tt>bytesize</tt> bytes in length without
83
+ # breaking string encoding by splitting multibyte characters or breaking
84
+ # grapheme clusters ("perceptual characters") by truncating at combining
85
+ # characters.
86
+ # Code taken from activesupport/lib/active_support/core_ext/string/filters.rb
87
+ def self.truncate_bytes(text, truncate_at, omission: "...")
88
+ omission ||= ""
89
+
90
+ case
91
+ when text.bytesize <= truncate_at
92
+ text.dup
93
+ when omission.bytesize > truncate_at
94
+ raise ArgumentError, "Omission #{omission.inspect} is #{omission.bytesize}, larger than the truncation length of #{truncate_at} bytes"
95
+ when omission.bytesize == truncate_at
96
+ omission.dup
97
+ else
98
+ text.class.new.tap do |cut|
99
+ cut_at = truncate_at - omission.bytesize
100
+
101
+ text.scan(/\X/) do |grapheme|
102
+ if cut.bytesize + grapheme.bytesize <= cut_at
103
+ cut << grapheme
104
+ else
105
+ break
106
+ end
107
+ end
108
+
109
+ cut << omission
110
+ end
111
+ end
112
+ end
71
113
  end
72
114
  end
@@ -1,5 +1,5 @@
1
1
  module Applicaster
2
2
  module Logger
3
- VERSION = "0.6.7"
3
+ VERSION = "0.7.0"
4
4
  end
5
5
  end
@@ -0,0 +1,27 @@
1
+ module Applicaster
2
+ module Sidekiq
3
+ class ExceptionLogger
4
+ def call(exception, ctxHash)
5
+ item = ctxHash[:job]
6
+ queue = item[:queue]
7
+
8
+ event = log_context(item, queue).merge({
9
+ message: "Fail: #{item['class']} JID-#{item['jid']}",
10
+ exception_class: exception.class.to_s,
11
+ exception_message: Applicaster::Logger.truncate_bytes(exception.message.to_s, 500),
12
+ })
13
+ logger.info(event)
14
+ end
15
+
16
+ private
17
+
18
+ def log_context(item, queue)
19
+ Applicaster::Sidekiq::JobLogger.log_context(item, queue)
20
+ end
21
+
22
+ def logger
23
+ ::Sidekiq.logger
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,50 @@
1
+ module Applicaster
2
+ module Sidekiq
3
+ class JobLogger
4
+ def call(item, queue)
5
+ puts "JobLogger"
6
+ start = Time.now
7
+ event = log_context(item, queue).merge({
8
+ message: "Start: #{item['class']} JID-#{item['jid']}",
9
+ })
10
+ logger.info(event)
11
+ yield
12
+ event = log_context(item, queue).merge({
13
+ message: "Done: #{item['class']} JID-#{item['jid']}",
14
+ })
15
+ event[:sidekiq][:duration] = elapsed(start)
16
+ logger.info(event)
17
+ end
18
+
19
+ private
20
+
21
+ def elapsed(start)
22
+ (Time.now - start).round(3)
23
+ end
24
+
25
+ def logger
26
+ ::Sidekiq.logger
27
+ end
28
+
29
+ def log_context(item, queue)
30
+ self.class.log_context(item, queue)
31
+ end
32
+
33
+ def self.sidekiq_context
34
+ ::Thread.current[:sidekiq_context]
35
+ end
36
+
37
+ def self.log_context(item, queue)
38
+ {
39
+ sidekiq: {
40
+ jid: item['jid'],
41
+ context: sidekiq_context,
42
+ worker: item['class'].to_s,
43
+ queue: queue,
44
+ args: item['args'].inspect,
45
+ }
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -48,8 +48,7 @@ module Applicaster
48
48
  args: item['args'].inspect,
49
49
  runtime: elapsed(start),
50
50
  exception_class: e.class.to_s,
51
- exception_message: e.message,
52
- backtrace: e.backtrace.join("\n"),
51
+ exception_message: Applicaster::Logger.truncate_bytes(e.message.to_s, 500),
53
52
  memory: memory
54
53
  }))
55
54
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: applicaster-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Gorodetsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-29 00:00:00.000000000 Z
11
+ date: 2018-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-logger
@@ -85,6 +85,8 @@ files:
85
85
  - lib/applicaster/logger/railtie.rb
86
86
  - lib/applicaster/logger/version.rb
87
87
  - lib/applicaster/rack/request_data.rb
88
+ - lib/applicaster/sidekiq/exception_logger.rb
89
+ - lib/applicaster/sidekiq/job_logger.rb
88
90
  - lib/applicaster/sidekiq/middleware.rb
89
91
  homepage: ''
90
92
  licenses:
@@ -106,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
108
  version: '0'
107
109
  requirements: []
108
110
  rubyforge_project:
109
- rubygems_version: 2.5.2.1
111
+ rubygems_version: 2.6.13
110
112
  signing_key:
111
113
  specification_version: 4
112
114
  summary: Configures loggers to send logs to logstash