lenjador 1.2.1 → 2.1.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: 0b22b9348f98f915eaa67dec7cbbe762d09f86dc
4
- data.tar.gz: 2e22ff6de0251d2e007b83e626e14c20d860b281
2
+ SHA256:
3
+ metadata.gz: 5d96edbeff69ee13444e45a5924d8fedd4907725831f9ce62fd2f3280b48b244
4
+ data.tar.gz: b7e501d6a7dba6b7461df12e72530796900a4241700dd676c48d6e6f8341e0b2
5
5
  SHA512:
6
- metadata.gz: e48ff7741f76c07e5dd00e910e32cd6013ee4ff6854ccfc29bf4dc249e54635500c4ff5fedd51ac7394a643baa4a6e1a60269b41b9c5fb2006110fefe45f13af
7
- data.tar.gz: 0cd79d4f685890c86d4c0c182f256f35c0dbe48a95b77a548193cbec05f21a8df5ce551c1fd77fe5a9a751a5a9491b04c24e78392c9bb7ae7fa92ea2f272ae29
6
+ metadata.gz: b1142e4a26fb13f3ba2d880de8c3b58e18eacc77de055d36b10c3a9783c6c1d597a50aab94f1611a43fc17f8490f6598049da6f5d4c9ed73af77be2fa5ccb26e
7
+ data.tar.gz: 6f6d29354c80eae57485fc2f3fa5f6e0643aad692763957ab43c8ba3041a8f5d9cbba6fb3e9453a121c6634d5e7e776d82df0286a966b97613d03c437b602827
data/.rubocop.yml ADDED
@@ -0,0 +1,41 @@
1
+ inherit_gem:
2
+ rubocop-salemove: config/default.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.4
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: yes
9
+ EnforcedStyle: always
10
+ Include:
11
+ - 'lib/**/*'
12
+
13
+ Metrics/MethodLength:
14
+ Enabled: no
15
+
16
+ Style/GuardClause:
17
+ Enabled: no
18
+
19
+ RSpec/VerifiedDoubles:
20
+ Enabled: no
21
+
22
+ Metrics/BlockLength:
23
+ Enabled: no
24
+
25
+ RSpec/MessageSpies:
26
+ Enabled: no
27
+
28
+ RSpec/ExpectOutput:
29
+ Enabled: no
30
+
31
+ RSpec/ExampleLength:
32
+ Enabled: no
33
+
34
+ RSpec/MultipleExpectations:
35
+ Enabled: no
36
+
37
+ Style/SafeNavigation:
38
+ Enabled: no
39
+
40
+ Style/SingleLineMethods:
41
+ Enabled: no
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.4.1
1
+ ruby-2.7.1
data/.travis.yml CHANGED
@@ -1,7 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2
4
- - 2.3
5
3
  - 2.4
6
4
  - 2.5
7
- - jruby
5
+ - 2.7
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
- source "https://rubygems.org"
2
-
3
- group :test, :development do
4
- gem 'pry'
5
- gem 'rspec'
6
- end
1
+ source 'https://rubygems.org'
7
2
 
8
3
  gemspec
data/Rakefile CHANGED
@@ -1,7 +1,9 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rubocop/rake_task'
3
+ require 'rspec/core/rake_task'
2
4
 
3
- require "rspec/core/rake_task"
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
 
6
- task ci: :spec
7
- task default: :spec
7
+ RuboCop::RakeTask.new(:rubocop)
8
+
9
+ task default: %i[rubocop spec]
@@ -21,20 +21,20 @@ pointers = %w[
21
21
  end
22
22
 
23
23
  x.report("Flat hash whitelisting (action=#{action})") do
24
- preprocessor.process(flat_hash: { scalar: 'value', array: [1, 2], hash: {} })
24
+ preprocessor.process(flat_hash: {scalar: 'value', array: [1, 2], hash: {}})
25
25
  end
26
26
 
27
27
  x.report("Nested hash whitelisting (action=#{action})") do
28
28
  preprocessor.process(
29
29
  nested_hash: {
30
30
  next_level_hash: {
31
- deep_hash: { scalar: 'value', array: [1, 2] }
31
+ deep_hash: {scalar: 'value', array: [1, 2]}
32
32
  },
33
33
  next_level_hash2: {
34
- deep_hash: { scalar: 'value', array: [1, 2] }
34
+ deep_hash: {scalar: 'value', array: [1, 2]}
35
35
  },
36
36
  next_level_hash3: {
37
- deep_hash: { scalar: 'value', array: [1, 2] }
37
+ deep_hash: {scalar: 'value', array: [1, 2]}
38
38
  }
39
39
  }
40
40
  )
@@ -43,9 +43,9 @@ pointers = %w[
43
43
  x.report("Flat array whitelisting (action=#{action})") do
44
44
  preprocessor.process(
45
45
  nested_array: [
46
- { deep_array: [1, 2, 3] },
47
- { deep_array: [1, 2, 3] },
48
- { deep_array: [1, 2, 3] }
46
+ {deep_array: [1, 2, 3]},
47
+ {deep_array: [1, 2, 3]},
48
+ {deep_array: [1, 2, 3]}
49
49
  ]
50
50
  )
51
51
  end
data/lenjador.gemspec CHANGED
@@ -1,36 +1,31 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
 
5
6
  Gem::Specification.new do |gem|
6
- if RUBY_PLATFORM =~ /java/
7
- gem.name = 'lenjador-jruby'
8
- else
9
- gem.name = 'lenjador'
10
- end
11
-
12
- gem.version = '1.2.1'
13
- gem.authors = ["Salemove"]
14
- gem.email = ["support@salemove.com"]
15
- gem.description = %q{It's lenjadoric}
16
- gem.summary = %q{What description said}
17
- gem.license = "MIT"
7
+ gem.name = 'lenjador'
8
+ gem.version = '2.1.0'
9
+ gem.authors = ['Salemove']
10
+ gem.email = ['support@salemove.com']
11
+ gem.description = "It's lenjadoric"
12
+ gem.summary = 'What description said'
13
+ gem.license = 'MIT'
14
+ gem.required_ruby_version = '>= 2.4'
18
15
 
19
- gem.files = `git ls-files`.split($/)
16
+ gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
20
17
  gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
22
- gem.require_paths = ["lib"]
19
+ gem.require_paths = ['lib']
23
20
 
24
21
  gem.add_dependency 'lru_redux'
22
+ gem.add_dependency 'oj', '~> 3.6'
25
23
 
26
- if RUBY_PLATFORM =~ /java/
27
- gem.add_dependency 'jrjackson'
28
- else
29
- gem.add_dependency 'oj'
30
- end
31
-
32
- gem.add_development_dependency "bundler", "~> 1.3"
33
- gem.add_development_dependency "rake"
34
- gem.add_development_dependency "bunny"
35
- gem.add_development_dependency "benchmark-ips"
24
+ gem.add_development_dependency 'benchmark-ips'
25
+ gem.add_development_dependency 'bundler'
26
+ gem.add_development_dependency 'pry'
27
+ gem.add_development_dependency 'rake'
28
+ gem.add_development_dependency 'rspec'
29
+ gem.add_development_dependency 'rubocop-salemove'
30
+ gem.add_development_dependency 'ruby-prof'
36
31
  end
data/lib/lenjador.rb CHANGED
@@ -1,63 +1,78 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
  require_relative 'lenjador/adapters'
3
5
  require_relative 'lenjador/utils'
4
6
  require_relative 'lenjador/null_logger'
5
7
  require_relative 'lenjador/preprocessors'
6
8
 
7
- LOG_LEVEL_QUERY_METHODS = [:debug?, :info?, :warn?, :error?, :fatal?]
8
-
9
9
  class Lenjador
10
- def self.build(service_name, loggers_config, preprocessors_config = {})
11
- 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
+
12
16
  preprocessors = preprocessors_config.map do |type, arguments|
13
17
  Preprocessors.get(type.to_s, arguments || {})
14
18
  end
15
- adapters = loggers_config.map do |type, arguments|
16
- Adapters.get(type.to_s, service_name, arguments || {})
17
- end
18
- 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)
19
23
  end
20
24
 
21
- def initialize(adapters, preprocessors)
22
- @adapters = adapters
25
+ def initialize(adapter, level, preprocessors)
26
+ @adapter = adapter
27
+ @level = level
23
28
  @preprocessors = preprocessors
24
29
  end
25
30
 
26
31
  def debug(*args, &block)
27
- log :debug, *args, &block
32
+ log(Severity::DEBUG, *args, &block)
28
33
  end
29
34
 
30
35
  def info(*args, &block)
31
- log :info, *args, &block
36
+ log(Severity::INFO, *args, &block)
32
37
  end
33
38
 
34
39
  def warn(*args, &block)
35
- log :warn, *args, &block
40
+ log(Severity::WARN, *args, &block)
36
41
  end
37
42
 
38
43
  def error(*args, &block)
39
- log :error, *args, &block
44
+ log(Severity::ERROR, *args, &block)
40
45
  end
41
46
 
42
47
  def fatal(*args, &block)
43
- log :fatal, *args, &block
48
+ log(Severity::FATAL, *args, &block)
44
49
  end
45
50
 
46
- LOG_LEVEL_QUERY_METHODS.each do |method|
47
- define_method(method) do
48
- @adapters.any? {|adapter| adapter.public_send(method) }
49
- 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
60
+
61
+ def level=(new_level)
62
+ raise ArgumentError, "invalid log level: #{new_level}" unless new_level.is_a?(Integer)
63
+
64
+ @level = new_level
50
65
  end
51
66
 
52
67
  private
53
68
 
54
69
  def log(level, *args, &block)
70
+ return true if level < @level
71
+
55
72
  data = parse_log_data(*args, &block)
56
73
  processed_data = preprocess(data)
57
74
 
58
- @adapters.each do |adapter|
59
- adapter.log(level, processed_data)
60
- end
75
+ @adapter.log(level, processed_data)
61
76
  end
62
77
 
63
78
  def preprocess(data)
@@ -69,6 +84,6 @@ class Lenjador
69
84
  def parse_log_data(message = nil, metadata = {}, &block)
70
85
  return message if message.is_a?(Hash)
71
86
 
72
- (metadata || {}).merge(message: block ? block.call : message)
87
+ (metadata || {}).merge(message: block ? yield : message)
73
88
  end
74
89
  end
@@ -1,22 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Lenjador
2
4
  module Adapters
3
- LOG_LEVELS = %w(debug info warn error fatal unknown).freeze
4
-
5
- def self.get(type, service_name, arguments)
5
+ def self.get(service_name, config)
6
6
  adapter =
7
- if type == 'stdout'
8
- if arguments.fetch(:json, false)
9
- require_relative 'adapters/stdout_json_adapter'
10
- StdoutJsonAdapter
11
- else
12
- require_relative 'adapters/stdout_adapter'
13
- StdoutAdapter
14
- end
7
+ if config.fetch(:json, false)
8
+ require_relative 'adapters/stdout_json_adapter'
9
+ StdoutJsonAdapter
15
10
  else
16
- raise "Unsupported logger: #{type}"
11
+ require_relative 'adapters/stdout_adapter'
12
+ StdoutAdapter
17
13
  end
18
- level = LOG_LEVELS.index(arguments.fetch(:level, 'debug'))
19
- adapter.new(level, service_name, arguments)
14
+ adapter.new(service_name)
20
15
  end
21
16
  end
22
17
  end
@@ -1,28 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'forwardable'
2
4
 
3
5
  class Lenjador
4
6
  module Adapters
5
7
  class StdoutAdapter
6
- extend Forwardable
7
-
8
8
  attr_reader :logger
9
9
 
10
- def_delegators :@logger, :debug?, :info?, :warn?, :error?, :fatal?
11
-
12
- def initialize(level, *)
13
- @logger = Logger.new(STDOUT)
14
- @logger.level = level
10
+ def initialize(_service_name)
11
+ @logger = Logger.new($stdout)
15
12
  end
16
13
 
17
14
  def log(level, metadata = {})
18
15
  message = metadata[:message]
19
- data = metadata.select { |key, value| key != :message }
16
+ data = metadata.reject { |key, _value| key == :message }
20
17
  log_data = [
21
18
  message,
22
19
  data.empty? ? nil : Utils.generate_json(data)
23
20
  ].compact.join(' ')
24
21
 
25
- @logger.public_send level, log_data
22
+ @logger.add(level, log_data)
26
23
  end
27
24
  end
28
25
  end
@@ -1,58 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Lenjador
2
4
  module Adapters
3
5
  class StdoutJsonAdapter
4
- def initialize(level, service_name, *)
5
- @level = level
6
- @service_name = service_name
6
+ def initialize(service_name)
7
7
  @application_name = Utils.application_name(service_name)
8
- @mutex = Mutex.new if RUBY_ENGINE == "jruby"
8
+ @mutex = Mutex.new if RUBY_ENGINE == 'jruby'
9
9
  end
10
10
 
11
11
  def log(level, metadata = {})
12
- if meets_threshold?(level)
13
- message = Utils.build_event(metadata, level, @application_name)
14
- print_line(Utils.generate_json(message))
15
- end
16
- end
17
-
18
- def debug?
19
- meets_threshold?(:debug)
20
- end
21
-
22
- def info?
23
- meets_threshold?(:info)
24
- end
25
-
26
- def warn?
27
- meets_threshold?(:warn)
28
- end
29
-
30
- def error?
31
- meets_threshold?(:error)
32
- end
33
-
34
- def fatal?
35
- meets_threshold?(:fatal)
12
+ message = Utils.build_event(metadata, Lenjador::SEV_LABEL[level], @application_name)
13
+ print_line(Utils.generate_json(message))
36
14
  end
37
15
 
38
16
  private
39
17
 
40
- def meets_threshold?(level)
41
- LOG_LEVELS.index(level.to_s) >= @level
42
- end
43
-
44
18
  # puts is atomic in MRI starting from 2.5.0
45
- if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.5.0"
19
+ if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '2.5.0'
46
20
  def print_line(str)
47
21
  $stdout.puts(str)
48
22
  end
49
- elsif RUBY_ENGINE == "jruby"
23
+ elsif RUBY_ENGINE == 'jruby'
50
24
  def print_line(str)
51
- @mutex.synchronize { $stdout.write(str + "\n") }
25
+ @mutex.synchronize { $stdout.write("#{str}\n") }
52
26
  end
53
27
  else
54
28
  def print_line(str)
55
- $stdout.write(str + "\n")
29
+ $stdout.write("#{str}\n")
56
30
  end
57
31
  end
58
32
  end
@@ -1,18 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Lenjador
2
4
  class NullLogger
3
- def debug(*)
4
- end
5
+ def debug(*); end
5
6
 
6
- def info(*)
7
- end
7
+ def info(*); end
8
8
 
9
- def warn(*)
10
- end
9
+ def warn(*); end
11
10
 
12
- def error(*)
13
- end
11
+ def error(*); end
14
12
 
15
- def fatal(*)
16
- end
13
+ def fatal(*); end
17
14
  end
18
15
  end