logasm 0.9.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +0 -22
- data/lib/logasm/adapters/stdout_adapter.rb +4 -1
- data/lib/logasm/adapters/stdout_json_adapter.rb +1 -1
- data/lib/logasm/adapters.rb +1 -7
- data/lib/logasm/utils.rb +1 -4
- data/lib/logasm.rb +1 -1
- data/logasm.gemspec +2 -1
- data/spec/adapters/stdout_json_adapter_spec.rb +1 -1
- data/spec/logasm_spec.rb +5 -34
- data/spec/utils_spec.rb +0 -4
- metadata +16 -11
- data/lib/logasm/adapters/logstash_adapter/formatter.rb +0 -16
- data/lib/logasm/adapters/logstash_adapter.rb +0 -31
- data/lib/logasm/adapters/rabbitmq_adapter.rb +0 -75
- data/spec/adapters/logstash_adapter/formatter_spec.rb +0 -20
- data/spec/adapters/logstash_adapter_spec.rb +0 -13
- data/spec/adapters/rabbitmq_adapter_spec.rb +0 -42
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb7e9b0c7705fcdbfd723890c04ccbb1f5aeb944
|
4
|
+
data.tar.gz: d554034a757044f6880a9e12a8e1fad0ff83d1fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bcf5f48bb51ed0f433919b8a64b0671510b17159daea4e5cded9a7666384de6468ed3ee47531fd9ee144425f5b7de46e6c657292c2e36823e404f65b99d1958
|
7
|
+
data.tar.gz: 5e6875c898fbb2f7239ab6c471aa1016f57ba789ecb9e32753e56039c9ab7751eb95ec6c54e08cdfd1794656dea6f3238d873e7e34334893f4f7b5cce9ace5bd
|
data/README.md
CHANGED
@@ -9,8 +9,6 @@ Logasm
|
|
9
9
|
Logasm.build(application_name, logger_config)
|
10
10
|
```
|
11
11
|
|
12
|
-
<b>application_name</b> is the name of the application. Only logstash logger will use this.
|
13
|
-
|
14
12
|
<b>logger_config</b> is a hash with logger types and their configuration.
|
15
13
|
|
16
14
|
#### Configuration
|
@@ -19,10 +17,6 @@ Logasm.build(application_name, logger_config)
|
|
19
17
|
loggers:
|
20
18
|
stdout:
|
21
19
|
level: 'debug'
|
22
|
-
logstash:
|
23
|
-
level: 'info'
|
24
|
-
host: 'localhost'
|
25
|
-
port: 5228
|
26
20
|
```
|
27
21
|
Supported log levels:
|
28
22
|
|
@@ -44,22 +38,6 @@ require 'logasm'
|
|
44
38
|
logasm = Logasm.build('myApp', stdout: nil)
|
45
39
|
```
|
46
40
|
|
47
|
-
Creating a new logstash logger
|
48
|
-
|
49
|
-
```ruby
|
50
|
-
require 'logasm'
|
51
|
-
|
52
|
-
logasm = Logasm.build('myApp', logstash: { host: "localhost", port: 5228 })
|
53
|
-
```
|
54
|
-
|
55
|
-
Creating a new logger that logs into stdout and logstash at the same time
|
56
|
-
|
57
|
-
```ruby
|
58
|
-
require 'logasm'
|
59
|
-
|
60
|
-
logasm = Logasm.build('myApp', { stdout: nil, logstash: { host: "localhost", port: 5228 }})
|
61
|
-
```
|
62
|
-
|
63
41
|
When no loggers are specified, it creates a stdout logger by default.
|
64
42
|
|
65
43
|
## Preprocessors
|
@@ -17,7 +17,10 @@ class Logasm
|
|
17
17
|
def log(level, metadata = {})
|
18
18
|
message = metadata[:message]
|
19
19
|
data = metadata.select { |key, value| key != :message }
|
20
|
-
log_data = [
|
20
|
+
log_data = [
|
21
|
+
message,
|
22
|
+
data.empty? ? nil : Oj.dump(data, mode: :compat, time_format: :ruby)
|
23
|
+
].compact.join(' ')
|
21
24
|
|
22
25
|
@logger.public_send level, log_data
|
23
26
|
end
|
data/lib/logasm/adapters.rb
CHANGED
@@ -4,13 +4,7 @@ class Logasm
|
|
4
4
|
|
5
5
|
def self.get(type, service_name, arguments)
|
6
6
|
adapter =
|
7
|
-
if type == '
|
8
|
-
require_relative 'adapters/rabbitmq_adapter'
|
9
|
-
RabbitmqAdapter
|
10
|
-
elsif type == 'logstash'
|
11
|
-
require_relative 'adapters/logstash_adapter'
|
12
|
-
LogstashAdapter
|
13
|
-
elsif type == 'stdout'
|
7
|
+
if type == 'stdout'
|
14
8
|
if arguments.fetch(:json, false)
|
15
9
|
require_relative 'adapters/stdout_json_adapter'
|
16
10
|
StdoutJsonAdapter
|
data/lib/logasm/utils.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
require 'socket'
|
2
1
|
require 'time'
|
3
2
|
|
4
3
|
class Logasm
|
5
4
|
module Utils
|
6
|
-
HOST = ::Socket.gethostname
|
7
5
|
DECIMAL_FRACTION_OF_SECOND = 3
|
8
6
|
|
9
7
|
# Build logstash json compatible event
|
@@ -36,8 +34,7 @@ class Logasm
|
|
36
34
|
|
37
35
|
def self.overwritable_params
|
38
36
|
{
|
39
|
-
:@timestamp => Time.now.utc.iso8601(DECIMAL_FRACTION_OF_SECOND)
|
40
|
-
host: HOST
|
37
|
+
:@timestamp => Time.now.utc.iso8601(DECIMAL_FRACTION_OF_SECOND)
|
41
38
|
}
|
42
39
|
end
|
43
40
|
|
data/lib/logasm.rb
CHANGED
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.
|
7
|
+
gem.version = '1.0.0'
|
8
8
|
gem.authors = ["Salemove"]
|
9
9
|
gem.email = ["support@salemove.com"]
|
10
10
|
gem.description = %q{It's logasmic}
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
|
|
18
18
|
|
19
19
|
gem.add_dependency 'inflecto'
|
20
20
|
gem.add_dependency 'lru_redux'
|
21
|
+
gem.add_dependency 'oj'
|
21
22
|
|
22
23
|
gem.add_development_dependency "bundler", "~> 1.3"
|
23
24
|
gem.add_development_dependency "rake"
|
@@ -12,7 +12,7 @@ describe Logasm::Adapters::StdoutJsonAdapter do
|
|
12
12
|
let(:adapter) { described_class.new(debug_level_code, service_name) }
|
13
13
|
let(:metadata) { {x: 'y'} }
|
14
14
|
let(:event) { {a: 'b', x: 'y'} }
|
15
|
-
let(:serialized_event) {
|
15
|
+
let(:serialized_event) { Oj.dump(event, mode: :compat, time_format: :ruby) }
|
16
16
|
let(:service_name) { 'my-service' }
|
17
17
|
|
18
18
|
before do
|
data/spec/logasm_spec.rb
CHANGED
@@ -20,26 +20,7 @@ describe Logasm do
|
|
20
20
|
described_class.build('test_service', stdout: {json: true})
|
21
21
|
end
|
22
22
|
|
23
|
-
it 'creates
|
24
|
-
expect(described_class).to receive(:new) do |adapters|
|
25
|
-
expect(adapters.count).to be(1)
|
26
|
-
expect(adapters.first).to be_a(described_class::Adapters::LogstashAdapter)
|
27
|
-
end
|
28
|
-
|
29
|
-
described_class.build('test_service', logstash: {host: 'localhost', port: 5228})
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'creates multiple loggers' do
|
33
|
-
expect(described_class).to receive(:new) do |adapters|
|
34
|
-
expect(adapters.count).to be(2)
|
35
|
-
expect(adapters.first).to be_a(described_class::Adapters::StdoutAdapter)
|
36
|
-
expect(adapters.last).to be_a(described_class::Adapters::LogstashAdapter)
|
37
|
-
end
|
38
|
-
|
39
|
-
described_class.build('test_service', stdout: nil, logstash: {host: 'localhost', port: 5228})
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'creates file logger when no loggers are specified' do
|
23
|
+
it 'creates stdout logger when no loggers are specified' do
|
43
24
|
expect(described_class).to receive(:new) do |adapters|
|
44
25
|
expect(adapters.count).to be(1)
|
45
26
|
expect(adapters.first).to be_a(described_class::Adapters::StdoutAdapter)
|
@@ -124,14 +105,9 @@ describe Logasm do
|
|
124
105
|
end
|
125
106
|
|
126
107
|
context 'log level queries' do
|
127
|
-
context 'when
|
108
|
+
context 'when adapter has debug level' do
|
128
109
|
let(:logger) do
|
129
|
-
described_class.build(
|
130
|
-
'test_service',
|
131
|
-
stdout: {level: 'debug'},
|
132
|
-
logstash: {level: 'info', host: '127.0.0.1', port: 5228 },
|
133
|
-
rabbitmq: {level: 'fatal'},
|
134
|
-
)
|
110
|
+
described_class.build('test_service', stdout: {level: 'debug'})
|
135
111
|
end
|
136
112
|
|
137
113
|
it 'responds true to debug? and higher levels' do
|
@@ -143,14 +119,9 @@ describe Logasm do
|
|
143
119
|
end
|
144
120
|
end
|
145
121
|
|
146
|
-
context 'when
|
122
|
+
context 'when adapter has info level' do
|
147
123
|
let(:logger) do
|
148
|
-
described_class.build(
|
149
|
-
'test_service',
|
150
|
-
rabbitmq: {level: 'info'},
|
151
|
-
stdout: {level: 'warn'},
|
152
|
-
logstash: {level: 'warn', host: '127.0.0.1', port: 5228 },
|
153
|
-
)
|
124
|
+
described_class.build('test_service', stdout: {level: 'info'})
|
154
125
|
end
|
155
126
|
|
156
127
|
it 'responds true to info? and higher levels' do
|
data/spec/utils_spec.rb
CHANGED
@@ -34,10 +34,6 @@ describe Logasm::Utils do
|
|
34
34
|
expect(event[:@timestamp]).to eq('2015-10-11T23:10:21.123Z')
|
35
35
|
end
|
36
36
|
|
37
|
-
it 'includes the host' do
|
38
|
-
expect(event[:host]).to be_a(String)
|
39
|
-
end
|
40
|
-
|
41
37
|
context 'when @timestamp provided' do
|
42
38
|
let(:metadata) { {message: 'test', :@timestamp => 'a timestamp'} }
|
43
39
|
|
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.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Salemove
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inflecto
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oj
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -111,9 +125,6 @@ files:
|
|
111
125
|
- benchmark/whitelisting.rb
|
112
126
|
- lib/logasm.rb
|
113
127
|
- lib/logasm/adapters.rb
|
114
|
-
- lib/logasm/adapters/logstash_adapter.rb
|
115
|
-
- lib/logasm/adapters/logstash_adapter/formatter.rb
|
116
|
-
- lib/logasm/adapters/rabbitmq_adapter.rb
|
117
128
|
- lib/logasm/adapters/stdout_adapter.rb
|
118
129
|
- lib/logasm/adapters/stdout_json_adapter.rb
|
119
130
|
- lib/logasm/null_logger.rb
|
@@ -125,9 +136,6 @@ files:
|
|
125
136
|
- lib/logasm/preprocessors/whitelist.rb
|
126
137
|
- lib/logasm/utils.rb
|
127
138
|
- logasm.gemspec
|
128
|
-
- spec/adapters/logstash_adapter/formatter_spec.rb
|
129
|
-
- spec/adapters/logstash_adapter_spec.rb
|
130
|
-
- spec/adapters/rabbitmq_adapter_spec.rb
|
131
139
|
- spec/adapters/stdout_adapter_spec.rb
|
132
140
|
- spec/adapters/stdout_json_adapter_spec.rb
|
133
141
|
- spec/logasm_spec.rb
|
@@ -162,9 +170,6 @@ signing_key:
|
|
162
170
|
specification_version: 4
|
163
171
|
summary: What description said
|
164
172
|
test_files:
|
165
|
-
- spec/adapters/logstash_adapter/formatter_spec.rb
|
166
|
-
- spec/adapters/logstash_adapter_spec.rb
|
167
|
-
- spec/adapters/rabbitmq_adapter_spec.rb
|
168
173
|
- spec/adapters/stdout_adapter_spec.rb
|
169
174
|
- spec/adapters/stdout_json_adapter_spec.rb
|
170
175
|
- spec/logasm_spec.rb
|
@@ -1,16 +0,0 @@
|
|
1
|
-
class Logasm
|
2
|
-
module Adapters
|
3
|
-
class LogstashAdapter
|
4
|
-
class Formatter < ::Logger::Formatter
|
5
|
-
def initialize(service_name)
|
6
|
-
@service_name = service_name
|
7
|
-
end
|
8
|
-
|
9
|
-
def call(level, _time, _progname, message)
|
10
|
-
event = Utils.build_event(message, level, @service_name)
|
11
|
-
"#{event.to_json}\n"
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'socket'
|
2
|
-
require_relative 'logstash_adapter/formatter'
|
3
|
-
require 'forwardable'
|
4
|
-
|
5
|
-
class Logasm
|
6
|
-
module Adapters
|
7
|
-
class LogstashAdapter
|
8
|
-
extend Forwardable
|
9
|
-
attr_reader :logger
|
10
|
-
|
11
|
-
def_delegators :@logger, :debug?, :info?, :warn?, :error?, :fatal?
|
12
|
-
|
13
|
-
def initialize(level, service, arguments = {})
|
14
|
-
host = arguments.fetch(:host)
|
15
|
-
port = arguments.fetch(:port)
|
16
|
-
device = UDPSocket.new.tap do |socket|
|
17
|
-
socket.connect(host, port)
|
18
|
-
end
|
19
|
-
|
20
|
-
@logger = Logger.new(device).tap do |logger|
|
21
|
-
logger.formatter = Formatter.new(service)
|
22
|
-
logger.level = level
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def log(level, data)
|
27
|
-
@logger.public_send level, data
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'bunny'
|
3
|
-
rescue LoadError => e
|
4
|
-
raise unless e.message =~ /bunny/
|
5
|
-
exception = e.exception('To use RabbitMQ adapter for logging, please install bunny!')
|
6
|
-
exception.set_backtrace(e.backtrace)
|
7
|
-
raise exception
|
8
|
-
end
|
9
|
-
|
10
|
-
class Logasm
|
11
|
-
module Adapters
|
12
|
-
class RabbitmqAdapter
|
13
|
-
CONFIGURATION_KEYS = [:host, :hosts, :user, :pass, :port]
|
14
|
-
|
15
|
-
attr_reader :bunny
|
16
|
-
|
17
|
-
def initialize(level, service_name, arguments = {})
|
18
|
-
config = arguments.select { |key, value| CONFIGURATION_KEYS.include?(key) }
|
19
|
-
@level = level
|
20
|
-
@service_name = service_name
|
21
|
-
@bunny = Bunny.new(config)
|
22
|
-
@queue_name = arguments.fetch(:queue, 'logstash-queue')
|
23
|
-
end
|
24
|
-
|
25
|
-
def log(level, metadata = {})
|
26
|
-
if meets_threshold?(level)
|
27
|
-
message = Utils.build_event(metadata, level, @service_name)
|
28
|
-
deliver_message message
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def debug?
|
33
|
-
meets_threshold?(:debug)
|
34
|
-
end
|
35
|
-
|
36
|
-
def info?
|
37
|
-
meets_threshold?(:info)
|
38
|
-
end
|
39
|
-
|
40
|
-
def warn?
|
41
|
-
meets_threshold?(:warn)
|
42
|
-
end
|
43
|
-
|
44
|
-
def error?
|
45
|
-
meets_threshold?(:error)
|
46
|
-
end
|
47
|
-
|
48
|
-
def fatal?
|
49
|
-
meets_threshold?(:fatal)
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
def meets_threshold?(level)
|
55
|
-
LOG_LEVELS.index(level.to_s) >= @level
|
56
|
-
end
|
57
|
-
|
58
|
-
def deliver_message(message)
|
59
|
-
exchange.publish(JSON.dump(message), {
|
60
|
-
content_type: 'application/json',
|
61
|
-
routing_key: @queue_name
|
62
|
-
})
|
63
|
-
rescue Bunny::ConnectionClosedError
|
64
|
-
end
|
65
|
-
|
66
|
-
def exchange
|
67
|
-
@exchange ||= begin
|
68
|
-
bunny.start
|
69
|
-
channel = bunny.create_channel
|
70
|
-
channel.default_exchange
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative '../../../lib/logasm/adapters/logstash_adapter/formatter'
|
3
|
-
|
4
|
-
describe Logasm::Adapters::LogstashAdapter::Formatter do
|
5
|
-
subject(:event) { formatter.call(severity, Time.now, nil, message) }
|
6
|
-
|
7
|
-
let(:formatter) { described_class.new(service_name) }
|
8
|
-
let(:service_name) { 'test_service' }
|
9
|
-
let(:severity) { 'INFO' }
|
10
|
-
let(:message) { {x: 'y'} }
|
11
|
-
|
12
|
-
it 'returns correct json' do
|
13
|
-
hash = JSON.parse(subject)
|
14
|
-
expect(hash['x']).to eq('y')
|
15
|
-
expect(hash['application']).to eq('test_service')
|
16
|
-
expect(hash['level']).to eq('info')
|
17
|
-
expect(hash['host']).to be_a(String)
|
18
|
-
expect(hash['@timestamp']).to match(/\d{4}-\d{2}-\d{2}T.*/)
|
19
|
-
end
|
20
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative '../../lib/logasm/adapters/logstash_adapter'
|
3
|
-
|
4
|
-
describe Logasm::Adapters::LogstashAdapter do
|
5
|
-
let(:logstash) { described_class.new(0, 'test_service', { host: 'localhost', port: '5228'}) }
|
6
|
-
let(:logger) { logstash.logger }
|
7
|
-
|
8
|
-
it 'delegates to the logger' do
|
9
|
-
expect(logger).to receive(:info).with(message: 'test')
|
10
|
-
|
11
|
-
logstash.log :info, message: 'test'
|
12
|
-
end
|
13
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative '../../lib/logasm/adapters/rabbitmq_adapter'
|
3
|
-
|
4
|
-
describe Logasm::Adapters::RabbitmqAdapter do
|
5
|
-
let(:adapter) do
|
6
|
-
described_class.new(log_level, 'TestService', {
|
7
|
-
host: 'localhost', user: 'guest', pass: 'guest', port: '5672'
|
8
|
-
})
|
9
|
-
end
|
10
|
-
let(:bunny) { double(start: nil, create_channel: channel) }
|
11
|
-
let(:channel) { double(default_exchange: exchange) }
|
12
|
-
let(:exchange) { spy }
|
13
|
-
|
14
|
-
before do
|
15
|
-
allow(Bunny).to receive(:new) { bunny }
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#log' do
|
19
|
-
context 'when logging a message' do
|
20
|
-
let(:log_level) { 0 }
|
21
|
-
|
22
|
-
it 'delegates to bunny' do
|
23
|
-
adapter.log :info, message: 'test'
|
24
|
-
|
25
|
-
expect(exchange).to have_received(:publish).with(
|
26
|
-
match(/{"@timestamp":"\d{4}-\d{2}-\d{2}T.*","host":".+","message":"test","application":"test_service","level":"info"}/),
|
27
|
-
{ content_type: 'application/json', routing_key: 'logstash-queue' }
|
28
|
-
)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'when log level is lower than threshold' do
|
33
|
-
let(:log_level) { 3 }
|
34
|
-
|
35
|
-
it 'does not delegate to bunny' do
|
36
|
-
adapter.log :info, message: 'test', a: 'b'
|
37
|
-
|
38
|
-
expect(exchange).to_not have_received(:publish)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|