logasm 0.1.1 → 0.2.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: 200640bf85d51b2e5a0624792740b9a3b4282b32
4
- data.tar.gz: f957214552bdbdb5a6b0875e6937156a593de3a7
3
+ metadata.gz: dc85c64499598712bb88fae7042b01cb73c2f43f
4
+ data.tar.gz: a31ac1cb0489c75c740d52316a72e968672d83b4
5
5
  SHA512:
6
- metadata.gz: 7d22edc2348903f4893b2f79dc8c34a34279280468d5c0ae96b5e3a6204cf0ca6b270929e2d80c61e2abda5705a019ac131769339741968d326002e110ed852d
7
- data.tar.gz: 6f48d7208176093532f4d6773ea5488803099bad11ceb0e750248a0d0fa2d9b35bbc67322b80112ed816f96951ee427bc6c586bc9cfcfdaacb78b0f250124eef
6
+ metadata.gz: 99904786ff29d235df18e6a3752b55c450893ccb07a27d3add9045752fb4694c9ded0eb4f12c324a697daff0f805e1ff50b77c124158226ec5cacd3574350e0f
7
+ data.tar.gz: 8fbfe86ee157deb8f2da0b4e1fd8ca352c9e6de499d3ca915a5b763ca2b8ba704bf2935aa68ee19a8dd39c2626fe314c72a7a133b33f8de0d99892d48c13ec15
data/Gemfile.lock CHANGED
@@ -1,15 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- logasm (0.1.1)
4
+ logasm (0.2.0)
5
5
  inflecto
6
6
  logstash-event (~> 1.2)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ amq-protocol (2.0.0)
12
+ atomic (1.1.99)
13
+ bunny (1.6.3)
14
+ amq-protocol (>= 1.9.2)
11
15
  coderay (1.1.0)
12
16
  diff-lcs (1.2.5)
17
+ freddy (0.4.1)
18
+ bunny (= 1.6.3)
19
+ hamster (~> 1.0.1.pre.rc3)
20
+ symbolizer
21
+ thread (~> 0.2)
22
+ hamster (1.0.1.pre.rc3)
23
+ atomic (~> 1.1)
13
24
  inflecto (0.0.2)
14
25
  logstash-event (1.2.02)
15
26
  method_source (0.8.2)
@@ -18,25 +29,29 @@ GEM
18
29
  method_source (~> 0.8.1)
19
30
  slop (~> 3.4)
20
31
  rake (10.3.2)
21
- rspec (3.1.0)
22
- rspec-core (~> 3.1.0)
23
- rspec-expectations (~> 3.1.0)
24
- rspec-mocks (~> 3.1.0)
25
- rspec-core (3.1.2)
26
- rspec-support (~> 3.1.0)
27
- rspec-expectations (3.1.0)
32
+ rspec (3.3.0)
33
+ rspec-core (~> 3.3.0)
34
+ rspec-expectations (~> 3.3.0)
35
+ rspec-mocks (~> 3.3.0)
36
+ rspec-core (3.3.2)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-expectations (3.3.1)
28
39
  diff-lcs (>= 1.2.0, < 2.0)
29
- rspec-support (~> 3.1.0)
30
- rspec-mocks (3.1.0)
31
- rspec-support (~> 3.1.0)
32
- rspec-support (3.1.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-mocks (3.3.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-support (3.3.0)
33
45
  slop (3.6.0)
46
+ symbolizer (0.0.1)
47
+ thread (0.2.2)
34
48
 
35
49
  PLATFORMS
36
50
  ruby
37
51
 
38
52
  DEPENDENCIES
39
53
  bundler (~> 1.3)
54
+ freddy (~> 0.4.1)
40
55
  logasm!
41
56
  pry
42
57
  rake
@@ -1,5 +1,6 @@
1
1
  require 'socket'
2
2
  require 'logstash-event'
3
+ require_relative 'logstash_adapter/formatter'
3
4
 
4
5
  class Logasm
5
6
  module Adapters
@@ -0,0 +1,22 @@
1
+ require 'socket'
2
+
3
+ class Logasm
4
+ module Adapters
5
+ class RabbitmqAdapter
6
+ class MessageBuilder
7
+ HOST = ::Socket.gethostname
8
+
9
+ def initialize(service_name)
10
+ @service_name = service_name
11
+ end
12
+
13
+ def build_message(metadata, level)
14
+ metadata.merge({ application: @service_name,
15
+ level: level,
16
+ host: HOST,
17
+ :@timestamp => Time.now.utc.iso8601(3) })
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'bunny'
2
+
3
+ class Logasm
4
+ module Adapters
5
+ class RabbitmqAdapter
6
+ class Publisher
7
+ def initialize(queue, config)
8
+ connection = Bunny.new("amqp://#{config[:user]}:#{config[:pass]}@#{config[:host]}:#{config[:port]}")
9
+ connection.start
10
+ @exchange = connection.create_channel.default_exchange
11
+ @queue = queue
12
+ end
13
+
14
+ def publish(message)
15
+ @exchange.publish(message.to_json, :routing_key => @queue)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ begin
2
+ require 'freddy'
3
+ rescue LoadError => e
4
+ raise unless e.message =~ /freddy/
5
+ exception = e.exception('To use RabbitMQ adapter for logging, please install freddy!')
6
+ exception.set_backtrace(e.backtrace)
7
+ raise exception
8
+ end
9
+
10
+ require_relative 'rabbitmq_adapter/message_builder'
11
+
12
+ class Logasm
13
+ module Adapters
14
+ class RabbitmqAdapter
15
+ attr_reader :publisher
16
+
17
+ CONFIGURATION_KEYS = [:host, :user, :pass, :port]
18
+
19
+ def initialize(level, service, arguments = {})
20
+ config = arguments.select { |key, value| CONFIGURATION_KEYS.include?(key) }
21
+ logger = Logger.new(STDOUT)
22
+ @queue = arguments.fetch(:queue, 'logstash-queue')
23
+ @level = level
24
+
25
+ @message_builder = MessageBuilder.new(service)
26
+ @freddy = Freddy.build(logger, config)
27
+ end
28
+
29
+ def log(level, metadata = {})
30
+ if meets_threshold?(level)
31
+ message = @message_builder.build_message metadata, level
32
+ @freddy.deliver @queue, metadata
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def meets_threshold?(level)
39
+ LOG_LEVELS.index(level.to_s) >= @level
40
+ end
41
+ end
42
+ end
43
+ end
@@ -10,8 +10,8 @@ class Logasm
10
10
 
11
11
  def log(level, metadata = {})
12
12
  message = metadata[:message]
13
- metadata.delete(:message)
14
- log_data = [message, metadata.empty? ? nil : metadata.to_json].compact.join(' ')
13
+ data = metadata.select { |key, value| key != :message }
14
+ log_data = [message, data.empty? ? nil : data.to_json].compact.join(' ')
15
15
 
16
16
  @logger.public_send level, log_data
17
17
  end
@@ -3,6 +3,7 @@ class Logasm
3
3
  LOG_LEVELS = %w(debug info warn error fatal unknown).freeze
4
4
 
5
5
  def self.get(type, service_name, arguments)
6
+ require_relative "adapters/#{type.to_s}_adapter"
6
7
  adapter = const_get(Inflecto.camelize(type.to_s) + 'Adapter')
7
8
  level = LOG_LEVELS.index(arguments.fetch(:level, 'debug'))
8
9
  adapter.new(level, service_name, arguments)
data/lib/logasm.rb CHANGED
@@ -1,9 +1,6 @@
1
1
  require 'inflecto'
2
2
  require 'logger'
3
3
  require_relative 'logasm/adapters'
4
- require_relative 'logasm/adapters/stdout_adapter'
5
- require_relative 'logasm/adapters/logstash_adapter'
6
- require_relative 'logasm/adapters/logstash_adapter/formatter'
7
4
 
8
5
  class Logasm
9
6
  def self.build(service_name, loggers_config)
data/logasm.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = "logasm"
7
- gem.version = '0.1.1'
7
+ gem.version = '0.2.0'
8
8
  gem.authors = ["Salemove"]
9
9
  gem.email = ["support@salemove.com"]
10
10
  gem.description = %q{It's logasmic}
@@ -21,4 +21,5 @@ Gem::Specification.new do |gem|
21
21
 
22
22
  gem.add_development_dependency "bundler", "~> 1.3"
23
23
  gem.add_development_dependency "rake"
24
+ gem.add_development_dependency "freddy", "~> 0.4.1"
24
25
  end
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require_relative '../../../lib/logasm/adapters/logstash_adapter/formatter'
2
3
 
3
4
  describe Logasm::Adapters::LogstashAdapter::Formatter do
4
5
  subject(:event) { JSON.parse(formatter.call(severity, time, nil, message)) }
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require_relative '../../lib/logasm/adapters/logstash_adapter'
2
3
 
3
4
  describe Logasm::Adapters::LogstashAdapter do
4
5
  let(:logstash) { described_class.new(0, 'test_service', { host: 'localhost', port: '5228'}) }
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+ require_relative '../../../lib/logasm/adapters/rabbitmq_adapter/message_builder'
3
+
4
+ describe Logasm::Adapters::RabbitmqAdapter::MessageBuilder do
5
+ subject { message_builder.build_message(metadata, level) }
6
+
7
+ let(:message_builder) { described_class.new(service_name) }
8
+ let(:service_name) { 'test_service' }
9
+ let(:level) { :info }
10
+ let(:metadata) { {message: 'test'} }
11
+
12
+ it 'adds necessary arguments' do
13
+ expect(subject[:message]).to eq(metadata[:message])
14
+ expect(subject[:application]).to eq(service_name)
15
+ expect(subject[:level]).to eq(level)
16
+ expect(subject).to have_key(:host)
17
+ expect(subject).to have_key(:@timestamp)
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/logasm/adapters/rabbitmq_adapter'
3
+
4
+ describe Logasm::Adapters::RabbitmqAdapter do
5
+ let(:publisher) { adapter.publisher }
6
+
7
+ before do
8
+ Logasm::Adapters::RabbitmqAdapter::Publisher.any_instance.stub(:initialize).and_return(double)
9
+ end
10
+
11
+ describe '#log' do
12
+ context 'when logging a message' do
13
+ let(:adapter) { described_class.new(0, nil, { host: 'localhost',
14
+ user: 'guest',
15
+ pass: 'guest',
16
+ port: '5672' }
17
+ ) }
18
+
19
+ it 'delegates to publisher' do
20
+ expect(publisher).to receive(:publish)
21
+
22
+ adapter.log :info, message: 'test'
23
+ end
24
+ end
25
+
26
+ context 'when log level is lower than threshold' do
27
+ let(:adapter) { described_class.new(3, nil, { host: 'localhost',
28
+ user: 'guest',
29
+ pass: 'guest',
30
+ port: '5672' }
31
+ ) }
32
+
33
+ it 'does not delegate to freddy' do
34
+ expect(publisher).not_to receive(:publish)
35
+ adapter.log :info, message: 'test'
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'logasm'
2
+ require_relative '../../lib/logasm/adapters/stdout_adapter'
3
3
 
4
4
  describe Logasm::Adapters::StdoutAdapter do
5
5
  it 'creates a stdout logger' do
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  RSpec.configure do |config|
2
- config.treat_symbols_as_metadata_keys_with_true_values = true
3
2
  config.run_all_when_everything_filtered = true
4
3
  config.filter_run :focus
5
4
  config.order = 'random'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salemove
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-11 00:00:00.000000000 Z
11
+ date: 2015-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: freddy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.1
69
83
  description: It's logasmic
70
84
  email:
71
85
  - support@salemove.com
@@ -85,10 +99,15 @@ files:
85
99
  - lib/logasm/adapters.rb
86
100
  - lib/logasm/adapters/logstash_adapter.rb
87
101
  - lib/logasm/adapters/logstash_adapter/formatter.rb
102
+ - lib/logasm/adapters/rabbitmq_adapter.rb
103
+ - lib/logasm/adapters/rabbitmq_adapter/message_builder.rb
104
+ - lib/logasm/adapters/rabbitmq_adapter/publisher.rb
88
105
  - lib/logasm/adapters/stdout_adapter.rb
89
106
  - logasm.gemspec
90
107
  - spec/adapters/logstash_adapter/formatter_spec.rb
91
108
  - spec/adapters/logstash_adapter_spec.rb
109
+ - spec/adapters/rabbitmq_adapter/message_builder_spec.rb
110
+ - spec/adapters/rabbitmq_adapter_spec.rb
92
111
  - spec/adapters/stdout_adapter_spec.rb
93
112
  - spec/logasm_spec.rb
94
113
  - spec/spec_helper.rb
@@ -119,6 +138,8 @@ summary: What description said
119
138
  test_files:
120
139
  - spec/adapters/logstash_adapter/formatter_spec.rb
121
140
  - spec/adapters/logstash_adapter_spec.rb
141
+ - spec/adapters/rabbitmq_adapter/message_builder_spec.rb
142
+ - spec/adapters/rabbitmq_adapter_spec.rb
122
143
  - spec/adapters/stdout_adapter_spec.rb
123
144
  - spec/logasm_spec.rb
124
145
  - spec/spec_helper.rb