loggerx 0.1.1 → 0.2.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
2
  SHA256:
3
- metadata.gz: 219b8c59ad0049727c7e48f9f584b3bb11ac921be02321cfff210fbe9e0deb6e
4
- data.tar.gz: 39aef55cc7d8974ceb884c230931786cfcaa55c9348b7a77b7492da9788d0e90
3
+ metadata.gz: ef752536a65e45147353b942c929a06694dbe744ab5482ad9eb51eed26e9a739
4
+ data.tar.gz: 53f9e598326aa28f1ab8abdd06434c1f268863d9e77d7096c1552fdcb5138b87
5
5
  SHA512:
6
- metadata.gz: 7446cd0e041c013939338ccfa3d673a323a5dab2118b46a6071d62a8f73310e1867fcb12bb6619ad82d3b82a7e75765d3a2c20fc88febe3726bc55c94881a484
7
- data.tar.gz: f49d1ca3e640f7e35116c51f64539136cef6efef0a5af9ae699bb872cd0489c9271b38b6f8865165e710e87facd33d755186110d3ab78c64af126b64689280fa
6
+ metadata.gz: b6655835b8695913f29fd84e94fcf1700dba61951a24fb32e2d0cb567ff20c340068f0e4f0a56957ce7a1ba697fcace8878a31af69c4c2e3402f0765960ed4ad
7
+ data.tar.gz: 4e445fa4b53d19d360325da3e09f190c2959e867d74ed912598bec47aa48dc114b4159eba4f3b8a702a124fa65b091ffbeaea9dc0bfee1cddd693e3f56993535
data/.rubocop.yml ADDED
@@ -0,0 +1,36 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-rspec
5
+ - rubocop-rake
6
+
7
+ AllCops:
8
+ NewCops: enable
9
+ TargetRubyVersion: 3.1
10
+
11
+ Style/StringLiterals:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Style/StringLiteralsInInterpolation:
16
+ Enabled: true
17
+ EnforcedStyle: double_quotes
18
+
19
+ Layout/LineLength:
20
+ Max: 120
21
+
22
+ #追加
23
+ Style/FrozenStringLiteralComment:
24
+ Enabled: false
25
+
26
+ Style/Documentation:
27
+ Enabled: false
28
+ Style/ClassAndModuleChildren:
29
+ Enabled: false
30
+ Style/GuardClause:
31
+ Enabled: false
32
+ Style/IfUnlessModifier:
33
+ Enabled: false
34
+
35
+ Metrics/ClassLength:
36
+ Enabled: false
@@ -6,19 +6,19 @@ module Loggerx
6
6
  require "fileutils"
7
7
  require "stringio"
8
8
 
9
- LOG_FILENAME_BASE = "#{Time.now.strftime("%Y%m%d-%H%M%S")}.log"
9
+ LOG_FILENAME_BASE = "#{Time.now.strftime("%Y%m%d-%H%M%S")}.log".freeze
10
10
  @log_file = nil
11
11
  @log_stdout = nil
12
12
  @stdout_backup = $stdout
13
13
  @stringio = StringIO.new(+"", "w+")
14
- #@limit_of_num_of_files ||= 3
15
14
 
16
15
  attr_reader :error_count
17
16
 
18
17
  def initialize(prefix, fname, log_dir, stdout_flag, level = :info)
19
18
  return if @log_file
20
19
 
21
- @error_count = 0
20
+ @error_access_count = 0
21
+ @error_stderror_count = 0
22
22
  level_hs = {
23
23
  debug: Logger::DEBUG,
24
24
  info: Logger::INFO,
@@ -39,12 +39,14 @@ module Loggerx
39
39
  fname = prefix + LOG_FILENAME_BASE if fname == :default
40
40
  @log_file = setup_logger_file(log_dir, fname) if fname
41
41
 
42
- obj = proc do |_, _, _, msg| "#{msg}\n" end
42
+ obj = proc do |_, _, _, msg|
43
+ "#{msg}\n"
44
+ end
43
45
  register_log_format(obj)
44
46
  register_log_level(level_hs[level])
45
47
  end
46
48
 
47
- def get_logger_stdout
49
+ def logger_stdout
48
50
  @log_stdout
49
51
  end
50
52
 
@@ -56,11 +58,12 @@ module Loggerx
56
58
 
57
59
  def setup_logger_stdout(log_stdout)
58
60
  return log_stdout unless log_stdout.nil?
61
+
59
62
  begin
60
63
  # log_stdout = Logger.new($stdout)
61
- log_stdout = Logger.new(STDOUT)
62
- rescue
63
- @error_count += 1
64
+ log_stdout = Logger.new($stdout)
65
+ rescue StandardError
66
+ @error_stderror_count += 1
64
67
  end
65
68
  log_stdout
66
69
  end
@@ -71,9 +74,9 @@ module Loggerx
71
74
  begin
72
75
  log_file = Logger.new(filepath)
73
76
  rescue Errno::EACCES
74
- @error_count += 1
75
- rescue
76
- @error_count += 1
77
+ @error_access_count += 1
78
+ rescue StandardError
79
+ @error_stderror_count += 1
77
80
  end
78
81
  log_file
79
82
  end
@@ -86,7 +89,6 @@ module Loggerx
86
89
  def register_log_level(level)
87
90
  @log_file&.level = level
88
91
  @log_stdout&.level = level
89
- #
90
92
  # Log4r互換インターフェイス
91
93
  # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
92
94
  end
@@ -110,7 +112,7 @@ module Loggerx
110
112
  puts(value)
111
113
  str = error_sub(value)
112
114
  # puts(str) unless @log_stdout
113
- puts(str) #unless @log_stdout
115
+ puts(str) # unless @log_stdout
114
116
  true
115
117
  end
116
118
 
@@ -167,32 +169,29 @@ module Loggerx
167
169
 
168
170
  def hash_to_args(hash)
169
171
  prefix = hash["prefix"]
170
-
171
172
  log_dir_pn = Pathname.new(hash["log_dir"])
172
- #log_dir_pn = Pathname.new( obj[:log_dir] )
173
- # log_dir_pn = Pathname.new( obj[0] )
174
173
 
175
174
  stdout_flag_str = hash["stdout_flag"]
176
- if stdout_flag_str.instance_of?(String)
177
- case stdout_flag_str
178
- when "true"
179
- stdout_flag = true
175
+ stdout_flag = if stdout_flag_str.instance_of?(String)
176
+ case stdout_flag_str
177
+ when "true"
178
+ true
179
+ else
180
+ false
181
+ end
180
182
  else
181
- stdout_flag = false
183
+ stdout_flag_str
182
184
  end
183
- else
184
- stdout_flag = stdout_flag_str
185
- end
186
185
 
187
186
  fname_str = hash["fname"]
188
- case fname_str
189
- when "default"
190
- fname = fname_str.to_sym
191
- when "false"
192
- fname = false
193
- else
194
- fname = fname_str
195
- end
187
+ fname = case fname_str
188
+ when "default"
189
+ fname_str.to_sym
190
+ when "false"
191
+ false
192
+ else
193
+ fname_str
194
+ end
196
195
 
197
196
  level = hash["level"].to_sym
198
197
 
@@ -211,6 +210,7 @@ module Loggerx
211
210
 
212
211
  def init(prefix, fname, log_dir, stdout_flag, level = :info)
213
212
  return if @log_file
213
+
214
214
  @log_file = new(prefix, fname, log_dir, stdout_flag, level)
215
215
  end
216
216
 
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Loggerx
6
+ class Loggerxcm0
7
+ require "logger"
8
+ require "fileutils"
9
+ require "stringio"
10
+
11
+ LOG_FILENAME_BASE = "#{Time.now.strftime("%Y%m%d-%H%M%S")}.log".freeze
12
+ @log_file = nil
13
+ @log_stdout = nil
14
+ @stdout_backup = $stdout
15
+ @stringio = StringIO.new(+"", "w+")
16
+ # @limit_of_num_of_files ||= 3
17
+
18
+ class << self
19
+ def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
20
+ list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime)
21
+ latest_index = list.size - limit_of_num_of_files
22
+ list[0, latest_index].map(&:unlink) if latest_index.positive?
23
+ end
24
+
25
+ def init(prefix, fname, log_dir, stdout_flag, level = :info)
26
+ return if @log_file
27
+
28
+ @error_count = 0
29
+ level_hs = {
30
+ debug: Logger::DEBUG,
31
+ info: Logger::INFO,
32
+ warn: Logger::WARN,
33
+ error: Logger::ERROR,
34
+ fatal: Logger::FATAL,
35
+ unknown: Logger::UNKNOWN,
36
+ }
37
+ @log_dir_pn = Pathname.new(log_dir)
38
+
39
+ @limit_of_num_of_files ||= 5
40
+
41
+ ensure_quantum_log_files(@log_dir_pn, @limit_of_num_of_files, prefix)
42
+
43
+ @log_stdout = setup_logger_stdout(@log_stdout) if stdout_flag
44
+
45
+ fname = nil if fname == false
46
+ fname = prefix + LOG_FILENAME_BASE if fname == :default
47
+ @log_file = setup_logger_file(@log_file, log_dir, fname) if fname
48
+
49
+ obj = proc do |_, _, _, msg|
50
+ "#{msg}\n"
51
+ end
52
+ register_log_format(obj)
53
+ register_log_level(level_hs[level])
54
+ end
55
+
56
+ def valid?
57
+ !@log_file.nil?
58
+ end
59
+
60
+ def setup_logger_stdout(log_stdout)
61
+ return log_stdout unless log_stdout.nil?
62
+
63
+ begin
64
+ log_stdout = Logger.new($stdout)
65
+ rescue StandardError
66
+ @error_count += 1
67
+ end
68
+ log_stdout
69
+ end
70
+
71
+ def setup_logger_file(log_file, log_dir, fname)
72
+ filepath = Pathname.new(log_dir).join(fname)
73
+ if log_file.nil?
74
+ begin
75
+ log_file = Logger.new(filepath)
76
+ rescue Errno::EACCES
77
+ @error_count += 1
78
+ rescue StandardError
79
+ @error_count += 1
80
+ end
81
+ end
82
+ log_file
83
+ end
84
+
85
+ def register_log_format(obj)
86
+ @log_file&.formatter = obj
87
+ @log_stdout&.formatter = obj
88
+ end
89
+
90
+ def register_log_level(level)
91
+ @log_file&.level = level
92
+ @log_stdout&.level = level
93
+ #
94
+ # Log4r互換インターフェイス
95
+ # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
96
+ end
97
+
98
+ def to_string(value)
99
+ if value.instance_of?(Array)
100
+ @stdout_backup ||= $stdout
101
+ @stringio ||= StringIO.new(+"", "w+")
102
+ $stdout = @stringio
103
+ $stdout = @stdout_backup
104
+ @stringio.rewind
105
+ str = @stringio.read
106
+ @stringio.truncate(0)
107
+ str
108
+ else
109
+ value
110
+ end
111
+ end
112
+
113
+ def show(value)
114
+ puts(value)
115
+ str = error_sub(value)
116
+ # puts(str) unless @log_stdout
117
+ puts(str) # unless @log_stdout
118
+ true
119
+ end
120
+
121
+ def error_sub(value)
122
+ str = to_string(value)
123
+ @log_file&.error(str)
124
+ @log_stdout&.error(str)
125
+ str
126
+ end
127
+
128
+ def error(value)
129
+ error_sub(value)
130
+ true
131
+ end
132
+
133
+ def debug(value)
134
+ str = to_string(value)
135
+ @log_file&.debug(str)
136
+ @log_stdout&.debug(str)
137
+ true
138
+ end
139
+
140
+ def info(value)
141
+ str = to_string(value)
142
+ @log_file&.info(str)
143
+ @log_stdout&.info(str)
144
+ true
145
+ end
146
+
147
+ def warn(value)
148
+ str = to_string(value)
149
+ @log_file&.warn(str)
150
+ @log_stdout&.warn(str)
151
+ true
152
+ end
153
+
154
+ def fatal(value)
155
+ str = to_string(value)
156
+ @log_file&.fatal(str)
157
+ @log_stdout&.fatal(str)
158
+ true
159
+ end
160
+
161
+ def close
162
+ @log_file&.close
163
+ # @log_stdout&.close
164
+ end
165
+ end
166
+ end
167
+
168
+ class Loggerxcm < Loggerxcm0
169
+ end
170
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loggerx
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/loggerx.rb CHANGED
@@ -2,8 +2,10 @@
2
2
 
3
3
  require_relative "loggerx/version"
4
4
  require_relative "loggerx/loggerx"
5
+ require_relative "loggerx/loggerxcm"
5
6
 
6
7
  module Loggerx
7
8
  class Error < StandardError; end
9
+
8
10
  # Your code goes here...
9
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loggerx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ykominami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-28 00:00:00.000000000 Z
11
+ date: 2023-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utility class for Logging.
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - ".rspec"
21
+ - ".rubocop.yml"
21
22
  - ".tool-versions"
22
23
  - CHANGELOG.md
23
24
  - CODE_OF_CONDUCT.md
@@ -26,6 +27,7 @@ files:
26
27
  - Rakefile
27
28
  - lib/loggerx.rb
28
29
  - lib/loggerx/loggerx.rb
30
+ - lib/loggerx/loggerxcm.rb
29
31
  - lib/loggerx/version.rb
30
32
  - loggerx.code-workspace
31
33
  - sig/loggerx.rbs
@@ -35,6 +37,7 @@ licenses:
35
37
  metadata:
36
38
  source_code_uri: https://github.com/ykominami/loggerx
37
39
  changelog_uri: https://github.com/ykominami/loggerx/blob/main/CHANGELOG.md
40
+ rubygems_mfa_required: 'true'
38
41
  post_install_message:
39
42
  rdoc_options: []
40
43
  require_paths:
@@ -43,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
46
  requirements:
44
47
  - - ">="
45
48
  - !ruby/object:Gem::Version
46
- version: 3.2.0
49
+ version: 3.1.0
47
50
  required_rubygems_version: !ruby/object:Gem::Requirement
48
51
  requirements:
49
52
  - - ">="