semantic_logger 4.7.2 → 4.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +50 -20
  3. data/lib/semantic_logger/appender/async.rb +1 -1
  4. data/lib/semantic_logger/appender/async_batch.rb +3 -1
  5. data/lib/semantic_logger/appender/bugsnag.rb +36 -23
  6. data/lib/semantic_logger/appender/elasticsearch.rb +24 -6
  7. data/lib/semantic_logger/appender/file.rb +249 -68
  8. data/lib/semantic_logger/appender/io.rb +68 -0
  9. data/lib/semantic_logger/appender/kafka.rb +4 -0
  10. data/lib/semantic_logger/appender/sentry_ruby.rb +138 -0
  11. data/lib/semantic_logger/appender/splunk.rb +2 -1
  12. data/lib/semantic_logger/appender/splunk_http.rb +3 -2
  13. data/lib/semantic_logger/appender/syslog.rb +5 -3
  14. data/lib/semantic_logger/appender/wrapper.rb +3 -2
  15. data/lib/semantic_logger/appender.rb +13 -6
  16. data/lib/semantic_logger/appenders.rb +31 -27
  17. data/lib/semantic_logger/base.rb +16 -7
  18. data/lib/semantic_logger/formatters/base.rb +1 -0
  19. data/lib/semantic_logger/formatters/color.rb +3 -3
  20. data/lib/semantic_logger/formatters/logfmt.rb +72 -0
  21. data/lib/semantic_logger/formatters/syslog.rb +2 -1
  22. data/lib/semantic_logger/formatters/syslog_cee.rb +2 -1
  23. data/lib/semantic_logger/formatters.rb +10 -11
  24. data/lib/semantic_logger/log.rb +2 -4
  25. data/lib/semantic_logger/loggable.rb +8 -1
  26. data/lib/semantic_logger/logger.rb +16 -6
  27. data/lib/semantic_logger/processor.rb +3 -3
  28. data/lib/semantic_logger/semantic_logger.rb +18 -10
  29. data/lib/semantic_logger/subscriber.rb +10 -0
  30. data/lib/semantic_logger/sync_processor.rb +5 -5
  31. data/lib/semantic_logger/test/capture_log_events.rb +34 -0
  32. data/lib/semantic_logger/utils.rb +29 -10
  33. data/lib/semantic_logger/version.rb +1 -1
  34. data/lib/semantic_logger.rb +4 -0
  35. metadata +13 -10
@@ -114,14 +114,12 @@ module SemanticLogger
114
114
 
115
115
  # Elastic logging: Log when :duration exceeds :min_duration
116
116
  # Except if there is an exception when it will always be logged
117
- if duration
118
- return false if (duration < min_duration) && exception.nil?
119
- end
117
+ return false if duration && ((duration < min_duration) && exception.nil?)
120
118
 
121
119
  if backtrace
122
120
  self.backtrace = Utils.extract_backtrace(backtrace)
123
121
  elsif level_index >= SemanticLogger.backtrace_level_index
124
- self.backtrace = Utils.extract_backtrace
122
+ self.backtrace = Utils.extract_backtrace(caller)
125
123
  end
126
124
 
127
125
  true
@@ -8,7 +8,7 @@
8
8
  # Example:
9
9
  # require 'semantic_logger'
10
10
  # SemanticLogger.default_level = :debug
11
- # SemanticLogger.add_appender(io: STDOUT, formatter: :color)
11
+ # SemanticLogger.add_appender(io: $stdout, formatter: :color)
12
12
  #
13
13
  # class ExternalSupplier
14
14
  # # Create class and instance logger methods
@@ -32,7 +32,14 @@ module SemanticLogger
32
32
  module Loggable
33
33
  def self.included(base)
34
34
  base.extend ClassMethods
35
+ base.singleton_class.class_eval do
36
+ undef_method :logger if method_defined?(:logger)
37
+ undef_method :logger= if method_defined?(:logger=)
38
+ end
35
39
  base.class_eval do
40
+ undef_method :logger if method_defined?(:logger)
41
+ undef_method :logger= if method_defined?(:logger=)
42
+
36
43
  # Returns [SemanticLogger::Logger] class level logger
37
44
  def self.logger
38
45
  @semantic_logger ||= SemanticLogger[self]
@@ -21,7 +21,19 @@ module SemanticLogger
21
21
  end
22
22
 
23
23
  def self.processor
24
- @processor ||= SemanticLogger.sync? ? SyncProcessor.new : Processor.new
24
+ @processor ||= Processor.new
25
+ end
26
+
27
+ # Switch to the synchronous processor
28
+ def self.sync!
29
+ return if @processor.is_a?(SyncProcessor)
30
+
31
+ @processor = SyncProcessor.new(@processor&.appenders)
32
+ end
33
+
34
+ # Running without the background logging thread?
35
+ def self.sync?
36
+ processor.is_a?(SyncProcessor)
25
37
  end
26
38
 
27
39
  # Returns a Logger instance
@@ -70,11 +82,9 @@ module SemanticLogger
70
82
  return unless @subscribers
71
83
 
72
84
  @subscribers.each do |subscriber|
73
- begin
74
- subscriber.call(log)
75
- rescue Exception => e
76
- processor.logger.error("Exception calling :on_log subscriber", e)
77
- end
85
+ subscriber.call(log)
86
+ rescue Exception => e
87
+ processor.logger.error("Exception calling :on_log subscriber", e)
78
88
  end
79
89
  end
80
90
  end
@@ -1,7 +1,7 @@
1
1
  module SemanticLogger
2
2
  # Thread that submits and processes log requests
3
3
  class Processor < Appender::Async
4
- # Allow the internal logger to be overridden from its default of STDERR
4
+ # Allow the internal logger to be overridden from its default of $stderr
5
5
  # Can be replaced with another Ruby logger or Rails logger, but never to
6
6
  # SemanticLogger::Logger itself since it is for reporting problems
7
7
  # while trying to log to the various appenders
@@ -11,11 +11,11 @@ module SemanticLogger
11
11
 
12
12
  # Internal logger for SemanticLogger
13
13
  # For example when an appender is not working etc..
14
- # By default logs to STDERR
14
+ # By default logs to $stderr
15
15
  def self.logger
16
16
  @logger ||=
17
17
  begin
18
- l = SemanticLogger::Appender::File.new(io: STDERR, level: :warn)
18
+ l = SemanticLogger::Appender::IO.new($stderr, level: :warn)
19
19
  l.name = name
20
20
  l
21
21
  end
@@ -101,7 +101,7 @@ module SemanticLogger
101
101
  # Or,
102
102
  # io: [IO]
103
103
  # An IO Stream to log to.
104
- # For example STDOUT, STDERR, etc.
104
+ # For example $stdout, $stderr, etc.
105
105
  #
106
106
  # Or,
107
107
  # appender: [Symbol|SemanticLogger::Subscriber]
@@ -122,7 +122,7 @@ module SemanticLogger
122
122
  # Default: SemanticLogger.default_level
123
123
  #
124
124
  # formatter: [Symbol|Object|Proc]
125
- # Any of the following symbol values: :default, :color, :json
125
+ # Any of the following symbol values: :default, :color, :json, :logfmt, etc...
126
126
  # Or,
127
127
  # An instance of a class that implements #call
128
128
  # Or,
@@ -138,14 +138,14 @@ module SemanticLogger
138
138
  # Examples:
139
139
  #
140
140
  # # Send all logging output to Standard Out (Screen)
141
- # SemanticLogger.add_appender(io: STDOUT)
141
+ # SemanticLogger.add_appender(io: $stdout)
142
142
  #
143
143
  # # Send all logging output to a file
144
144
  # SemanticLogger.add_appender(file_name: 'logfile.log')
145
145
  #
146
146
  # # Send all logging output to a file and only :info and above to standard output
147
147
  # SemanticLogger.add_appender(file_name: 'logfile.log')
148
- # SemanticLogger.add_appender(io: STDOUT, level: :info)
148
+ # SemanticLogger.add_appender(io: $stdout, level: :info)
149
149
  #
150
150
  # Log to log4r, Logger, etc.:
151
151
  #
@@ -154,7 +154,7 @@ module SemanticLogger
154
154
  # require 'semantic_logger'
155
155
  #
156
156
  # # Built-in Ruby logger
157
- # log = Logger.new(STDOUT)
157
+ # log = Logger.new($stdout)
158
158
  # log.level = Logger::DEBUG
159
159
  #
160
160
  # SemanticLogger.default_level = :debug
@@ -164,7 +164,7 @@ module SemanticLogger
164
164
  # logger.info "Hello World"
165
165
  # logger.debug("Login time", user: 'Joe', duration: 100, ip_address: '127.0.0.1')
166
166
  def self.add_appender(**args, &block)
167
- appender = Logger.processor.appenders.add(**args, &block)
167
+ appender = appenders.add(**args, &block)
168
168
  # Start appender thread if it is not already running
169
169
  Logger.processor.start
170
170
  appender
@@ -173,7 +173,15 @@ module SemanticLogger
173
173
  # Remove an existing appender
174
174
  # Currently only supports appender instances
175
175
  def self.remove_appender(appender)
176
- Logger.processor.appenders.delete(appender)
176
+ return unless appender
177
+
178
+ appenders.delete(appender)
179
+ appender.close
180
+ end
181
+
182
+ # Clear out all previously registered appenders
183
+ def self.clear_appenders!
184
+ Logger.processor.close
177
185
  end
178
186
 
179
187
  # Returns [SemanticLogger::Subscriber] a copy of the list of active
@@ -181,7 +189,7 @@ module SemanticLogger
181
189
  # Use SemanticLogger.add_appender and SemanticLogger.remove_appender
182
190
  # to manipulate the active appenders list
183
191
  def self.appenders
184
- Logger.processor.appenders.to_a
192
+ Logger.processor.appenders
185
193
  end
186
194
 
187
195
  # Flush all queued log entries disk, database, etc.
@@ -494,12 +502,12 @@ module SemanticLogger
494
502
  # I.e. Instead of logging messages in a separate thread for better performance,
495
503
  # log them using the current thread.
496
504
  def self.sync!
497
- @sync = true
505
+ Logger.sync!
498
506
  end
499
507
 
500
508
  # Running in synchronous mode?
501
509
  def self.sync?
502
- @sync
510
+ Logger.sync?
503
511
  end
504
512
 
505
513
  # Initial default Level for all new instances of SemanticLogger::Logger
@@ -22,6 +22,11 @@ module SemanticLogger
22
22
  # NOOP
23
23
  end
24
24
 
25
+ # Method called to log an event
26
+ def log(log)
27
+ raise NotImplementedError
28
+ end
29
+
25
30
  # Returns [SemanticLogger::Formatters::Default] default formatter for this subscriber.
26
31
  def default_formatter
27
32
  SemanticLogger::Formatters::Default.new
@@ -68,6 +73,11 @@ module SemanticLogger
68
73
  super(log) && (log.metric_only? ? metrics? : true)
69
74
  end
70
75
 
76
+ # Whether this appender is logging to stdout or stderror
77
+ def console_output?
78
+ false
79
+ end
80
+
71
81
  private
72
82
 
73
83
  # Initializer for Abstract Class SemanticLogger::Subscriber
@@ -10,7 +10,7 @@ module SemanticLogger
10
10
  def_delegator :@appenders, :close
11
11
  def_delegator :@appenders, :reopen
12
12
 
13
- # Allow the internal logger to be overridden from its default of STDERR
13
+ # Allow the internal logger to be overridden from its default of $stderr
14
14
  # Can be replaced with another Ruby logger or Rails logger, but never to
15
15
  # SemanticLogger::Logger itself since it is for reporting problems
16
16
  # while trying to log to the various appenders
@@ -20,11 +20,11 @@ module SemanticLogger
20
20
 
21
21
  # Internal logger for SemanticLogger
22
22
  # For example when an appender is not working etc..
23
- # By default logs to STDERR
23
+ # By default logs to $stderr
24
24
  def self.logger
25
25
  @logger ||=
26
26
  begin
27
- l = SemanticLogger::Appender::File.new(io: STDERR, level: :warn)
27
+ l = SemanticLogger::Appender::IO.new($stderr, level: :warn)
28
28
  l.name = name
29
29
  l
30
30
  end
@@ -32,8 +32,8 @@ module SemanticLogger
32
32
 
33
33
  attr_reader :appenders
34
34
 
35
- def initialize
36
- @appenders = Appenders.new(self.class.logger.dup)
35
+ def initialize(appenders = nil)
36
+ @appenders = appenders || Appenders.new(self.class.logger.dup)
37
37
  end
38
38
 
39
39
  def start
@@ -0,0 +1,34 @@
1
+ module SemanticLogger
2
+ module Test
3
+ # Logging class to captures all logging events in memory.
4
+ #
5
+ # Example:
6
+ #
7
+ # class UserTest < ActiveSupport::TestCase
8
+ # describe User do
9
+ # let(:capture_logger) { SemanticLogger::Test::CaptureLogEvents.new }
10
+ # let(:user) { User.new }
11
+ #
12
+ # it "logs message" do
13
+ # user.stub(:logger, capture_logger) do
14
+ # user.enable!
15
+ # end
16
+ # assert_equal "Hello World", capture_logger.events.last.message
17
+ # assert_equal :info, capture_logger.events.last.level
18
+ # end
19
+ # end
20
+ # end
21
+ class CaptureLogEvents < SemanticLogger::Subscriber
22
+ attr_accessor :events
23
+
24
+ # By default collect all log levels, and collect metric only log events.
25
+ def initialize(level: :trace, metrics: true)
26
+ super(level: level, metrics: true)
27
+ end
28
+
29
+ def log(log)
30
+ (@events ||= []) << log
31
+ end
32
+ end
33
+ end
34
+ end
@@ -32,29 +32,48 @@ module SemanticLogger
32
32
  end
33
33
  end
34
34
 
35
- SELF_PATTERN = File.join("lib", "semantic_logger")
36
-
37
- # Extract the backtrace leaving out the last few Semantic Logger lines.
35
+ # Extract the backtrace stripping off the leading semantic logger entries.
36
+ # Leaves all other system and gem path entries in place.
38
37
  def self.extract_backtrace(stack = caller)
39
- while (first = stack.first) && first.include?(SELF_PATTERN)
38
+ while (first = stack.first) && extract_path?(first)
40
39
  stack.shift
41
40
  end
42
41
  stack
43
42
  end
44
43
 
45
- # Strips off all gems and built-in ruby code paths from the top of the stack until application code is found.
44
+ def self.extract_paths
45
+ @extract_paths ||= %w[lib/semantic_logger lib/rails_semantic_logger]
46
+ end
47
+
48
+ # Whether this path should be excluded from any cleansed backtrace
49
+ def self.extract_path?(path)
50
+ extract_paths.any? { |exclude| path.include?(exclude) }
51
+ end
52
+
53
+ # Try to strip everything off of the supplied backtrace, until the first application stack entry is at the top.
54
+ # For example all leading gem paths and built-in ruby code paths are removed from the top.
55
+ # Once the first application entry is found, the remaining stack is returned.
46
56
  def self.strip_backtrace(stack = caller)
47
- while (first = stack.first) && system_path?(first)
57
+ while (first = stack.first) && (strip_path?(first) || extract_path?(first))
48
58
  stack.shift
49
59
  end
50
60
  stack
51
61
  end
52
62
 
53
- GEM_ROOT = File.expand_path("../../..", __dir__) + "/"
63
+ # Paths to exclude in the stripped backtrace
64
+ # Includes Gems and built-in Ruby code paths
65
+ def self.strip_paths
66
+ @strip_paths ||=
67
+ begin
68
+ paths = Gem.path | [Gem.default_dir]
69
+ paths << RbConfig::CONFIG["rubylibdir"]
70
+ paths
71
+ end
72
+ end
54
73
 
55
- def self.system_path?(path)
56
- path.start_with?(GEM_ROOT) ||
57
- path.start_with?(RbConfig::CONFIG["rubylibdir"])
74
+ # Whether this path should be excluded from any cleansed backtrace
75
+ def self.strip_path?(path)
76
+ strip_paths.any? { |exclude| path.start_with?(exclude) }
58
77
  end
59
78
  end
60
79
  end
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger
2
- VERSION = "4.7.2".freeze
2
+ VERSION = "4.10.0".freeze
3
3
  end
@@ -32,6 +32,10 @@ module SemanticLogger
32
32
  autoload :Minitest, "semantic_logger/reporters/minitest"
33
33
  end
34
34
 
35
+ module Test
36
+ autoload :CaptureLogEvents, "semantic_logger/test/capture_log_events"
37
+ end
38
+
35
39
  if defined?(JRuby)
36
40
  module JRuby
37
41
  autoload :GarbageCollectionLogger, "semantic_logger/jruby/garbage_collection_logger"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: semantic_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.7.2
4
+ version: 4.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-04 00:00:00.000000000 Z
11
+ date: 2022-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -24,9 +24,8 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
- description:
27
+ description:
28
28
  email:
29
- - reidmo@gmail.com
30
29
  executables: []
31
30
  extensions: []
32
31
  extra_rdoc_files: []
@@ -46,11 +45,13 @@ files:
46
45
  - lib/semantic_logger/appender/graylog.rb
47
46
  - lib/semantic_logger/appender/honeybadger.rb
48
47
  - lib/semantic_logger/appender/http.rb
48
+ - lib/semantic_logger/appender/io.rb
49
49
  - lib/semantic_logger/appender/kafka.rb
50
50
  - lib/semantic_logger/appender/mongodb.rb
51
51
  - lib/semantic_logger/appender/new_relic.rb
52
52
  - lib/semantic_logger/appender/rabbitmq.rb
53
53
  - lib/semantic_logger/appender/sentry.rb
54
+ - lib/semantic_logger/appender/sentry_ruby.rb
54
55
  - lib/semantic_logger/appender/splunk.rb
55
56
  - lib/semantic_logger/appender/splunk_http.rb
56
57
  - lib/semantic_logger/appender/syslog.rb
@@ -68,6 +69,7 @@ files:
68
69
  - lib/semantic_logger/formatters/default.rb
69
70
  - lib/semantic_logger/formatters/fluentd.rb
70
71
  - lib/semantic_logger/formatters/json.rb
72
+ - lib/semantic_logger/formatters/logfmt.rb
71
73
  - lib/semantic_logger/formatters/one_line.rb
72
74
  - lib/semantic_logger/formatters/raw.rb
73
75
  - lib/semantic_logger/formatters/signalfx.rb
@@ -87,13 +89,14 @@ files:
87
89
  - lib/semantic_logger/subscriber.rb
88
90
  - lib/semantic_logger/sync.rb
89
91
  - lib/semantic_logger/sync_processor.rb
92
+ - lib/semantic_logger/test/capture_log_events.rb
90
93
  - lib/semantic_logger/utils.rb
91
94
  - lib/semantic_logger/version.rb
92
- homepage: https://github.com/rocketjob/semantic_logger
95
+ homepage: https://logger.rocketjob.io
93
96
  licenses:
94
97
  - Apache-2.0
95
98
  metadata: {}
96
- post_install_message:
99
+ post_install_message:
97
100
  rdoc_options: []
98
101
  require_paths:
99
102
  - lib
@@ -101,15 +104,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
104
  requirements:
102
105
  - - ">="
103
106
  - !ruby/object:Gem::Version
104
- version: '2.3'
107
+ version: '2.5'
105
108
  required_rubygems_version: !ruby/object:Gem::Requirement
106
109
  requirements:
107
110
  - - ">="
108
111
  - !ruby/object:Gem::Version
109
112
  version: '0'
110
113
  requirements: []
111
- rubygems_version: 3.1.2
112
- signing_key:
114
+ rubygems_version: 3.3.3
115
+ signing_key:
113
116
  specification_version: 4
114
117
  summary: Feature rich logging framework, and replacement for existing Ruby & Rails
115
118
  loggers.