lenjador 1.4.0 → 2.0.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
- SHA1:
3
- metadata.gz: ecb260502893758ca056faa11a5e4b5347d450c1
4
- data.tar.gz: f3dab9f737b556ae47c9d2095b07076963ee31b6
2
+ SHA256:
3
+ metadata.gz: 33c13138df41134b3d17319ff6d02d86e5b261ab5b45eaf901ec35e19bb0667b
4
+ data.tar.gz: 963d753a7188d8ce556b2f03236db1a18e9428e8d5f71d07890e039ea6dcdf51
5
5
  SHA512:
6
- metadata.gz: 19d983bb91596e4e4c2da6c88d98ba765c3d3e5dceb579b9fb6598868935ca0392c18293b3ac4cc5fb7a018acb19761cf00352f76f8c1c3fda066a04be66cb8a
7
- data.tar.gz: 9dabc7e541b8f41bc03e0ec9cf899771bd2d625ea22733d986aed483fe0be89cb6195a5b94f593ae141df1f9f9e452368b4ad3581b343470ffeb42157947aeeb
6
+ metadata.gz: 77ea852dc496c9c973954da2d83de33811a6c7e37b7028ca1596aece01009944e0d476f5e8e32790c380735338283d5ee62e634f0555da7aa58b8a99a73bdb91
7
+ data.tar.gz: 367a1528be10b7d2cde8f9d8cbe16080ebdf90a9d9e6c0670b42ced776018c1d1871b904434f1b6d11e18eeebbd993365573e5ec8acb21f8cb6d04eb3563f585
data/.rubocop.yml CHANGED
@@ -36,3 +36,6 @@ Style/SafeNavigation:
36
36
 
37
37
  Performance/RegexpMatch:
38
38
  Enabled: no
39
+
40
+ Style/SingleLineMethods:
41
+ Enabled: no
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.3
4
3
  - 2.4
5
4
  - 2.5
6
5
  - jruby
data/lenjador.gemspec CHANGED
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = RUBY_PLATFORM =~ /java/ ? 'lenjador-jruby' : 'lenjador'
8
- gem.version = '1.4.0'
8
+ gem.version = '2.0.0'
9
9
  gem.authors = ['Salemove']
10
10
  gem.email = ['support@salemove.com']
11
11
  gem.description = "It's lenjadoric"
@@ -27,7 +27,7 @@ Gem::Specification.new do |gem|
27
27
  end
28
28
 
29
29
  gem.add_development_dependency 'benchmark-ips'
30
- gem.add_development_dependency 'bundler', '~> 1.3'
30
+ gem.add_development_dependency 'bundler'
31
31
  gem.add_development_dependency 'pry'
32
32
  gem.add_development_dependency 'rake'
33
33
  gem.add_development_dependency 'rspec'
data/lib/lenjador.rb CHANGED
@@ -6,60 +6,67 @@ require_relative 'lenjador/utils'
6
6
  require_relative 'lenjador/null_logger'
7
7
  require_relative 'lenjador/preprocessors'
8
8
 
9
- LOG_LEVEL_QUERY_METHODS = %i[debug? info? warn? error? fatal?].freeze
10
-
11
9
  class Lenjador
12
- def self.build(service_name, loggers_config, preprocessors_config = {})
13
- loggers_config ||= {stdout: nil}
10
+ Severity = ::Logger::Severity
11
+ SEV_LABEL = %w[debug info warn error fatal any].freeze
12
+
13
+ def self.build(service_name, logger_config, preprocessors_config = {})
14
+ logger_config ||= {}
15
+
14
16
  preprocessors = preprocessors_config.map do |type, arguments|
15
17
  Preprocessors.get(type.to_s, arguments || {})
16
18
  end
17
- adapters = loggers_config.map do |type, arguments|
18
- Adapters.get(type.to_s, service_name, arguments || {})
19
- end
20
- new(adapters, preprocessors)
19
+ adapter = Adapters.get(service_name, logger_config)
20
+ level = SEV_LABEL.index(logger_config.fetch(:level, 'debug'))
21
+
22
+ new(adapter, level, preprocessors)
21
23
  end
22
24
 
23
- def initialize(adapters, preprocessors)
24
- @adapters = adapters
25
+ def initialize(adapter, level, preprocessors)
26
+ @adapter = adapter
27
+ @level = level
25
28
  @preprocessors = preprocessors
26
29
  end
27
30
 
28
31
  def debug(*args, &block)
29
- log :debug, *args, &block
32
+ log(Severity::DEBUG, *args, &block)
30
33
  end
31
34
 
32
35
  def info(*args, &block)
33
- log :info, *args, &block
36
+ log(Severity::INFO, *args, &block)
34
37
  end
35
38
 
36
39
  def warn(*args, &block)
37
- log :warn, *args, &block
40
+ log(Severity::WARN, *args, &block)
38
41
  end
39
42
 
40
43
  def error(*args, &block)
41
- log :error, *args, &block
44
+ log(Severity::ERROR, *args, &block)
42
45
  end
43
46
 
44
47
  def fatal(*args, &block)
45
- log :fatal, *args, &block
48
+ log(Severity::FATAL, *args, &block)
46
49
  end
47
50
 
48
- LOG_LEVEL_QUERY_METHODS.each do |method|
49
- define_method(method) do
50
- @adapters.any? { |adapter| adapter.public_send(method) }
51
- end
52
- end
51
+ def debug?; @level <= Severity::DEBUG; end
52
+
53
+ def info?; @level <= Severity::INFO; end
54
+
55
+ def warn?; @level <= Severity::WARN; end
56
+
57
+ def error?; @level <= Severity::ERROR; end
58
+
59
+ def fatal?; @level <= Severity::FATAL; end
53
60
 
54
61
  private
55
62
 
56
63
  def log(level, *args, &block)
64
+ return true if level < @level
65
+
57
66
  data = parse_log_data(*args, &block)
58
67
  processed_data = preprocess(data)
59
68
 
60
- @adapters.each do |adapter|
61
- adapter.log(level, processed_data)
62
- end
69
+ @adapter.log(level, processed_data)
63
70
  end
64
71
 
65
72
  def preprocess(data)
@@ -2,21 +2,16 @@
2
2
 
3
3
  class Lenjador
4
4
  module Adapters
5
- LOG_LEVELS = %i[debug info warn error fatal unknown].freeze
6
-
7
- def self.get(type, service_name, arguments)
8
- raise "Unsupported logger: #{type}" if type != 'stdout'
9
-
5
+ def self.get(service_name, config)
10
6
  adapter =
11
- if arguments.fetch(:json, false)
7
+ if config.fetch(:json, false)
12
8
  require_relative 'adapters/stdout_json_adapter'
13
9
  StdoutJsonAdapter
14
10
  else
15
11
  require_relative 'adapters/stdout_adapter'
16
12
  StdoutAdapter
17
13
  end
18
- level = LOG_LEVELS.index(arguments.fetch(:level, :debug).to_sym)
19
- adapter.new(level, service_name, arguments)
14
+ adapter.new(service_name)
20
15
  end
21
16
  end
22
17
  end
@@ -5,15 +5,10 @@ require 'forwardable'
5
5
  class Lenjador
6
6
  module Adapters
7
7
  class StdoutAdapter
8
- extend Forwardable
9
-
10
8
  attr_reader :logger
11
9
 
12
- def_delegators :@logger, :debug?, :info?, :warn?, :error?, :fatal?
13
-
14
- def initialize(level, *)
10
+ def initialize(_service_name)
15
11
  @logger = Logger.new(STDOUT)
16
- @logger.level = level
17
12
  end
18
13
 
19
14
  def log(level, metadata = {})
@@ -24,7 +19,7 @@ class Lenjador
24
19
  data.empty? ? nil : Utils.generate_json(data)
25
20
  ].compact.join(' ')
26
21
 
27
- @logger.public_send level, log_data
22
+ @logger.add(level, log_data)
28
23
  end
29
24
  end
30
25
  end
@@ -3,46 +3,18 @@
3
3
  class Lenjador
4
4
  module Adapters
5
5
  class StdoutJsonAdapter
6
- def initialize(level, service_name, *)
7
- @level = level
8
- @service_name = service_name
6
+ def initialize(service_name)
9
7
  @application_name = Utils.application_name(service_name)
10
8
  @mutex = Mutex.new if RUBY_ENGINE == 'jruby'
11
9
  end
12
10
 
13
11
  def log(level, metadata = {})
14
- return unless meets_threshold?(level)
15
-
16
- message = Utils.build_event(metadata, level, @application_name)
12
+ message = Utils.build_event(metadata, Lenjador::SEV_LABEL[level], @application_name)
17
13
  print_line(Utils.generate_json(message))
18
14
  end
19
15
 
20
- def debug?
21
- meets_threshold?(:debug)
22
- end
23
-
24
- def info?
25
- meets_threshold?(:info)
26
- end
27
-
28
- def warn?
29
- meets_threshold?(:warn)
30
- end
31
-
32
- def error?
33
- meets_threshold?(:error)
34
- end
35
-
36
- def fatal?
37
- meets_threshold?(:fatal)
38
- end
39
-
40
16
  private
41
17
 
42
- def meets_threshold?(level)
43
- LOG_LEVELS.index(level) >= @level
44
- end
45
-
46
18
  # puts is atomic in MRI starting from 2.5.0
47
19
  if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '2.5.0'
48
20
  def print_line(str)
@@ -3,45 +3,45 @@ require 'lenjador/adapters/stdout_adapter'
3
3
 
4
4
  describe Lenjador::Adapters::StdoutAdapter do
5
5
  it 'creates a stdout logger' do
6
- io_logger = described_class.new(0)
6
+ io_logger = described_class.new('service name')
7
7
 
8
- logger = io_logger.instance_variable_get(:@logger)
8
+ logger = io_logger.logger
9
9
  expect(logger).to be_a Logger
10
10
  end
11
11
 
12
12
  describe '#log' do
13
- let(:adapter) { described_class.new(0) }
13
+ let(:adapter) { described_class.new('sevice name') }
14
14
  let(:logger) { adapter.logger }
15
15
 
16
16
  context 'with only a message' do
17
17
  it 'stringifies it correctly' do
18
- expect(logger).to receive(:info).with('test')
18
+ expect(logger).to receive(:add).with(Logger::Severity::INFO, 'test')
19
19
 
20
- adapter.log :info, message: 'test'
20
+ adapter.log Lenjador::Severity::INFO, message: 'test'
21
21
  end
22
22
  end
23
23
 
24
24
  context 'with an empty message' do
25
25
  it 'stringifies it correctly' do
26
- expect(logger).to receive(:info).with(' {"a":"b"}')
26
+ expect(logger).to receive(:add).with(Logger::Severity::INFO, ' {"a":"b"}')
27
27
 
28
- adapter.log :info, message: '', a: 'b'
28
+ adapter.log Lenjador::Severity::INFO, message: '', a: 'b'
29
29
  end
30
30
  end
31
31
 
32
32
  context 'with no message' do
33
33
  it 'stringifies it correctly' do
34
- expect(logger).to receive(:info).with('{"a":"b"}')
34
+ expect(logger).to receive(:add).with(Logger::Severity::INFO, '{"a":"b"}')
35
35
 
36
- adapter.log :info, a: 'b'
36
+ adapter.log Lenjador::Severity::INFO, a: 'b'
37
37
  end
38
38
  end
39
39
 
40
40
  context 'with a message and metadata' do
41
41
  it 'stringifies it correctly' do
42
- expect(logger).to receive(:info).with('test {"a":"b"}')
42
+ expect(logger).to receive(:add).with(Logger::Severity::INFO, 'test {"a":"b"}')
43
43
 
44
- adapter.log :info, message: 'test', a: 'b'
44
+ adapter.log Lenjador::Severity::INFO, message: 'test', a: 'b'
45
45
  end
46
46
  end
47
47
  end
@@ -3,11 +3,6 @@ require 'json'
3
3
  require 'lenjador/adapters/stdout_json_adapter'
4
4
 
5
5
  describe Lenjador::Adapters::StdoutJsonAdapter do
6
- let(:debug_level_code) { 0 }
7
- let(:debug_level) { Lenjador::Adapters::LOG_LEVELS[debug_level_code] }
8
- let(:info_level_code) { 1 }
9
- let(:info_level) { Lenjador::Adapters::LOG_LEVELS[info_level_code] }
10
-
11
6
  let(:stdout) { StringIO.new }
12
7
 
13
8
  around do |example|
@@ -22,35 +17,24 @@ describe Lenjador::Adapters::StdoutJsonAdapter do
22
17
  end
23
18
 
24
19
  describe '#log' do
25
- context 'when below threshold' do
26
- let(:adapter) { described_class.new(debug_level_code, service_name) }
27
- let(:metadata) { {x: 'y'} }
28
- let(:event) { {a: 'b', x: 'y'} }
29
- let(:serialized_event) { JSON.dump(event) }
30
- let(:service_name) { 'my-service' }
31
- let(:application_name) { 'my_service' }
32
-
33
- before do
34
- allow(Lenjador::Utils).to receive(:build_event)
35
- .with(metadata, info_level, application_name)
36
- .and_return(event)
37
- end
38
-
39
- it 'sends serialized event to $stdout' do
40
- adapter.log(info_level, metadata)
41
- expect(output).to eq serialized_event + "\n"
42
- end
20
+ let(:adapter) { described_class.new(service_name) }
21
+ let(:metadata) { {x: 'y'} }
22
+ let(:event) { {a: 'b', x: 'y'} }
23
+ let(:serialized_event) { JSON.dump(event) }
24
+ let(:service_name) { 'my-service' }
25
+ let(:application_name) { 'my_service' }
26
+ let(:info) { Lenjador::Severity::INFO }
27
+ let(:info_label) { 'info' }
28
+
29
+ before do
30
+ allow(Lenjador::Utils).to receive(:build_event)
31
+ .with(metadata, info_label, application_name)
32
+ .and_return(event)
43
33
  end
44
34
 
45
- context 'when above threshold' do
46
- let(:adapter) { described_class.new(info_level_code, service_name) }
47
- let(:metadata) { {x: 'y'} }
48
- let(:service_name) { 'my-service' }
49
-
50
- it 'does not log the event' do
51
- adapter.log(debug_level, metadata)
52
- expect(output).to be_empty
53
- end
35
+ it 'sends serialized event to $stdout' do
36
+ adapter.log(info, metadata)
37
+ expect(output).to eq serialized_event + "\n"
54
38
  end
55
39
  end
56
40
 
@@ -3,34 +3,31 @@ require 'spec_helper'
3
3
  describe Lenjador do
4
4
  describe '.build' do
5
5
  it 'creates stdout logger' do
6
- expect(described_class).to receive(:new) do |adapters|
7
- expect(adapters.count).to be(1)
8
- expect(adapters.first).to be_a(described_class::Adapters::StdoutAdapter)
6
+ expect(described_class).to receive(:new) do |adapter|
7
+ expect(adapter).to be_a(described_class::Adapters::StdoutAdapter)
9
8
  end
10
9
 
11
- described_class.build('test_service', stdout: nil)
10
+ described_class.build('test_service', {})
12
11
  end
13
12
 
14
13
  it 'creates stdout json logger' do
15
- expect(described_class).to receive(:new) do |adapters|
16
- expect(adapters.count).to be(1)
17
- expect(adapters.first).to be_a(described_class::Adapters::StdoutJsonAdapter)
14
+ expect(described_class).to receive(:new) do |adapter|
15
+ expect(adapter).to be_a(described_class::Adapters::StdoutJsonAdapter)
18
16
  end
19
17
 
20
- described_class.build('test_service', stdout: {json: true})
18
+ described_class.build('test_service', json: true)
21
19
  end
22
20
 
23
21
  it 'creates stdout logger when no loggers are specified' do
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::StdoutAdapter)
22
+ expect(described_class).to receive(:new) do |adapter|
23
+ expect(adapter).to be_a(described_class::Adapters::StdoutAdapter)
27
24
  end
28
25
 
29
26
  described_class.build('test_service', nil)
30
27
  end
31
28
 
32
29
  it 'creates preprocessor when preprocessor defined' do
33
- expect(described_class).to receive(:new) do |_adapters, preprocessors|
30
+ expect(described_class).to receive(:new) do |_adapter, _level, preprocessors|
34
31
  expect(preprocessors.count).to be(1)
35
32
  expect(preprocessors.first).to be_a(described_class::Preprocessors::Blacklist)
36
33
  end
@@ -41,64 +38,66 @@ describe Lenjador do
41
38
  end
42
39
 
43
40
  context 'when preprocessor defined' do
44
- let(:lenjador) { described_class.new([adapter], [preprocessor]) }
41
+ let(:lenjador) { described_class.new(adapter, level, [preprocessor]) }
45
42
  let(:adapter) { double }
43
+ let(:level) { Lenjador::Severity::DEBUG }
46
44
  let(:preprocessor) { double }
47
45
  let(:data) { {data: 'data'} }
48
46
 
49
47
  it 'preprocesses data before logging' do
50
48
  expect(preprocessor).to receive(:process).with(data).and_return(data.merge(processed: true)).ordered
51
- expect(adapter).to receive(:log).with(:info, data.merge(processed: true)).ordered
49
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, data.merge(processed: true)).ordered
52
50
 
53
51
  lenjador.info(data)
54
52
  end
55
53
  end
56
54
 
57
55
  context 'when parsing log data' do
58
- let(:lenjador) { described_class.new([adapter], preprocessors) }
56
+ let(:lenjador) { described_class.new(adapter, level, preprocessors) }
59
57
  let(:adapter) { double }
58
+ let(:level) { Lenjador::Severity::DEBUG }
60
59
  let(:preprocessors) { [] }
61
60
 
62
61
  it 'parses empty string with nil metadata' do
63
- expect(adapter).to receive(:log).with(:info, message: '')
62
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: '')
64
63
 
65
64
  lenjador.info('', nil)
66
65
  end
67
66
 
68
67
  it 'parses nil as metadata' do
69
- expect(adapter).to receive(:log).with(:info, message: nil)
68
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: nil)
70
69
 
71
70
  lenjador.info(nil)
72
71
  end
73
72
 
74
73
  it 'parses only message' do
75
- expect(adapter).to receive(:log).with(:info, message: 'test message')
74
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: 'test message')
76
75
 
77
76
  lenjador.info 'test message'
78
77
  end
79
78
 
80
79
  it 'parses only metadata' do
81
- expect(adapter).to receive(:log).with(:info, test: 'data')
80
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, test: 'data')
82
81
 
83
82
  lenjador.info test: 'data'
84
83
  end
85
84
 
86
85
  it 'parses message and metadata' do
87
- expect(adapter).to receive(:log).with(:info, message: 'test message', test: 'data')
86
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: 'test message', test: 'data')
88
87
 
89
88
  lenjador.info 'test message', test: 'data'
90
89
  end
91
90
 
92
91
  it 'parses block as a message' do
93
92
  message = 'test message'
94
- expect(adapter).to receive(:log).with(:info, message: message)
93
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: message)
95
94
 
96
95
  lenjador.info { message }
97
96
  end
98
97
 
99
98
  it 'ignores progname on block syntax' do
100
99
  message = 'test message'
101
- expect(adapter).to receive(:log).with(:info, message: message)
100
+ expect(adapter).to receive(:log).with(described_class::Severity::INFO, message: message)
102
101
 
103
102
  lenjador.info('progname') { message }
104
103
  end
@@ -106,9 +105,7 @@ describe Lenjador do
106
105
 
107
106
  context 'with log level' do
108
107
  context 'when adapter has debug level' do
109
- let(:logger) do
110
- described_class.build('test_service', stdout: {level: 'debug'})
111
- end
108
+ let(:logger) { described_class.build('test_service', level: 'debug') }
112
109
 
113
110
  it 'responds true to debug? and higher levels' do
114
111
  expect(logger.debug?).to be(true)
@@ -120,9 +117,7 @@ describe Lenjador do
120
117
  end
121
118
 
122
119
  context 'when adapter has info level' do
123
- let(:logger) do
124
- described_class.build('test_service', stdout: {level: 'info'})
125
- end
120
+ let(:logger) { described_class.build('test_service', level: 'info') }
126
121
 
127
122
  it 'responds true to info? and higher levels' do
128
123
  expect(logger.debug?).to be(false)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lenjador
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 2.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: 2018-09-08 00:00:00.000000000 Z
11
+ date: 2019-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lru_redux
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '1.3'
75
+ version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '1.3'
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -196,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  version: '0'
197
197
  requirements: []
198
198
  rubyforge_project:
199
- rubygems_version: 2.6.11
199
+ rubygems_version: 2.7.6
200
200
  signing_key:
201
201
  specification_version: 4
202
202
  summary: What description said