semantic_logger 3.0.0 → 3.0.1

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: 16e4711a6003653a8d7cf1668fe3180e10493e43
4
- data.tar.gz: c0266b7ebb30904275543fc083e553260bead056
3
+ metadata.gz: 50213eafcd6560b8240844cfb686a2a954537091
4
+ data.tar.gz: d6df1d6905b315ccef4915cf59b819e62d2697bf
5
5
  SHA512:
6
- metadata.gz: cdbf75bce867ba9fae2a524447d62f65093fc6ada2cb2214b67f599b5ee77b3d997ffb29e3e6db34efb09fdab24b8cc662700b664a58835aa1d88763f4d893b7
7
- data.tar.gz: 3eff413a5f6a09af3f4aa84b7eaf717bd10cedff41d49202d0882442ba53e3519aff4b88c37a06bcc5703e9b744c2dbbc6f5533948c24cfd03fd9bc90905412e
6
+ metadata.gz: 048295e8e5a07f91ffe44a2108cb12135fbcfb25d604ad3ec0656e41cc0435689dc215de8bd0c994b81ec1fcf4e850e46d7733c7e2a5676e3b2e92ecc42971e5
7
+ data.tar.gz: ce6f018889578dc8a44855e2bfc528d939cf5b8c0bd4872c41f55b5e1537adb8e99cd231c86e4829164bea1a5f1c2b728c2df52c566d79d73bcbf7bf578370b5
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  # Load AwesomePrint if available
2
3
  begin
3
4
  require 'awesome_print'
@@ -18,8 +18,10 @@ class SemanticLogger::Appender::Elasticsearch < SemanticLogger::Appender::Http
18
18
  #
19
19
  # Parameters:
20
20
  # index: [String]
21
- # Index to store the logs in Elasticsearch.
22
- # Default: 'semantic_logger-YYYY.MM.DD'
21
+ # Prefix of the index to store the logs in Elasticsearch.
22
+ # The final index appends the date so that indexes are used per day.
23
+ # I.e. The final index will look like 'semantic_logger-YYYY.MM.DD'
24
+ # Default: 'semantic_logger'
23
25
  #
24
26
  # type: [String]
25
27
  # Document type to associate with logs when they are written.
@@ -36,18 +38,24 @@ class SemanticLogger::Appender::Elasticsearch < SemanticLogger::Appender::Http
36
38
  # The Proc must return true or false.
37
39
  def initialize(options, &block)
38
40
  options = options.dup
39
- @index = options.delete(:index) || "semantic_logger-#{Date.today.to_s.gsub('-', '.')}"
41
+ @index = options.delete(:index) || "semantic_logger"
40
42
  @type = options.delete(:type) || 'log'
41
43
  options[:url] ||= 'http://localhost:9200'
42
44
 
43
45
  super(options, &block)
46
+ end
47
+
48
+ # Log to the index for today
49
+ def log(log)
50
+ return false if (level_index > (log.level_index || 0)) ||
51
+ !include_message?(log) # Filtered out?
44
52
 
45
- @request_uri = "#{@index}/#{@type}"
53
+ post(formatter.call(log, self), "#{index}-#{log.time.strftime('%Y.%m.%d')}/#{type}")
46
54
  end
47
55
 
48
- # Deletes all log data captured for this index
49
- def delete_all
50
- delete(index)
56
+ # Deletes all log data captured for a day
57
+ def delete_all(date = Date.today)
58
+ "#{index}-#{date.strftime('%Y.%m.%d')}/#{type}"
51
59
  end
52
60
 
53
61
  end
@@ -21,7 +21,7 @@ require 'json'
21
21
  # SemanticLogger.add_appender(appender)
22
22
  class SemanticLogger::Appender::Http < SemanticLogger::Appender::Base
23
23
  attr_accessor :username, :application, :host, :compress, :header
24
- attr_reader :http, :url, :server, :port, :request_uri, :ssl_options
24
+ attr_reader :http, :url, :server, :port, :path, :ssl_options
25
25
 
26
26
  # Create HTTP(S) log appender
27
27
  #
@@ -90,11 +90,13 @@ class SemanticLogger::Appender::Http < SemanticLogger::Appender::Base
90
90
  uri = URI.parse(@url)
91
91
  (@ssl_options ||= {})[:use_ssl] = true if uri.scheme == 'https'
92
92
 
93
- @server = uri.host
94
- @port = uri.port
95
- @username = uri.user if !@username && uri.user
96
- @password = uri.password if !@password && uri.password
97
- @request_uri = uri.request_uri
93
+ @server = uri.host
94
+ raise(ArgumentError, "Invalid format for :url: #{@url.inspect}. Should be similar to: 'http://hostname:port/path'") unless @url
95
+
96
+ @port = uri.port
97
+ @username = uri.user if !@username && uri.user
98
+ @password = uri.password if !@password && uri.password
99
+ @path = uri.request_uri
98
100
 
99
101
  reopen
100
102
 
@@ -132,20 +134,20 @@ class SemanticLogger::Appender::Http < SemanticLogger::Appender::Base
132
134
  end
133
135
 
134
136
  # HTTP Post
135
- def post(body, request_uri = nil)
136
- request = Net::HTTP::Post.new(request_uri || @request_uri, @header)
137
+ def post(body, request_uri = path)
138
+ request = Net::HTTP::Post.new(request_uri, @header)
137
139
  process_request(request, body)
138
140
  end
139
141
 
140
142
  # HTTP Put
141
- def put(body, request_uri = nil)
142
- request = Net::HTTP::Put.new(request_uri || @request_uri, @header)
143
+ def put(body, request_uri = path)
144
+ request = Net::HTTP::Put.new(request_uri, @header)
143
145
  process_request(request, body)
144
146
  end
145
147
 
146
148
  # HTTP Delete
147
- def delete(request_uri = nil)
148
- request = Net::HTTP::Delete.new(request_uri || @request_uri, @header)
149
+ def delete(request_uri = path)
150
+ request = Net::HTTP::Delete.new(request_uri, @header)
149
151
  process_request(request)
150
152
  end
151
153
 
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  # Splunk log appender.
2
3
  #
3
4
  # Use the newer, faster and more complete JSON over HTTP interface for Splunk.
@@ -1,3 +1,3 @@
1
1
  module SemanticLogger #:nodoc
2
- VERSION = '3.0.0'
2
+ VERSION = '3.0.1'
3
3
  end
@@ -0,0 +1,72 @@
1
+ require_relative '../test_helper'
2
+
3
+ # Unit Test for SemanticLogger::Appender::Elasticsearch
4
+ module Appender
5
+ class ElasticsearchTest < Minitest::Test
6
+ response_mock = Struct.new(:code, :body)
7
+
8
+ describe SemanticLogger::Appender::Elasticsearch do
9
+ before do
10
+ @appender = SemanticLogger::Appender::Elasticsearch.new(
11
+ url: 'http://localhost:9200'
12
+ )
13
+ @message = 'AppenderElasticsearchTest log message'
14
+ end
15
+
16
+ it 'logs to daily indexes' do
17
+ index = nil
18
+ @appender.stub(:post, -> json, ind { index = ind }) do
19
+ @appender.info @message
20
+ end
21
+ assert_equal "semantic_logger-#{Time.now.utc.strftime('%Y.%m.%d')}/log", index
22
+ end
23
+
24
+ SemanticLogger::LEVELS.each do |level|
25
+ it "send #{level}" do
26
+ request = nil
27
+ @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do
28
+ @appender.send(level, @message)
29
+ end
30
+ message = JSON.parse(request.body)
31
+ assert_equal @message, message['message']
32
+ assert_equal level.to_s, message['level']
33
+ refute message['exception']
34
+ end
35
+
36
+ it "sends #{level} exceptions" do
37
+ exc = nil
38
+ begin
39
+ Uh oh
40
+ rescue Exception => e
41
+ exc = e
42
+ end
43
+ request = nil
44
+ @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do
45
+ @appender.send(level, 'Reading File', exc)
46
+ end
47
+ hash = JSON.parse(request.body)
48
+ assert 'Reading File', hash['message']
49
+ assert exception = hash['exception']
50
+ assert 'NameError', exception['name']
51
+ assert 'undefined local variable or method', exception['message']
52
+ assert_equal level.to_s, hash['level']
53
+ assert exception['stack_trace'].first.include?(__FILE__), exception
54
+ end
55
+
56
+ it "sends #{level} custom attributes" do
57
+ request = nil
58
+ @appender.http.stub(:request, -> r { request = r; response_mock.new('200', 'ok') }) do
59
+ @appender.send(level, @message, {key1: 1, key2: 'a'})
60
+ end
61
+ message = JSON.parse(request.body)
62
+ assert_equal @message, message['message']
63
+ assert_equal level.to_s, message['level']
64
+ refute message['stack_trace']
65
+ assert_equal(1, message['key1'], message)
66
+ assert_equal('a', message['key2'], message)
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -40,9 +40,6 @@ module Appender
40
40
  end
41
41
  body = decompress_data(request.body)
42
42
  hash = JSON.parse(body)
43
- # assert message['event']['message'].include?('Reading File -- NameError: undefined local variable or method'), message['message']
44
- # assert_equal level.to_s, message['event']['level']
45
- # assert message['event']['backtrace'].include?(__FILE__), message['event']['backtrace']
46
43
  assert 'Reading File', hash['message']
47
44
  assert exception = hash['event']['exception']
48
45
  assert 'NameError', exception['name']
@@ -60,7 +57,7 @@ module Appender
60
57
  message = JSON.parse(body)
61
58
  assert_equal @message, message['event']['message']
62
59
  assert_equal level.to_s, message['event']['level']
63
- refute message['event']['backtrace']
60
+ refute message['event']['stack_trace']
64
61
  assert_equal(1, message['event']['key1'], message)
65
62
  assert_equal('a', message['event']['key2'], message)
66
63
  end
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: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-09 00:00:00.000000000 Z
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.0'
27
- - !ruby/object:Gem::Dependency
28
- name: json
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.7'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.7'
41
27
  description: Next generation logging system for Ruby to support highly concurrent,
42
28
  high throughput, low latency enterprise systems
43
29
  email:
@@ -72,6 +58,7 @@ files:
72
58
  - lib/semantic_logger/semantic_logger.rb
73
59
  - lib/semantic_logger/version.rb
74
60
  - test/appender/bugsnag_test.rb
61
+ - test/appender/elasticsearch_test.rb
75
62
  - test/appender/file_test.rb
76
63
  - test/appender/graylog_test.rb
77
64
  - test/appender/http_test.rb
@@ -107,12 +94,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
94
  version: '0'
108
95
  requirements: []
109
96
  rubyforge_project:
110
- rubygems_version: 2.5.1
97
+ rubygems_version: 2.4.8
111
98
  signing_key:
112
99
  specification_version: 4
113
100
  summary: Scalable, next generation enterprise logging for Ruby
114
101
  test_files:
115
102
  - test/appender/bugsnag_test.rb
103
+ - test/appender/elasticsearch_test.rb
116
104
  - test/appender/file_test.rb
117
105
  - test/appender/graylog_test.rb
118
106
  - test/appender/http_test.rb