tag_logger 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca7876f8a33dfbe7a209c3981c80c92013bb064151e0e5bd8c922551d0bc61b4
4
- data.tar.gz: f6d858cc3d50e54971ae194985264313886a6ea71b0fd8e0a5627a08b7849db9
3
+ metadata.gz: 06b6ffe603dda9c2a7833e47df69ffb15940a00d6bcb7721951316d7be5ad74d
4
+ data.tar.gz: e51ecdcf5d7ff594a3658862864a4da8f058693f107c30ab596838d44d27e876
5
5
  SHA512:
6
- metadata.gz: 200a2c7c1f70bc92d8ab308c988365901bd7e410f7cc60db28353570cbea56c0ceb31abf60e647e81c4d286a713e4cd79da08b8f4320d9637daaa955aef0ea2a
7
- data.tar.gz: f60823c056fb2aac28fd3bb8cb8bb9266de5c293059ee70e9e980b7e3934679ddd1b7dd7607baf015b83b4f5eb6becd5ff6cd737076092cb59cb3e6f24dae5da
6
+ metadata.gz: ee664cba8fce8430fea45838e72e28bc3d72afaebcd78ee144a977d092feb1f8cdf61af8c349b2d9f48c79e02339316170861104f519d26769a5c27fb938a470
7
+ data.tar.gz: 5f5750e470787a3d334e05103ad43893122640431aa202162011f3c308037343a2402858bd23b28a7d20072310353e962509a4cdf836672915e753b4e7fa2c3e
data/README.md CHANGED
@@ -11,7 +11,7 @@ gem 'tag_logger'
11
11
 
12
12
  Or install it yourself as:
13
13
  ```bash
14
- $ gem install sample_filter
14
+ $ gem install tag_logger
15
15
  ```
16
16
 
17
17
  ## Usage
@@ -19,6 +19,8 @@ First need to add configuration:
19
19
  ```ruby
20
20
  TagLogger.configure do |config|
21
21
  config.output_path = 'log/tag_logger.log'
22
+ config.filter_parameters = [:password]
23
+ config.use_stdout = true # STDOUT log isn't shown by default
22
24
  end
23
25
  ```
24
26
 
@@ -29,10 +31,15 @@ class MyClass
29
31
 
30
32
  def my_method
31
33
  # start logging with tags
32
- tag_logger 'tag_name'
34
+ tag_logger 'tag_name', 'another_name'
33
35
 
34
36
  # then log with levels: [:warn, :info, :debug, :fatal, :error]
35
37
  log :info, 'Log information'
38
+
39
+ # if data has sensitive information, it must be sanitized
40
+ # INFO -- : [tag_name] Log information {:email=>"test@email.com", :password=>"[FILTERED]"}
41
+ data = {email: 'test@email.com', password: '123456'}
42
+ log :info, "Log information #{sanitize_log(data)}"
36
43
  end
37
44
  end
38
45
  ```
data/lib/tag_logger.rb CHANGED
@@ -2,15 +2,27 @@ require 'tag_logger/helper_logger'
2
2
  require 'tag_logger/configuration'
3
3
 
4
4
  module TagLogger
5
+ FILTERED_TEXT = '[FILTERED]'.freeze
6
+
5
7
  def tag_logger(*tags)
6
8
  @tag_logger ||= HelperLogger.new(tags)
7
9
  end
8
10
 
9
11
  def log(type, text)
10
- if @tag_logger.nil?
11
- raise 'Initialize `tag_logger` method with tags name'
12
- else
13
- @tag_logger.write_log(type, text)
12
+ tag_logger if @tag_logger.nil?
13
+
14
+ @tag_logger.write_log(type, text)
15
+ end
16
+
17
+ def sanitize_log(data)
18
+ data = data.to_hash.transform_keys(&:to_sym)
19
+
20
+ filter_parameters = TagLogger.configuration.filter_parameters
21
+ filter_parameters.each do |key|
22
+ next if data[key.to_sym].nil?
23
+ data[key.to_sym] = FILTERED_TEXT
14
24
  end
25
+
26
+ data
15
27
  end
16
28
  end
@@ -9,6 +9,10 @@ module TagLogger
9
9
  end
10
10
 
11
11
  class Configuration
12
- attr_accessor :output_path
12
+ attr_accessor :output_path, :use_stdout, :filter_parameters
13
+
14
+ def initialize
15
+ @filter_parameters = Array.new
16
+ end
13
17
  end
14
18
  end
@@ -1,20 +1,27 @@
1
1
  module TagLogger
2
2
  class HelperLogger
3
- attr_reader :tags, :logger
3
+ attr_reader :tag_id, :tags, :path_logger, :stdout_logger
4
4
 
5
- def initialize(tags)
6
- if tags.empty?
7
- raise 'Tags for `tag_logger` are empty!'
8
- else
9
- @tags = tags
5
+ def initialize(tags = [])
6
+ raise 'TagLogger configuration is nil!' if TagLogger.configuration.nil?
7
+
8
+ @tag_id = rand(36**8).to_s(36)
9
+ @tags = tags
10
+
11
+ output_path = TagLogger.configuration.output_path
12
+ use_stdout = TagLogger.configuration.use_stdout
13
+
14
+ unless output_path.nil? || output_path.empty?
15
+ @path_logger ||= Logger.new(output_path)
16
+ path_logger.formatter = log_format
10
17
  end
11
18
 
12
- output_path = TagLogger.configuration&.output_path
13
- if output_path.nil? || output_path.empty?
14
- raise 'TagLogger configuration `output_path` is blank!'
15
- else
16
- @logger ||= Logger.new(output_path)
19
+ if TagLogger.configuration.use_stdout == true
20
+ @stdout_logger ||= Logger.new(STDOUT)
21
+ stdout_logger.formatter = log_format
17
22
  end
23
+
24
+ raise 'TagLogger output is blank!' if path_logger.nil? && stdout_logger.nil?
18
25
  end
19
26
 
20
27
  def write_log(type, text)
@@ -28,12 +35,16 @@ module TagLogger
28
35
 
29
36
  log_text = prepared_text(text)
30
37
  logger_class = types.fetch(type)
31
- logger_class.log(logger, log_text)
38
+
39
+ logger_class.log(path_logger, log_text) if path_logger
40
+ logger_class.log(stdout_logger, log_text) if stdout_logger
32
41
  end
33
42
 
34
43
  private
35
44
 
36
45
  def prepared_text(text)
46
+ return text if tags.empty?
47
+
37
48
  ''.tap do |s|
38
49
  tags.each do |t|
39
50
  s << ("[#{t}]" + ' ')
@@ -42,6 +53,12 @@ module TagLogger
42
53
  end
43
54
  end
44
55
 
56
+ def log_format
57
+ proc do |severity, time, _program_name, message|
58
+ "#{time.utc.iso8601(3)} ##{Process.pid} TID-#{Thread.current.object_id.to_s(36)} TAG-#{tag_id} #{severity}: #{message}\n"
59
+ end
60
+ end
61
+
45
62
  class ErrorLogger
46
63
  def self.log(logger, text)
47
64
  logger.error(text)
@@ -1,3 +1,3 @@
1
1
  module TagLogger
2
- VERSION = '1.0.2'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tag_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - iliakg
@@ -41,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements: []
44
- rubygems_version: 3.0.6
44
+ rubygems_version: 3.1.4
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: Simple gem for tag logging