loggerx 0.3.1 → 0.3.4

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: 3fd826254376e64dc953e21e9ba92bc7ae0fc1a9f53c293b19ed093616acce29
4
- data.tar.gz: 81d5fcb3dc14a34df39d491a88604a163041548baffa1f9d829645d8f2f95f95
3
+ metadata.gz: 618517da50647ce9e4961b0d6bbfab37bce4179661403664b23a2832da37ca5b
4
+ data.tar.gz: c93ade4b5ff8a52d8ca77b3264dc0bef3bb0b168b53b6a07efaa575737ae9fac
5
5
  SHA512:
6
- metadata.gz: c9af8636e84e6469b63737a4b884406a3288b94d1ed60538f170272ac558515cd867d1f05317ad4d720dd1a3b41d2f88d2534cf3c3ebc731c55d70f2ceee0886
7
- data.tar.gz: c5f6b9998cde8d0cc122786b20a7287120e07a215ac3182c24e116efb04cad518018ee4b0fc49db50bd8253e6f20d9b5bbaef88175022fd04588fadc89991889
6
+ metadata.gz: 652c43e5a99523f117322b2da7b6f2e3adb76d98fb17ad01cd7e6011c883bc87a78ef01945d34dc842a53f1b5a9bc7caf712580300c3109a17a7b0c697a106f5
7
+ data.tar.gz: 982c8a7b997d32bcfd713d356b844f71c2aba0b204ddadc0863b4a51b8bb738b504f61e8c297a30eb06c9d1173ae6cf282c2565c0db92d6d4c8bb6825fda411d
data/.tool-versions CHANGED
@@ -1 +1 @@
1
- ruby 3.1.2
1
+ ruby 3.3.4
@@ -2,31 +2,37 @@
2
2
 
3
3
  module Loggerx
4
4
  class Loggerx
5
+ # Implementation omitted
5
6
  require 'logger'
6
7
  require 'fileutils'
7
8
  require 'stringio'
8
9
 
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
14
 
15
- attr_reader :error_count
15
+ @valid = false
16
16
 
17
- def initialize(prefix, fname, log_dir, stdout_flag, level = :info)
17
+ def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
18
+ list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime)
19
+ latest_index = list.size - limit_of_num_of_files
20
+ list[0, latest_index].map(&:unlink) if latest_index.positive?
21
+ end
22
+
23
+ def initialize(prefix, fname, log_dir, stdout_flag, level: :info)
18
24
  return if @log_file
19
25
 
20
- @error_access_count = 0
21
- @error_stderror_count = 0
22
- level_hs = {
26
+ @error_count = 0
27
+ @level_hs = {
23
28
  debug: Logger::DEBUG,
24
29
  info: Logger::INFO,
25
30
  warn: Logger::WARN,
26
31
  error: Logger::ERROR,
27
32
  fatal: Logger::FATAL,
28
- unknown: Logger::UNKNOWN,
33
+ unknown: Logger::UNKNOWN
29
34
  }
35
+ @log_level = @level_hs[level]
30
36
  @log_dir_pn = Pathname.new(log_dir)
31
37
 
32
38
  @limit_of_num_of_files ||= 5
@@ -37,72 +43,60 @@ module Loggerx
37
43
 
38
44
  fname = nil if fname == false
39
45
  fname = prefix + LOG_FILENAME_BASE if fname == :default
40
- @log_file = setup_logger_file(log_dir, fname) if fname
41
- # register_log_format(obj)
42
- # register_log_level(level_hs[level])
43
- level_hs[level]
44
- end
46
+ @log_file = setup_logger_file(@log_file, log_dir, fname) if fname
45
47
 
46
- def formatter
47
- @log_file&.formatter
48
- # @log_stdout&.formatter
49
- end
50
-
51
- def formatter=(obj)
52
- @log_file&.formatter = obj
53
- @log_stdout&.formatter = obj
54
- end
48
+ obj = proc do |_, _, _, msg|
49
+ "#{msg}\n"
50
+ end
51
+ register_log_format(obj)
52
+ register_log_level(@log_level)
55
53
 
56
- def logger_stdout
57
- @log_stdout
58
- end
54
+ @valid = true
59
55
 
60
- def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
61
- list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime)
62
- latest_index = list.size - limit_of_num_of_files
63
- list[0, latest_index].map(&:unlink) if latest_index.positive?
56
+ Loggerxcm.fatal('fatal')
57
+ Loggerxcm.debug('debug')
58
+ Loggerxcm.info('info')
59
+ Loggerxcm.warn('warn')
60
+ Loggerxcm.error('error')
61
+ # Loggerxcm.unknown("unknown")
64
62
  end
65
63
 
66
64
  def setup_logger_stdout(log_stdout)
67
65
  return log_stdout unless log_stdout.nil?
68
66
 
69
67
  begin
70
- log ||= ActiveSupport::TaggedLogging.new($stdout)
71
- rescue StandardError
72
- @error_stderror_count += 1
68
+ log_stdout = Logger.new($stdout)
69
+ rescue StandardError => exc
70
+ pust exc.message
71
+ @error_count += 1
73
72
  end
74
- log
73
+ log_stdout
75
74
  end
76
75
 
77
- def setup_logger_file(log_dir, fname)
76
+ def setup_logger_file(log_file, log_dir, fname)
78
77
  filepath = Pathname.new(log_dir).join(fname)
79
- begin
80
- log ||= ActiveSupport::TaggedLogging.new(filepath)
81
- rescue Errno::EACCES
82
- @error_access_count += 1
83
- rescue StandardError
84
- @error_stderror_count += 1
78
+ if log_file.nil?
79
+ begin
80
+ log_file = Logger.new(filepath.to_s)
81
+ rescue Errno::EACCES
82
+ @error_count += 1
83
+ rescue StandardError => exc
84
+ puts exc
85
+ @error_count += 1
86
+ end
85
87
  end
86
- log
87
- end
88
-
89
- def level
90
- @log_file&.level
91
- # @log_stdout&.level
92
- # Log4r互換インターフェイス
93
- # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
88
+ log_file
94
89
  end
95
90
 
96
- def level=(value)
97
- @log_file&.level = value
98
- @log_stdout&.level = value
99
- # Log4r互換インターフェイス
100
- # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
91
+ def register_log_format(obj)
92
+ @log_file&.formatter = obj
93
+ @log_stdout&.formatter = obj
101
94
  end
102
95
 
103
- def register_log_level(level)
104
- @log_file&.level = level
105
- @log_stdout&.level = level
96
+ def register_log_level(log_level)
97
+ @log_file.level = log_level
98
+ @log_stdout.level = log_level
99
+ #
106
100
  # Log4r互換インターフェイス
107
101
  # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
108
102
  end
@@ -112,10 +106,10 @@ module Loggerx
112
106
  @stdout_backup ||= $stdout
113
107
  @stringio ||= StringIO.new(+'', 'w+')
114
108
  $stdout = @stringio
115
- $stdout = @stdout_backup
116
109
  @stringio.rewind
117
110
  str = @stringio.read
118
111
  @stringio.truncate(0)
112
+ $stdout = @stdout_backup
119
113
  str
120
114
  else
121
115
  value
@@ -123,17 +117,27 @@ module Loggerx
123
117
  end
124
118
 
125
119
  def show(value)
126
- puts(value)
127
- str = error_sub(value)
128
- # puts(str) unless @log_stdout
129
- puts(str) # unless @log_stdout
120
+ if @valid
121
+ if value.instance_of?(Array)
122
+ value.map do |v|
123
+ str = v.to_s
124
+ Util.puts_valid_str(str)
125
+ end
126
+ else
127
+ Util.puts_valid_str(str)
128
+ end
129
+ str = error_sub(value)
130
+ Util.puts_valid_str(str)
131
+ end
130
132
  true
131
133
  end
132
134
 
133
135
  def error_sub(value)
134
136
  str = to_string(value)
135
- @log_file&.error(str)
136
- @log_stdout&.error(str)
137
+ if @valid
138
+ @log_file&.error(str)
139
+ @log_stdout&.error(str)
140
+ end
137
141
  str
138
142
  end
139
143
 
@@ -144,140 +148,50 @@ module Loggerx
144
148
 
145
149
  def debug(value)
146
150
  str = to_string(value)
147
- @log_file&.debug(str)
148
- @log_stdout&.debug(str)
151
+ if @valid
152
+ @log_file&.debug(str)
153
+ @log_stdout&.debug(str)
154
+ end
155
+
149
156
  true
150
157
  end
151
158
 
152
159
  def info(value)
153
160
  str = to_string(value)
154
- @log_file&.info(str)
155
- @log_stdout&.info(str)
161
+ if @valid
162
+ @log_file&.info(str)
163
+ @log_stdout&.info(str)
164
+ end
156
165
  true
157
166
  end
158
167
 
159
168
  def warn(value)
160
169
  str = to_string(value)
161
- @log_file&.warn(str)
162
- @log_stdout&.warn(str)
170
+ if @valid
171
+ @log_file&.warn(str)
172
+ @log_stdout&.warn(str)
173
+ end
163
174
  true
164
175
  end
165
176
 
166
177
  def fatal(value)
167
178
  str = to_string(value)
168
- @log_file&.fatal(str)
169
- @log_stdout&.fatal(str)
179
+ if @valid
180
+ @log_file&.fatal(str)
181
+ @log_stdout&.fatal(str)
182
+ end
170
183
  true
171
184
  end
172
185
 
173
186
  def close
174
- @log_file&.close
175
- # @log_stdout&.close
176
- end
177
-
178
- class << self
179
- def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
180
- @log_file.ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
181
- end
182
-
183
- def hash_to_args(hash)
184
- prefix = hash['prefix']
185
- log_dir_pn = Pathname.new(hash['log_dir'])
186
-
187
- stdout_flag_str = hash['stdout_flag']
188
- stdout_flag = if stdout_flag_str.instance_of?(String)
189
- case stdout_flag_str
190
- when 'true'
191
- true
192
- else
193
- false
194
- end
195
- else
196
- stdout_flag_str
197
- end
198
-
199
- fname_str = hash['fname']
200
- fname = case fname_str
201
- when 'default'
202
- fname_str.to_sym
203
- when 'false'
204
- false
205
- else
206
- fname_str
207
- end
208
-
209
- level = hash['level'].to_sym
210
-
211
- [prefix, fname, log_dir_pn, stdout_flag, level]
212
- end
213
-
214
- def create_by_hash(hash)
215
- prefix, fname, log_dir_pn, stdout_flag, level = hash_to_args(hash)
216
- Loggerx.new(prefix, fname, log_dir_pn, stdout_flag, level)
217
- end
218
-
219
- def init_by_hash(hash)
220
- prefix, fname, log_dir_pn, stdout_flag, level = hash_to_args(hash)
221
- init(prefix, fname, log_dir_pn, stdout_flag, level)
222
- end
223
-
224
- def init(prefix, fname, log_dir, stdout_flag, level = :info)
225
- return if @log_file
226
-
227
- @log_file = new(prefix, fname, log_dir, stdout_flag, level)
228
- end
229
-
230
- def setup_logger_stdout(log_stdout)
231
- @log_file.setup_logger_stdout(log_stdout)
232
- end
233
-
234
- def setup_logger_file(log_dir, fname)
235
- @log_file.setup_logger_file(log_dir, fname)
236
- end
237
-
238
- def register_log_format(obj)
239
- @log_file.register_log_format(obj)
240
- end
187
+ retrun unless @valid
241
188
 
242
- def register_log_level(level)
243
- @log_file.register_log_level(level)
244
- end
245
-
246
- def to_string(value)
247
- @log_file.to_string(value)
248
- end
249
-
250
- def show(value)
251
- @log_file.show(value)
252
- end
253
-
254
- def error_sub(value)
255
- @log_file.error_sub(value)
256
- end
257
-
258
- def error(value)
259
- @log_file.error(value)
260
- end
261
-
262
- def debug(value)
263
- @log_file.debug(value)
264
- end
265
-
266
- def info(value)
267
- @log_file.info(value)
268
- end
269
-
270
- def warn(value)
271
- @log_file.warn(value)
272
- end
273
-
274
- def fatal(value)
275
- @log_file.fatal(value)
276
- end
189
+ @log_file&.close
190
+ @log_file = nil
191
+ @log_stdout = nil
277
192
 
278
- def close
279
- @log_file&.close
280
- end
193
+ @valid = false
194
+ # @log_stdout&.close
281
195
  end
282
196
  end
283
- end
197
+ end
@@ -3,19 +3,39 @@
3
3
  require 'pathname'
4
4
 
5
5
  module Loggerx
6
+ # The `Loggerx::Loggerxcm0` class is designed to provide logging functionality specific to the Mkspec framework.
7
+ # It encapsulates the logging mechanism, offering a unified interface for recording various levels of messages,
8
+ # such as debug, info, warning, and error. This class aims to facilitate debugging and tracking of the application's
9
+ # flow by providing detailed and structured log messages. It can be configured to log messages to different outputs,
10
+ # including standard output, files, or external logging services, depending on the needs of the framework.
11
+ #
12
+ # @example Logging a debug message
13
+ # Loggerx::Loggerxcm0.debug("This is a debug message")
14
+ #
15
+ # @example Logging an error message
16
+ # Loggerx::Loggerxcm0.error("This is an error message")
17
+ #
18
+ # This class may also support log rotation, filtering of log messages based on severity, and formatting of log messages
19
+ # to include timestamps, source identifiers, and other relevant information.
6
20
  class Loggerxcm0
21
+ # Implementation omitted
7
22
  require 'logger'
8
23
  require 'fileutils'
9
24
  require 'stringio'
10
25
 
11
- LOG_FILENAME_BASE = "#{Time.now.strftime("%Y%m%d-%H%M%S")}.log".freeze
26
+ # LOG_FILENAME_BASE = "#{Time.now.strftime('%Y%m%d-%H%M%S')}.log".freeze
12
27
  @log_file = nil
13
28
  @log_stdout = nil
14
29
  @stdout_backup = $stdout
15
30
  @stringio = StringIO.new(+'', 'w+')
16
- # @limit_of_num_of_files ||= 3
31
+
32
+ @valid = false
17
33
 
18
34
  class << self
35
+ def valid?
36
+ @valid
37
+ end
38
+
19
39
  def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
20
40
  list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime)
21
41
  latest_index = list.size - limit_of_num_of_files
@@ -32,8 +52,9 @@ module Loggerx
32
52
  warn: Logger::WARN,
33
53
  error: Logger::ERROR,
34
54
  fatal: Logger::FATAL,
35
- unknown: Logger::UNKNOWN,
55
+ unknown: Logger::UNKNOWN
36
56
  }
57
+ @log_level = level_hs[level]
37
58
  @log_dir_pn = Pathname.new(log_dir)
38
59
 
39
60
  @limit_of_num_of_files ||= 5
@@ -50,11 +71,16 @@ module Loggerx
50
71
  "#{msg}\n"
51
72
  end
52
73
  register_log_format(obj)
53
- register_log_level(level_hs[level])
54
- end
74
+ register_log_level(@log_level)
55
75
 
56
- def valid?
57
- !@log_file.nil?
76
+ @valid = true
77
+
78
+ Loggerxcm.fatal('fatal')
79
+ Loggerxcm.debug('debug')
80
+ Loggerxcm.info('info')
81
+ Loggerxcm.warn('warn')
82
+ Loggerxcm.error('error')
83
+ # Loggerxcm.unknown("unknown")
58
84
  end
59
85
 
60
86
  def setup_logger_stdout(log_stdout)
@@ -62,7 +88,8 @@ module Loggerx
62
88
 
63
89
  begin
64
90
  log_stdout = Logger.new($stdout)
65
- rescue StandardError
91
+ rescue StandardError => exc
92
+ pust exc.message
66
93
  @error_count += 1
67
94
  end
68
95
  log_stdout
@@ -72,10 +99,11 @@ module Loggerx
72
99
  filepath = Pathname.new(log_dir).join(fname)
73
100
  if log_file.nil?
74
101
  begin
75
- log_file = Logger.new(filepath)
76
- # rescue Errno::EACCES
77
- # @error_count += 1
78
- rescue StandardError
102
+ log_file = Logger.new(filepath.to_s)
103
+ rescue Errno::EACCES
104
+ @error_count += 1
105
+ rescue StandardError => exc
106
+ puts exc
79
107
  @error_count += 1
80
108
  end
81
109
  end
@@ -87,9 +115,10 @@ module Loggerx
87
115
  @log_stdout&.formatter = obj
88
116
  end
89
117
 
90
- def register_log_level(level)
91
- @log_file&.level = level
92
- @log_stdout&.level = level
118
+ def register_log_level(log_level)
119
+ {log_level.class} ==========="
120
+ # @log_file&.level = log_level
121
+ # @log_stdout&.level = log_level
93
122
  #
94
123
  # Log4r互換インターフェイス
95
124
  # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
@@ -100,10 +129,10 @@ module Loggerx
100
129
  @stdout_backup ||= $stdout
101
130
  @stringio ||= StringIO.new(+'', 'w+')
102
131
  $stdout = @stringio
103
- $stdout = @stdout_backup
104
132
  @stringio.rewind
105
133
  str = @stringio.read
106
134
  @stringio.truncate(0)
135
+ $stdout = @stdout_backup
107
136
  str
108
137
  else
109
138
  value
@@ -111,17 +140,27 @@ module Loggerx
111
140
  end
112
141
 
113
142
  def show(value)
114
- puts(value)
115
- str = error_sub(value)
116
- # puts(str) unless @log_stdout
117
- puts(str) # unless @log_stdout
143
+ if @valid
144
+ if value.instance_of?(Array)
145
+ value.map do |v|
146
+ str = v.to_s
147
+ Util.puts_valid_str(str)
148
+ end
149
+ else
150
+ Util.puts_valid_str(str)
151
+ end
152
+ str = error_sub(value)
153
+ Util.puts_valid_str(str)
154
+ end
118
155
  true
119
156
  end
120
157
 
121
158
  def error_sub(value)
122
159
  str = to_string(value)
123
- @log_file&.error(str)
124
- @log_stdout&.error(str)
160
+ if @valid
161
+ @log_file&.error(str)
162
+ @log_stdout&.error(str)
163
+ end
125
164
  str
126
165
  end
127
166
 
@@ -132,34 +171,49 @@ module Loggerx
132
171
 
133
172
  def debug(value)
134
173
  str = to_string(value)
135
- @log_file&.debug(str)
136
- @log_stdout&.debug(str)
174
+ if @valid
175
+ @log_file&.debug(str)
176
+ @log_stdout&.debug(str)
177
+ end
178
+
137
179
  true
138
180
  end
139
181
 
140
182
  def info(value)
141
183
  str = to_string(value)
142
- @log_file&.info(str)
143
- @log_stdout&.info(str)
184
+ if @valid
185
+ @log_file&.info(str)
186
+ @log_stdout&.info(str)
187
+ end
144
188
  true
145
189
  end
146
190
 
147
191
  def warn(value)
148
192
  str = to_string(value)
149
- @log_file&.warn(str)
150
- @log_stdout&.warn(str)
193
+ if @valid
194
+ @log_file&.warn(str)
195
+ @log_stdout&.warn(str)
196
+ end
151
197
  true
152
198
  end
153
199
 
154
200
  def fatal(value)
155
201
  str = to_string(value)
156
- @log_file&.fatal(str)
157
- @log_stdout&.fatal(str)
202
+ if @valid
203
+ @log_file&.fatal(str)
204
+ @log_stdout&.fatal(str)
205
+ end
158
206
  true
159
207
  end
160
208
 
161
209
  def close
210
+ retrun unless @valid
211
+
162
212
  @log_file&.close
213
+ @log_file = nil
214
+ @log_stdout = nil
215
+
216
+ @valid = false
163
217
  # @log_stdout&.close
164
218
  end
165
219
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loggerx
4
- VERSION = '0.3.1'
4
+ VERSION = '0.3.4'
5
5
  end
data/lib/loggerx.rb CHANGED
@@ -8,4 +8,5 @@ module Loggerx
8
8
  class Error < StandardError; end
9
9
 
10
10
  # Your code goes here...
11
+ LOG_FILENAME_BASE = "#{Time.now.strftime('%Y%m%d-%H%M%S')}.log".freeze
11
12
  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.3.1
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - ykominami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-19 00:00:00.000000000 Z
11
+ date: 2024-12-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Utility class for Logging.
14
14
  email:
@@ -27,7 +27,6 @@ files:
27
27
  - Rakefile
28
28
  - SECURITY.md
29
29
  - lib/loggerx.rb
30
- - lib/loggerx/loggerx.code-workspace
31
30
  - lib/loggerx/loggerx.rb
32
31
  - lib/loggerx/loggerxcm.rb
33
32
  - lib/loggerx/version.rb
@@ -55,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
54
  - !ruby/object:Gem::Version
56
55
  version: '0'
57
56
  requirements: []
58
- rubygems_version: 3.5.14
57
+ rubygems_version: 3.5.11
59
58
  signing_key:
60
59
  specification_version: 4
61
60
  summary: Utility class for Logging.
@@ -1,8 +0,0 @@
1
- {
2
- "folders": [
3
- {
4
- "path": "../.."
5
- }
6
- ],
7
- "settings": {}
8
- }