semantic_logger 4.6.1 → 4.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +50 -20
- data/Rakefile +7 -7
- data/lib/semantic_logger/appender/async.rb +10 -9
- data/lib/semantic_logger/appender/async_batch.rb +7 -3
- data/lib/semantic_logger/appender/bugsnag.rb +43 -30
- data/lib/semantic_logger/appender/elasticsearch.rb +32 -14
- data/lib/semantic_logger/appender/elasticsearch_http.rb +4 -4
- data/lib/semantic_logger/appender/file.rb +249 -67
- data/lib/semantic_logger/appender/graylog.rb +12 -10
- data/lib/semantic_logger/appender/honeybadger.rb +3 -3
- data/lib/semantic_logger/appender/http.rb +20 -18
- data/lib/semantic_logger/appender/io.rb +68 -0
- data/lib/semantic_logger/appender/kafka.rb +9 -5
- data/lib/semantic_logger/appender/mongodb.rb +6 -6
- data/lib/semantic_logger/appender/new_relic.rb +2 -2
- data/lib/semantic_logger/appender/rabbitmq.rb +5 -5
- data/lib/semantic_logger/appender/sentry.rb +7 -7
- data/lib/semantic_logger/appender/sentry_ruby.rb +138 -0
- data/lib/semantic_logger/appender/splunk.rb +6 -5
- data/lib/semantic_logger/appender/splunk_http.rb +6 -6
- data/lib/semantic_logger/appender/syslog.rb +23 -15
- data/lib/semantic_logger/appender/tcp.rb +5 -5
- data/lib/semantic_logger/appender/udp.rb +2 -2
- data/lib/semantic_logger/appender/wrapper.rb +3 -2
- data/lib/semantic_logger/appender.rb +42 -36
- data/lib/semantic_logger/appenders.rb +34 -30
- data/lib/semantic_logger/base.rb +57 -27
- data/lib/semantic_logger/formatters/base.rb +9 -3
- data/lib/semantic_logger/formatters/color.rb +12 -8
- data/lib/semantic_logger/formatters/default.rb +18 -5
- data/lib/semantic_logger/formatters/fluentd.rb +3 -3
- data/lib/semantic_logger/formatters/json.rb +1 -1
- data/lib/semantic_logger/formatters/logfmt.rb +72 -0
- data/lib/semantic_logger/formatters/raw.rb +31 -7
- data/lib/semantic_logger/formatters/signalfx.rb +10 -9
- data/lib/semantic_logger/formatters/syslog.rb +8 -6
- data/lib/semantic_logger/formatters/syslog_cee.rb +8 -6
- data/lib/semantic_logger/formatters.rb +12 -13
- data/lib/semantic_logger/jruby/garbage_collection_logger.rb +4 -2
- data/lib/semantic_logger/levels.rb +9 -7
- data/lib/semantic_logger/log.rb +51 -61
- data/lib/semantic_logger/loggable.rb +8 -1
- data/lib/semantic_logger/logger.rb +19 -11
- data/lib/semantic_logger/metric/new_relic.rb +3 -3
- data/lib/semantic_logger/metric/signalfx.rb +3 -3
- data/lib/semantic_logger/metric/statsd.rb +7 -7
- data/lib/semantic_logger/processor.rb +9 -7
- data/lib/semantic_logger/reporters/minitest.rb +4 -4
- data/lib/semantic_logger/semantic_logger.rb +40 -19
- data/lib/semantic_logger/subscriber.rb +16 -5
- data/lib/semantic_logger/sync.rb +12 -0
- data/lib/semantic_logger/sync_processor.rb +43 -0
- data/lib/semantic_logger/test/capture_log_events.rb +34 -0
- data/lib/semantic_logger/utils.rb +32 -13
- data/lib/semantic_logger/version.rb +1 -1
- data/lib/semantic_logger.rb +27 -22
- metadata +15 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2ff7d4bcb345581f6ba19f834bb1bf2f921e97904f4b6236142b87af75a4526d
|
|
4
|
+
data.tar.gz: b1069d5360296c61c9796f630b1ec065eb1a0a7dc549778c66c5e26c90d0eadb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c5cfbac7dd8795b11cf79ed3fa9a4a086128d5ee030f0e9db40b75f30f58997c1674a4284b4b287939216bc8de83372e2b7fe00d73e7856c3cc6830310740f0d
|
|
7
|
+
data.tar.gz: 2150a809562247d771103a5d387ae5852ff5b0649a4f4288a7dd07a94420bafcfb6b36f3e21077fcc053c2d8bcaffe80a110ab65b52178fb612b7368fd79d87d
|
data/README.md
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
1
|
# Semantic Logger
|
|
2
|
-
[](https://rubygems.org/gems/semantic_logger) [](https://rubygems.org/gems/semantic_logger) [](https://github.com/reidmorrison/semantic_logger/actions?query=workflow%3Abuild) [](https://rubygems.org/gems/semantic_logger) [](http://opensource.org/licenses/Apache-2.0) 
|
|
3
3
|
|
|
4
4
|
Semantic Logger is a feature rich logging framework, and replacement for existing Ruby & Rails loggers.
|
|
5
5
|
|
|
6
|
-
* https://rocketjob.
|
|
6
|
+
* https://logger.rocketjob.io/
|
|
7
7
|
|
|
8
8
|
## Documentation
|
|
9
9
|
|
|
10
|
-
[Semantic Logger Guide](
|
|
11
|
-
|
|
12
|
-
[Reference Documentation](http://www.rubydoc.info/gems/semantic_logger/)
|
|
13
|
-
|
|
14
|
-
## Upgrading to Semantic Logger v4.4
|
|
15
|
-
|
|
16
|
-
With some forking frameworks it is necessary to call `reopen` after the fork. With v4.4 the
|
|
17
|
-
workaround for Ruby 2.5 crashes is no longer needed.
|
|
18
|
-
I.e. Please remove the following line if being called anywhere:
|
|
19
|
-
|
|
20
|
-
~~~ruby
|
|
21
|
-
SemanticLogger::Processor.instance.instance_variable_set(:@queue, Queue.new)
|
|
22
|
-
~~~
|
|
10
|
+
[Semantic Logger Guide](https://logger.rocketjob.io/)
|
|
23
11
|
|
|
24
12
|
## Logging Destinations
|
|
25
13
|
|
|
@@ -34,7 +22,7 @@ Logging to the following destinations are all supported "out-of-the-box":
|
|
|
34
22
|
* Splunk
|
|
35
23
|
* MongoDB
|
|
36
24
|
* Honeybadger
|
|
37
|
-
* Sentry
|
|
25
|
+
* Sentry (both with legacy `sentry-raven` and modern `sentry-ruby` gem)
|
|
38
26
|
* HTTP
|
|
39
27
|
* TCP
|
|
40
28
|
* UDP
|
|
@@ -50,7 +38,7 @@ handles saving log information to multiple destinations / appenders.
|
|
|
50
38
|
|
|
51
39
|
## Rails
|
|
52
40
|
|
|
53
|
-
When running Rails, use [rails_semantic_logger](http://github.com/
|
|
41
|
+
When running Rails, use [rails_semantic_logger](http://github.com/reidmorrison/rails_semantic_logger)
|
|
54
42
|
instead of Semantic Logger directly since it will automatically replace the Rails default logger with Semantic Logger.
|
|
55
43
|
|
|
56
44
|
## Rocket Job
|
|
@@ -72,8 +60,50 @@ and are therefore not automatically included by this gem:
|
|
|
72
60
|
- Splunk Appender: gem 'splunk-sdk-ruby'
|
|
73
61
|
- Elasticsearch Appender: gem 'elasticsearch'
|
|
74
62
|
- Kafka Appender: gem 'ruby-kafka'
|
|
63
|
+
- Legacy Sentry Appender: gem 'sentry-raven' (deprecated)
|
|
64
|
+
- Sentry Appender: gem 'sentry-ruby'
|
|
65
|
+
|
|
66
|
+
## Upgrading to Semantic Logger v4.9
|
|
67
|
+
|
|
68
|
+
These changes should not be noticeable by the majority of users of Semantic Logger, since
|
|
69
|
+
they are to the internal API. It is possible that advanced users may be using these internal
|
|
70
|
+
API's directly.
|
|
71
|
+
|
|
72
|
+
This does not affect any calls to the public api `SemanticLogger.add_appender`.
|
|
73
|
+
|
|
74
|
+
File and IO are now separate appenders. When creating the File appender explicitly, its arguments
|
|
75
|
+
have changed. For example, when requesting an IO stream, it needs to be changed from:
|
|
76
|
+
|
|
77
|
+
~~~ruby
|
|
78
|
+
SemanticLogger::Appender::File.new(io: $stderr)
|
|
79
|
+
~~~
|
|
80
|
+
to:
|
|
81
|
+
~~~ruby
|
|
82
|
+
SemanticLogger::Appender::IO.new($stderr)
|
|
83
|
+
~~~
|
|
84
|
+
|
|
85
|
+
Additionally, this needs to be changed from:
|
|
86
|
+
~~~ruby
|
|
87
|
+
SemanticLogger::Appender::File.new(file_name: "file.log")
|
|
88
|
+
~~~
|
|
89
|
+
to:
|
|
90
|
+
~~~ruby
|
|
91
|
+
SemanticLogger::Appender::File.new("file.log")
|
|
92
|
+
~~~
|
|
93
|
+
|
|
94
|
+
Rails Semantic Logger, if used, needs to be upgraded to v4.9 when upgrading to Semantic Logger v4.9.
|
|
95
|
+
|
|
96
|
+
## Upgrading to Semantic Logger v4.4
|
|
97
|
+
|
|
98
|
+
With some forking frameworks it is necessary to call `reopen` after the fork. With v4.4 the
|
|
99
|
+
workaround for Ruby 2.5 crashes is no longer needed.
|
|
100
|
+
I.e. Please remove the following line if being called anywhere:
|
|
101
|
+
|
|
102
|
+
~~~ruby
|
|
103
|
+
SemanticLogger::Processor.instance.instance_variable_set(:@queue, Queue.new)
|
|
104
|
+
~~~
|
|
75
105
|
|
|
76
|
-
##
|
|
106
|
+
## Upgrading to Semantic Logger v4.0
|
|
77
107
|
|
|
78
108
|
The following changes need to be made when upgrading to V4:
|
|
79
109
|
- Ruby V2.3 / JRuby V9.1 is now the minimum runtime version.
|
|
@@ -129,13 +159,13 @@ SemanticLogger.default_level = :trace
|
|
|
129
159
|
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)
|
|
130
160
|
~~~
|
|
131
161
|
|
|
132
|
-
If running rails, see: [Semantic Logger Rails](
|
|
162
|
+
If running rails, see: [Semantic Logger Rails](https://logger.rocketjob.io/rails.html)
|
|
133
163
|
|
|
134
164
|
## Author
|
|
135
165
|
|
|
136
166
|
[Reid Morrison](https://github.com/reidmorrison)
|
|
137
167
|
|
|
138
|
-
[Contributors](https://github.com/
|
|
168
|
+
[Contributors](https://github.com/reidmorrison/semantic_logger/graphs/contributors)
|
|
139
169
|
|
|
140
170
|
## Versioning
|
|
141
171
|
|
data/Rakefile
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require "rake/clean"
|
|
2
|
+
require "rake/testtask"
|
|
3
3
|
|
|
4
|
-
$LOAD_PATH.unshift File.expand_path(
|
|
5
|
-
require
|
|
4
|
+
$LOAD_PATH.unshift File.expand_path("lib", __dir__)
|
|
5
|
+
require "semantic_logger/version"
|
|
6
6
|
|
|
7
7
|
task :gem do
|
|
8
|
-
system
|
|
8
|
+
system "gem build semantic_logger.gemspec"
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
task publish: :gem do
|
|
12
12
|
system "git tag -a v#{SemanticLogger::VERSION} -m 'Tagging #{SemanticLogger::VERSION}'"
|
|
13
|
-
system
|
|
13
|
+
system "git push --tags"
|
|
14
14
|
system "gem push semantic_logger-#{SemanticLogger::VERSION}.gem"
|
|
15
15
|
system "rm semantic_logger-#{SemanticLogger::VERSION}.gem"
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
Rake::TestTask.new(:test) do |t|
|
|
19
|
-
t.pattern =
|
|
19
|
+
t.pattern = "test/**/*_test.rb"
|
|
20
20
|
t.verbose = true
|
|
21
21
|
t.warning = false
|
|
22
22
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "forwardable"
|
|
2
2
|
|
|
3
3
|
module SemanticLogger
|
|
4
4
|
module Appender
|
|
@@ -53,7 +53,7 @@ module SemanticLogger
|
|
|
53
53
|
# Re-open appender after a fork
|
|
54
54
|
def reopen
|
|
55
55
|
# Workaround CRuby crash on fork by recreating queue on reopen
|
|
56
|
-
# https://github.com/
|
|
56
|
+
# https://github.com/reidmorrison/semantic_logger/issues/103
|
|
57
57
|
@queue&.close
|
|
58
58
|
create_queue
|
|
59
59
|
|
|
@@ -73,6 +73,7 @@ module SemanticLogger
|
|
|
73
73
|
# Starts the worker thread if not running.
|
|
74
74
|
def thread
|
|
75
75
|
return @thread if @thread&.alive?
|
|
76
|
+
|
|
76
77
|
@thread = Thread.new { process }
|
|
77
78
|
end
|
|
78
79
|
|
|
@@ -115,21 +116,21 @@ module SemanticLogger
|
|
|
115
116
|
# This thread is designed to never go down unless the main thread terminates
|
|
116
117
|
# or the appender is closed.
|
|
117
118
|
Thread.current.name = logger.name
|
|
118
|
-
logger.trace
|
|
119
|
+
logger.trace "Async: Appender thread active"
|
|
119
120
|
begin
|
|
120
121
|
process_messages
|
|
121
|
-
rescue StandardError =>
|
|
122
|
+
rescue StandardError => e
|
|
122
123
|
# This block may be called after the file handles have been released by Ruby
|
|
123
124
|
begin
|
|
124
|
-
logger.error(
|
|
125
|
+
logger.error("Async: Restarting due to exception", e)
|
|
125
126
|
rescue StandardError
|
|
126
127
|
nil
|
|
127
128
|
end
|
|
128
129
|
retry
|
|
129
|
-
rescue Exception =>
|
|
130
|
+
rescue Exception => e
|
|
130
131
|
# This block may be called after the file handles have been released by Ruby
|
|
131
132
|
begin
|
|
132
|
-
logger.error(
|
|
133
|
+
logger.error("Async: Stopping due to fatal exception", e)
|
|
133
134
|
rescue StandardError
|
|
134
135
|
nil
|
|
135
136
|
end
|
|
@@ -137,7 +138,7 @@ module SemanticLogger
|
|
|
137
138
|
@thread = nil
|
|
138
139
|
# This block may be called after the file handles have been released by Ruby
|
|
139
140
|
begin
|
|
140
|
-
logger.trace(
|
|
141
|
+
logger.trace("Async: Thread has stopped")
|
|
141
142
|
rescue StandardError
|
|
142
143
|
nil
|
|
143
144
|
end
|
|
@@ -159,7 +160,7 @@ module SemanticLogger
|
|
|
159
160
|
break unless process_message(message)
|
|
160
161
|
end
|
|
161
162
|
end
|
|
162
|
-
logger.trace
|
|
163
|
+
logger.trace "Async: Queue Closed"
|
|
163
164
|
end
|
|
164
165
|
|
|
165
166
|
# Returns false when message processing should be stopped
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "concurrent"
|
|
2
2
|
|
|
3
3
|
module SemanticLogger
|
|
4
4
|
module Appender
|
|
@@ -42,7 +42,9 @@ module SemanticLogger
|
|
|
42
42
|
lag_threshold_s: lag_threshold_s
|
|
43
43
|
)
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
return if appender.respond_to?(:batch)
|
|
46
|
+
|
|
47
|
+
raise(ArgumentError, "#{appender.class.name} does not support batching. It must implement #batch")
|
|
46
48
|
end
|
|
47
49
|
|
|
48
50
|
# Add log message for processing.
|
|
@@ -62,6 +64,7 @@ module SemanticLogger
|
|
|
62
64
|
signal.wait(batch_seconds)
|
|
63
65
|
|
|
64
66
|
logs = []
|
|
67
|
+
messages = []
|
|
65
68
|
first = true
|
|
66
69
|
message_count = queue.length
|
|
67
70
|
message_count.times do
|
|
@@ -74,10 +77,11 @@ module SemanticLogger
|
|
|
74
77
|
first = false
|
|
75
78
|
end
|
|
76
79
|
else
|
|
77
|
-
|
|
80
|
+
messages << message
|
|
78
81
|
end
|
|
79
82
|
end
|
|
80
83
|
appender.batch(logs) if logs.size.positive?
|
|
84
|
+
messages.each { |message| process_message(message) }
|
|
81
85
|
signal.reset unless queue.size >= batch_size
|
|
82
86
|
end
|
|
83
87
|
end
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
begin
|
|
2
|
-
require
|
|
2
|
+
require "bugsnag"
|
|
3
3
|
rescue LoadError
|
|
4
|
-
raise LoadError
|
|
4
|
+
raise LoadError, 'Gem bugsnag is required for logging purposes. Please add the gem "bugsnag" to your Gemfile.'
|
|
5
5
|
end
|
|
6
6
|
|
|
7
7
|
# Send log messages to Bugsnag
|
|
@@ -30,7 +30,7 @@ module SemanticLogger
|
|
|
30
30
|
# Proc: Only include log messages where the supplied Proc returns true
|
|
31
31
|
# The Proc must return true or false.
|
|
32
32
|
def initialize(level: :error, **args, &block)
|
|
33
|
-
raise
|
|
33
|
+
raise "Bugsnag only supports :info, :warn, or :error log levels" unless %i[info warn error fatal].include?(level)
|
|
34
34
|
|
|
35
35
|
# Replace the Bugsnag logger so that we can identify its log messages and not forward them to Bugsnag
|
|
36
36
|
::Bugsnag.configure { |config| config.logger = SemanticLogger[Bugsnag] }
|
|
@@ -40,48 +40,61 @@ module SemanticLogger
|
|
|
40
40
|
|
|
41
41
|
# Returns [Hash] of parameters to send to Bugsnag.
|
|
42
42
|
def call(log, logger)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
hash = SemanticLogger::Formatters::Raw.new.call(log, logger)
|
|
44
|
+
hash.delete(:message) if hash[:exception] && (hash[:message] == hash[:exception][:message])
|
|
45
|
+
hash.delete(:time)
|
|
46
|
+
hash.delete(:level_index)
|
|
47
|
+
hash.delete(:exception)
|
|
48
|
+
hash[:file] = "#{hash[:file]}:#{hash.delete(:line)}" if hash.key?(:file)
|
|
49
|
+
hash
|
|
49
50
|
end
|
|
50
51
|
|
|
51
|
-
# Send an error notification to Bugsnag
|
|
52
52
|
def log(log)
|
|
53
53
|
# Ignore logs coming from Bugsnag itself
|
|
54
|
-
return false if log.name ==
|
|
54
|
+
return false if log.name == "Bugsnag"
|
|
55
55
|
|
|
56
56
|
# Send error messages as Runtime exceptions
|
|
57
|
-
exception =
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
log.exception.set_backtrace(log.backtrace) if !log.exception.backtrace && log.backtrace
|
|
61
|
-
log.exception
|
|
62
|
-
else
|
|
63
|
-
error = RuntimeError.new(log.message)
|
|
64
|
-
error.set_backtrace(log.backtrace) if log.backtrace
|
|
65
|
-
error
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
# For more documentation on the Bugsnag.notify method see:
|
|
69
|
-
# https://bugsnag.com/docs/notifiers/ruby#sending-handled-exceptions
|
|
70
|
-
::Bugsnag.notify(exception, formatter.call(log, self))
|
|
57
|
+
exception = extract_exception(log)
|
|
58
|
+
hash = formatter.call(log, self)
|
|
59
|
+
bugsnag_notify(exception, hash, log_level(log.level))
|
|
71
60
|
true
|
|
72
61
|
end
|
|
73
62
|
|
|
74
63
|
private
|
|
75
64
|
|
|
65
|
+
def bugsnag_notify(exception, hash, level)
|
|
66
|
+
if ::Bugsnag::VERSION.to_i >= 6
|
|
67
|
+
::Bugsnag.notify(exception) do |report|
|
|
68
|
+
report.severity = level
|
|
69
|
+
hash.each_pair { |key, value| report.add_tab(key, value) }
|
|
70
|
+
end
|
|
71
|
+
else
|
|
72
|
+
hash[:severity] = level
|
|
73
|
+
::Bugsnag.notify(exception, hash)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def extract_exception(log)
|
|
78
|
+
if log.exception
|
|
79
|
+
# Manually constructed Exception, without a backtrace.
|
|
80
|
+
log.exception.set_backtrace(log.backtrace) if !log.exception.backtrace && log.backtrace
|
|
81
|
+
return log.exception
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
error = RuntimeError.new(log.message)
|
|
85
|
+
error.set_backtrace(log.backtrace) if log.backtrace
|
|
86
|
+
error
|
|
87
|
+
end
|
|
88
|
+
|
|
76
89
|
# Bugsnag supports: error, warning or info
|
|
77
|
-
def log_level(
|
|
78
|
-
case
|
|
90
|
+
def log_level(level)
|
|
91
|
+
case level
|
|
79
92
|
when :error, :fatal
|
|
80
|
-
|
|
93
|
+
"error"
|
|
81
94
|
when :warn
|
|
82
|
-
|
|
95
|
+
"warning"
|
|
83
96
|
else
|
|
84
|
-
|
|
97
|
+
"info"
|
|
85
98
|
end
|
|
86
99
|
end
|
|
87
100
|
end
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
begin
|
|
2
|
-
require
|
|
2
|
+
require "elasticsearch"
|
|
3
3
|
rescue LoadError
|
|
4
|
-
raise LoadError
|
|
4
|
+
raise LoadError,
|
|
5
|
+
'Gem elasticsearch is required for logging to Elasticsearch. Please add the gem "elasticsearch" to your Gemfile.'
|
|
5
6
|
end
|
|
6
7
|
|
|
7
|
-
require
|
|
8
|
+
require "date"
|
|
8
9
|
|
|
9
10
|
# Forward all log messages to Elasticsearch.
|
|
10
11
|
#
|
|
@@ -17,7 +18,8 @@ require 'date'
|
|
|
17
18
|
module SemanticLogger
|
|
18
19
|
module Appender
|
|
19
20
|
class Elasticsearch < SemanticLogger::Subscriber
|
|
20
|
-
attr_accessor :url, :index, :date_pattern, :type, :client, :flush_interval, :timeout_interval, :batch_size,
|
|
21
|
+
attr_accessor :url, :index, :date_pattern, :type, :client, :flush_interval, :timeout_interval, :batch_size,
|
|
22
|
+
:elasticsearch_args
|
|
21
23
|
|
|
22
24
|
# Create Elasticsearch appender over persistent HTTP(S)
|
|
23
25
|
#
|
|
@@ -123,17 +125,17 @@ module SemanticLogger
|
|
|
123
125
|
# send_get_body_as [String]
|
|
124
126
|
# Specify the HTTP method to use for GET requests with a body.
|
|
125
127
|
# Default: 'GET'
|
|
126
|
-
def initialize(url:
|
|
127
|
-
index:
|
|
128
|
-
date_pattern:
|
|
129
|
-
type:
|
|
128
|
+
def initialize(url: "http://localhost:9200",
|
|
129
|
+
index: "semantic_logger",
|
|
130
|
+
date_pattern: "%Y.%m.%d",
|
|
131
|
+
type: "log",
|
|
130
132
|
level: nil,
|
|
131
133
|
formatter: nil,
|
|
132
134
|
filter: nil,
|
|
133
135
|
application: nil,
|
|
134
136
|
environment: nil,
|
|
135
137
|
host: nil,
|
|
136
|
-
|
|
138
|
+
data_stream: false,
|
|
137
139
|
**elasticsearch_args,
|
|
138
140
|
&block)
|
|
139
141
|
|
|
@@ -144,6 +146,7 @@ module SemanticLogger
|
|
|
144
146
|
@elasticsearch_args = elasticsearch_args.dup
|
|
145
147
|
@elasticsearch_args[:url] = url if url && !elasticsearch_args[:hosts]
|
|
146
148
|
@elasticsearch_args[:logger] = logger
|
|
149
|
+
@data_stream = data_stream
|
|
147
150
|
|
|
148
151
|
super(level: level, formatter: formatter, filter: filter, application: application, environment: environment, host: host, metrics: false, &block)
|
|
149
152
|
reopen
|
|
@@ -173,20 +176,35 @@ module SemanticLogger
|
|
|
173
176
|
private
|
|
174
177
|
|
|
175
178
|
def write_to_elasticsearch(messages)
|
|
176
|
-
bulk_result = @
|
|
177
|
-
|
|
179
|
+
bulk_result = if @data_stream
|
|
180
|
+
@client.bulk(index: index, body: messages)
|
|
181
|
+
else
|
|
182
|
+
@client.bulk(body: messages)
|
|
183
|
+
end
|
|
178
184
|
|
|
179
|
-
|
|
185
|
+
return unless bulk_result["errors"]
|
|
186
|
+
|
|
187
|
+
failed = bulk_result["items"].reject { |x| x["status"] == 201 }
|
|
180
188
|
logger.error("ElasticSearch: Write failed. Messages discarded. : #{failed}")
|
|
181
189
|
end
|
|
182
190
|
|
|
183
191
|
def bulk_index(log)
|
|
184
192
|
expanded_index_name = log.time.strftime("#{index}-#{date_pattern}")
|
|
185
|
-
|
|
193
|
+
if @data_stream
|
|
194
|
+
{"create" => {}}
|
|
195
|
+
else
|
|
196
|
+
{"index" => {"_index" => expanded_index_name, "_type" => type}}
|
|
197
|
+
end
|
|
186
198
|
end
|
|
187
199
|
|
|
188
200
|
def default_formatter
|
|
189
|
-
|
|
201
|
+
time_key = if @data_stream
|
|
202
|
+
"@timestamp"
|
|
203
|
+
else
|
|
204
|
+
:timestamp
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
SemanticLogger::Formatters::Raw.new(time_format: :iso_8601, time_key: time_key)
|
|
190
208
|
end
|
|
191
209
|
end
|
|
192
210
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require
|
|
1
|
+
require "date"
|
|
2
2
|
# Forward all log messages to Elasticsearch one at a time via a HTTP post.
|
|
3
3
|
#
|
|
4
4
|
# Note:
|
|
@@ -50,9 +50,9 @@ module SemanticLogger
|
|
|
50
50
|
# application: [String]
|
|
51
51
|
# Name of this application to appear in log messages.
|
|
52
52
|
# Default: SemanticLogger.application
|
|
53
|
-
def initialize(index:
|
|
54
|
-
type:
|
|
55
|
-
url:
|
|
53
|
+
def initialize(index: "semantic_logger",
|
|
54
|
+
type: "log",
|
|
55
|
+
url: "http://localhost:9200",
|
|
56
56
|
**http_args,
|
|
57
57
|
&block)
|
|
58
58
|
|