testa_logger 0.1.6 → 0.1.9

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
  SHA256:
3
- metadata.gz: 221e5b91b1d4b4cf4f23b9b05d62f513260e099fe31f8442d990b8cb5d018c8d
4
- data.tar.gz: 0f4b906fc126ab87c3505521de5c64c5cf37eaf0b310226059d913e4e8f10a84
3
+ metadata.gz: 25f577c5833651b7703e13fbe74baba79b9c55ef2c096674dfc9ef71474c1b89
4
+ data.tar.gz: 5536250ba315e6cbf652db841655c1d6dba996fa1a08b2a8a9aa074134694e10
5
5
  SHA512:
6
- metadata.gz: 1c76a747bdf8434b11a28a1e30fbfd42dd19d918d6e3e79ccbb955dd9335879d269d7caecc21619acf266627105b73c659042f318c362662eb8bdbfda7291233
7
- data.tar.gz: a21d127b08107f21bc999bfb7e4e38e1f639b2efe5ae0f9f15e8dae133e6e162e88f5f74de85cd2b3fb3f937799e45e126fb38745fa9c74c629dfa544e6a22db
6
+ metadata.gz: 2eb41038df42fe7076182558f8d3391ff004197056279111f06f0e11fe9bf92b4b996b036e7ed8735da9ad8d53088ef67aec1a66b9586b854a46027e6c016b99
7
+ data.tar.gz: f0bf28e003b4fa21cb94c65267ab1a0110939371148fd19b2053e03d98d57fb332f94686b7d271099fdeb3936f4afd980d7be100d666191d3205d10754728ecb
data/Gemfile.lock CHANGED
@@ -1,13 +1,12 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- testa_logger (0.1.5)
4
+ testa_logger (0.1.9)
5
5
  activesupport
6
6
  awesome_print
7
7
  aws-sdk-s3
8
8
  concurrent-ruby
9
9
  json
10
- ostruct
11
10
  securerandom
12
11
 
13
12
  GEM
@@ -43,7 +42,6 @@ GEM
43
42
  jmespath (1.6.1)
44
43
  json (2.6.2)
45
44
  minitest (5.16.2)
46
- ostruct (0.5.5)
47
45
  parallel (1.22.1)
48
46
  parser (3.1.2.1)
49
47
  ast (~> 2.4.1)
@@ -0,0 +1,77 @@
1
+ module TestaLogger
2
+ class Logger
3
+ class Options
4
+
5
+ # @return [String,Integer]
6
+ attr_accessor :shift_age
7
+
8
+ # @return [Integer]
9
+ attr_accessor :level, :tag_length
10
+
11
+ # @return [Proc]
12
+ attr_accessor :formatter
13
+
14
+ # @return [TrueClass, FalseClass]
15
+ attr_accessor :live, :persist
16
+
17
+ # @return [String, nil]
18
+ attr_accessor :filepath
19
+
20
+ # @return [String]
21
+ attr_accessor :faye_url, :faye_token
22
+
23
+ # @return [Hash]
24
+ attr_accessor :s3_credentials
25
+
26
+ def initialize(app, group, subgroup, options)
27
+ app_and_groups = format_app_and_groups(app, group, subgroup)
28
+ @shift_age = "daily"
29
+ @level = DEBUG
30
+ @formatter = proc do |severity, datetime, _, msg|
31
+ "#{app_and_groups}[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}] #{format('%-5.5s', severity)} -- : #{msg}\n"
32
+ end
33
+ @live = false
34
+ @filepath = nil
35
+ @tag_length = 13
36
+ @persist = true # requires aws credentials set in env variables
37
+ @faye_url = ENV["WEB_SOCKET_URL"]
38
+ @faye_token = ENV["FAYE_TOKEN"]
39
+ @s3_credentials = {
40
+ region: ENV["AWS_REGION"],
41
+ access_key_id: ENV["AWS_ACCESS_KEY_ID"],
42
+ secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"],
43
+ bucket_name: ENV["S3_BUCKET_NAME"],
44
+ }
45
+
46
+ handle_user_options(options)
47
+ end
48
+
49
+ def format_app_and_groups(app, group, subgroup)
50
+ string = "[ #{app} | #{group} "
51
+ if subgroup.nil?
52
+ string += "]"
53
+ else
54
+ string += "| #{subgroup} ]"
55
+ end
56
+ string
57
+ end
58
+
59
+ def handle_user_options(options)
60
+ options.deep_symbolize_keys!
61
+ @shift_age = options[:shift_age] unless options[:shift_age].nil?
62
+ unless options[:level].nil?
63
+ options[:level] = SEV_LABEL.index(options[:level].to_s.upcase).to_i unless options[:level].is_a?(Integer)
64
+ @level = options[:level]
65
+ end
66
+ @formatter = options[:formatter] unless options[:formatter].nil?
67
+ @live = options[:live] unless options[:live].nil?
68
+ @filepath = File.expand_path(options[:filepath]) unless options[:filepath].nil?
69
+ @tag_length = options[:tag_length] unless options[:tag_length].nil?
70
+ @persist = options[:persist] unless options[:persist].nil?
71
+ @faye_url = options[:faye_url] unless options[:faye_url].nil?
72
+ @faye_token = options[:faye_token] unless options[:faye_token].nil?
73
+ @s3_credentials = options[:s3_credentials].deep_symbolize_keys if options[:s3_credentials].present?
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,15 +1,13 @@
1
1
  module TestaLogger
2
2
  class Logger
3
3
  module Persistence
4
- def self.extended(base)
5
- require "aws-sdk-s3"
6
- base.init_s3_client
7
- end
8
-
9
4
  def init_s3_client
5
+ require "aws-sdk-s3"
10
6
  Aws.config.update(
11
- region: options.s3_creds[:region],
12
- credentials: Aws::Credentials.new(options.s3_creds[:access_key_id], options.s3_creds[:secret_access_key])
7
+ region: options.s3_credentials[:region],
8
+ credentials: Aws::Credentials.new(
9
+ options.s3_credentials[:access_key_id],
10
+ options.s3_credentials[:secret_access_key])
13
11
  )
14
12
  @s3 = Aws::S3::Client.new
15
13
  at_exit { persist rescue false }
@@ -47,7 +45,7 @@ module TestaLogger
47
45
 
48
46
  # stop writing into log file with it is being attached, otherwise checksum integrity will fail.
49
47
  response = @s3.put_object(
50
- bucket: options.s3_creds[:bucket_name],
48
+ bucket: options.s3_credentials[:bucket_name],
51
49
  key: key,
52
50
  body: IO.read(options.filepath)
53
51
  )
@@ -1,14 +1,18 @@
1
1
  require_relative "logger/persistence"
2
2
  require_relative "logger/dispatcher"
3
+ require_relative "logger/options"
3
4
 
4
5
  module TestaLogger
6
+ # noinspection RubyTooManyMethodsInspection
5
7
  class Logger
8
+ include Persistence
9
+
6
10
  TAG = "TestaLogger"
7
11
 
8
12
  # @return [String]
9
13
  attr_accessor :app, :group, :subgroup
10
14
 
11
- # @return [OpenStruct]
15
+ # @return [Options]
12
16
  attr_accessor :options
13
17
 
14
18
  # @return [Thread]
@@ -27,33 +31,17 @@ module TestaLogger
27
31
  # An unknown message that should always be logged.
28
32
  UNKNOWN = 5
29
33
 
34
+ NO_TAG = "NO-TAG"
35
+
30
36
  def initialize(app, group, subgroup = nil, options = {})
31
37
  @app = app
32
38
  @group = group
33
39
  @subgroup = subgroup
34
- handle_options(options)
40
+ @options = Options.new(app, group, subgroup, options)
35
41
  create_log_file
36
42
  setup_dispatcher
37
43
  start_write_thread
38
- self.extend Persistence if @options.persist
39
- end
40
-
41
- def handle_options(options)
42
- options.deep_symbolize_keys!
43
- @options = Logger.default_options
44
- @options.shift_age = options[:shift_age] unless options[:shift_age].nil?
45
- unless options[:level].nil?
46
- options[:level] = SEV_LABEL.index(options[:level].to_s.upcase).to_i unless options[:level].is_a?(Integer)
47
- @options.level = options[:level]
48
- end
49
- @options.formatter = options[:formatter] unless options[:formatter].nil?
50
- @options.live = options[:live] unless options[:live].nil?
51
- @options.filepath = File.expand_path(options[:filepath]) unless options[:filepath].nil?
52
- @options.tag_length = options[:tag_length] unless options[:tag_length].nil?
53
- @options.persist = options[:persist] unless options[:persist].nil?
54
- @options.faye_url = options[:faye_url] unless options[:faye_url].nil?
55
- @options.faye_token = options[:faye_token] unless options[:faye_token].nil?
56
- @options.s3_creds = options[:s3_creds].deep_symbolize_keys unless options[:s3_creds].blank?
44
+ init_s3_client if @options.persist
57
45
  end
58
46
 
59
47
  def create_log_file
@@ -90,33 +78,33 @@ module TestaLogger
90
78
  end
91
79
  end
92
80
 
93
- def debug(tag, *args, &block)
81
+ def debug(tag = NO_TAG, *args, &block)
94
82
  add_log_to_queue(DEBUG, tag, args, &block)
95
83
  end
96
84
 
97
- def info(tag, *args, &block)
85
+ def info(tag = NO_TAG, *args, &block)
98
86
  add_log_to_queue(INFO, tag, args, &block)
99
87
  end
100
88
 
101
- def warn(tag, *args, &block)
89
+ def warn(tag = NO_TAG, *args, &block)
102
90
  add_log_to_queue(WARN, tag, args, &block)
103
91
  end
104
92
 
105
- def error(tag, *args, &block)
93
+ def error(tag = NO_TAG, *args, &block)
106
94
  add_log_to_queue(ERROR, tag, args, &block)
107
95
  end
108
96
 
109
- def fatal(tag, *args, &block)
97
+ def fatal(tag = NO_TAG, *args, &block)
110
98
  add_log_to_queue(FATAL, tag, args, &block)
111
99
  end
112
100
 
113
- def unknown(tag, *args, &block)
101
+ def unknown(tag = NO_TAG, *args, &block)
114
102
  add_log_to_queue(UNKNOWN, tag, args, &block)
115
103
  end
116
104
 
117
105
  def level=(severity)
118
106
  if severity.is_a?(Integer)
119
- @level = severity
107
+ options.level = severity
120
108
  else
121
109
  SEV_LABEL.index(severity.to_s.downcase.upcase).to_i
122
110
  end
@@ -209,12 +197,6 @@ module TestaLogger
209
197
  }
210
198
  )
211
199
  end
212
-
213
- def default_formatter
214
- proc do |severity, datetime, _, msg|
215
- "[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%L')}] #{format('%-5.5s', severity)} -- : #{msg}\n"
216
- end
217
- end
218
200
  end
219
201
 
220
202
  private
@@ -223,6 +205,7 @@ module TestaLogger
223
205
  return if level < options.level
224
206
 
225
207
  time = Time.now
208
+ tag = extract_tag(tag, args)
226
209
  text = create_log_string(tag, args, &block)
227
210
  formatted_severity = format_severity(level)
228
211
  formatted_text = format_message(formatted_severity, time, "", text)
@@ -231,16 +214,21 @@ module TestaLogger
231
214
  @dispatcher.push(level, time, tag, formatted_text) if options.live
232
215
  end
233
216
 
234
- def create_log_string(tag, args, &block)
217
+ # rails and other applications do not use tags,
218
+ # therefore, if only 1 argument is provided, that means that the message is in the tag variable
219
+ # then args array is empty.
220
+ def extract_tag(tag, args)
235
221
  if args.count.zero?
236
- if block.nil?
237
- "[NO-TAG]: #{tag}"
238
- else
239
- "[#{padded_tag(tag)}]: #{args_to_string(block.call)}"
240
- end
241
- else
242
- "[#{padded_tag(tag)}]: #{args_to_string(args)}"
222
+ # if no additional arguments is given, message is in tag variable
223
+ args.unshift(tag)
224
+ tag = NO_TAG
243
225
  end
226
+ tag
227
+ end
228
+
229
+ def create_log_string(tag, args, &block)
230
+ args.push(block.call) if block
231
+ "[#{padded_tag(tag)}]: #{args_to_string(args.compact)}"
244
232
  end
245
233
 
246
234
  def padded_tag(tag)
@@ -1,3 +1,3 @@
1
1
  module TestaLogger
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.9"
3
3
  end
data/lib/testa_logger.rb CHANGED
@@ -4,7 +4,6 @@ require_relative "testa_logger/logger"
4
4
  require_relative "testa_logger/io_persistence_error"
5
5
  require "active_support"
6
6
  require "active_support/core_ext/hash"
7
- require "ostruct"
8
7
  require "awesome_print"
9
8
  require "concurrent-ruby"
10
9
  require "json"
data/logger.iml CHANGED
@@ -22,7 +22,6 @@
22
22
  <orderEntry type="library" scope="PROVIDED" name="jmespath (v1.6.1, RVM: ruby-2.7.6) [gem]" level="application" />
23
23
  <orderEntry type="library" scope="PROVIDED" name="json (v2.6.2, RVM: ruby-2.7.6) [gem]" level="application" />
24
24
  <orderEntry type="library" scope="PROVIDED" name="minitest (v5.16.2, RVM: ruby-2.7.6) [gem]" level="application" />
25
- <orderEntry type="library" scope="PROVIDED" name="ostruct (v0.5.5, RVM: ruby-2.7.6) [gem]" level="application" />
26
25
  <orderEntry type="library" scope="PROVIDED" name="parallel (v1.22.1, RVM: ruby-2.7.6) [gem]" level="application" />
27
26
  <orderEntry type="library" scope="PROVIDED" name="parser (v3.1.2.1, RVM: ruby-2.7.6) [gem]" level="application" />
28
27
  <orderEntry type="library" scope="PROVIDED" name="rack (v2.2.4, RVM: ruby-2.7.6) [gem]" level="application" />
@@ -43,21 +42,21 @@
43
42
  <option name="myRootTask">
44
43
  <RakeTaskImpl id="rake">
45
44
  <subtasks>
46
- <RakeTaskImpl description="Build testa_logger-0.1.1.gem into the pkg directory" fullCommand="build" id="build" />
45
+ <RakeTaskImpl description="Build testa_logger-0.1.6.gem into the pkg directory" fullCommand="build" id="build" />
47
46
  <RakeTaskImpl id="build">
48
47
  <subtasks>
49
- <RakeTaskImpl description="Generate SHA512 checksum if testa_logger-0.1.1.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
48
+ <RakeTaskImpl description="Generate SHA512 checksum if testa_logger-0.1.6.gem into the checksums directory" fullCommand="build:checksum" id="checksum" />
50
49
  </subtasks>
51
50
  </RakeTaskImpl>
52
51
  <RakeTaskImpl description="Remove any temporary products" fullCommand="clean" id="clean" />
53
52
  <RakeTaskImpl description="Remove any generated files" fullCommand="clobber" id="clobber" />
54
- <RakeTaskImpl description="Build and install testa_logger-0.1.1.gem into system gems" fullCommand="install" id="install" />
53
+ <RakeTaskImpl description="Build and install testa_logger-0.1.6.gem into system gems" fullCommand="install" id="install" />
55
54
  <RakeTaskImpl id="install">
56
55
  <subtasks>
57
- <RakeTaskImpl description="Build and install testa_logger-0.1.1.gem into system gems without network access" fullCommand="install:local" id="local" />
56
+ <RakeTaskImpl description="Build and install testa_logger-0.1.6.gem into system gems without network access" fullCommand="install:local" id="local" />
58
57
  </subtasks>
59
58
  </RakeTaskImpl>
60
- <RakeTaskImpl description="Create tag v0.1.1 and build and push testa_logger-0.1.1.gem to rubygems.org" fullCommand="release[remote]" id="release[remote]" />
59
+ <RakeTaskImpl description="Create tag v0.1.6 and build and push testa_logger-0.1.6.gem to rubygems.org" fullCommand="release[remote]" id="release[remote]" />
61
60
  <RakeTaskImpl description="Run tests" fullCommand="test" id="test" />
62
61
  <RakeTaskImpl description="" fullCommand="default" id="default" />
63
62
  <RakeTaskImpl description="" fullCommand="release" id="release" />
data/testa_logger.gemspec CHANGED
@@ -23,7 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_runtime_dependency "aws-sdk-s3"
24
24
  spec.add_runtime_dependency "concurrent-ruby"
25
25
  spec.add_runtime_dependency "json"
26
- spec.add_runtime_dependency "ostruct"
27
26
  spec.add_runtime_dependency "securerandom"
28
27
 
29
28
  # Specify which files should be added to the gem when it is released.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: testa_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - karlo.razumovic
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: ostruct
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :runtime
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: securerandom
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -132,6 +118,7 @@ files:
132
118
  - lib/testa_logger/log_device/period.rb
133
119
  - lib/testa_logger/logger.rb
134
120
  - lib/testa_logger/logger/dispatcher.rb
121
+ - lib/testa_logger/logger/options.rb
135
122
  - lib/testa_logger/logger/persistence.rb
136
123
  - lib/testa_logger/version.rb
137
124
  - logger.iml