loggerx 0.3.2 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/loggerx/loggerx.rb +90 -176
- data/lib/loggerx/loggerxcm.rb +17 -13
- data/lib/loggerx/version.rb +1 -1
- data/lib/loggerx.rb +1 -0
- metadata +1 -2
- data/lib/loggerx/loggerx.code-workspace +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29aa9e7ab752a0153a70eb61c1bc171f459a8bf617a8e38e7a949ea524aa2c56
|
4
|
+
data.tar.gz: 3e6acd787505a13975d7d81b21309808dc340302cfeae2cf07590ca55ee4dfaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4efaeecbfd9d0e01617362ddf5dfd734b7cdc4f213ac923e3537b67b2f2b879c6e82e7beef9f0bace475ea8964c201c693906032a104f2ba2adf91c801e75724
|
7
|
+
data.tar.gz: e930573db06a8d9a1524178322a57f173b4212c0f235a4a34dc9c84024853b788db22345a61e4ac3dea7b213e0e3b5dc149c203a8751d931fc335212d90d49e4
|
data/lib/loggerx/loggerx.rb
CHANGED
@@ -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
|
-
|
15
|
+
@valid = false
|
16
16
|
|
17
|
-
def
|
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
|
-
@
|
21
|
-
@
|
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
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
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
|
-
|
57
|
-
@log_stdout
|
58
|
-
end
|
54
|
+
@valid = true
|
59
55
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|
-
|
71
|
-
rescue StandardError
|
72
|
-
|
68
|
+
log_stdout = Logger.new($stdout)
|
69
|
+
rescue StandardError => exc
|
70
|
+
pust exc.message
|
71
|
+
@error_count += 1
|
73
72
|
end
|
74
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
-
|
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
|
97
|
-
@log_file&.
|
98
|
-
@log_stdout&.
|
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(
|
104
|
-
@log_file
|
105
|
-
@log_stdout
|
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
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
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
|
-
@
|
136
|
-
|
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
|
-
@
|
148
|
-
|
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
|
-
@
|
155
|
-
|
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
|
-
@
|
162
|
-
|
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
|
-
@
|
169
|
-
|
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
|
-
@
|
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
|
-
|
243
|
-
|
244
|
-
|
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
|
-
|
279
|
-
|
280
|
-
end
|
193
|
+
@valid = false
|
194
|
+
# @log_stdout&.close
|
281
195
|
end
|
282
196
|
end
|
283
|
-
end
|
197
|
+
end
|
data/lib/loggerx/loggerxcm.rb
CHANGED
@@ -23,15 +23,19 @@ module Loggerx
|
|
23
23
|
require 'fileutils'
|
24
24
|
require 'stringio'
|
25
25
|
|
26
|
-
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
|
27
27
|
@log_file = nil
|
28
28
|
@log_stdout = nil
|
29
29
|
@stdout_backup = $stdout
|
30
|
-
@stringio = StringIO.new(+
|
30
|
+
@stringio = StringIO.new(+'', 'w+')
|
31
31
|
|
32
32
|
@valid = false
|
33
33
|
|
34
34
|
class << self
|
35
|
+
def valid?
|
36
|
+
@valid
|
37
|
+
end
|
38
|
+
|
35
39
|
def ensure_quantum_log_files(log_dir_pn, limit_of_num_of_files, prefix)
|
36
40
|
list = log_dir_pn.children.select { |item| item.basename.to_s.match?("^#{prefix}") }.sort_by(&:mtime)
|
37
41
|
latest_index = list.size - limit_of_num_of_files
|
@@ -50,6 +54,7 @@ module Loggerx
|
|
50
54
|
fatal: Logger::FATAL,
|
51
55
|
unknown: Logger::UNKNOWN
|
52
56
|
}
|
57
|
+
@log_level = level_hs[level]
|
53
58
|
@log_dir_pn = Pathname.new(log_dir)
|
54
59
|
|
55
60
|
@limit_of_num_of_files ||= 5
|
@@ -66,15 +71,15 @@ module Loggerx
|
|
66
71
|
"#{msg}\n"
|
67
72
|
end
|
68
73
|
register_log_format(obj)
|
69
|
-
register_log_level(
|
74
|
+
register_log_level(@log_level)
|
70
75
|
|
71
76
|
@valid = true
|
72
77
|
|
73
|
-
Loggerxcm.fatal(
|
74
|
-
Loggerxcm.debug(
|
75
|
-
Loggerxcm.info(
|
76
|
-
Loggerxcm.warn(
|
77
|
-
Loggerxcm.error(
|
78
|
+
Loggerxcm.fatal('fatal')
|
79
|
+
Loggerxcm.debug('debug')
|
80
|
+
Loggerxcm.info('info')
|
81
|
+
Loggerxcm.warn('warn')
|
82
|
+
Loggerxcm.error('error')
|
78
83
|
# Loggerxcm.unknown("unknown")
|
79
84
|
end
|
80
85
|
|
@@ -110,10 +115,9 @@ module Loggerx
|
|
110
115
|
@log_stdout&.formatter = obj
|
111
116
|
end
|
112
117
|
|
113
|
-
def register_log_level(
|
114
|
-
|
115
|
-
@
|
116
|
-
@log_stdout&.level = level
|
118
|
+
def register_log_level(log_level)
|
119
|
+
# @log_file&.level = log_level
|
120
|
+
# @log_stdout&.level = log_level
|
117
121
|
#
|
118
122
|
# Log4r互換インターフェイス
|
119
123
|
# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
@@ -122,7 +126,7 @@ module Loggerx
|
|
122
126
|
def to_string(value)
|
123
127
|
if value.instance_of?(Array)
|
124
128
|
@stdout_backup ||= $stdout
|
125
|
-
@stringio ||= StringIO.new(+
|
129
|
+
@stringio ||= StringIO.new(+'', 'w+')
|
126
130
|
$stdout = @stringio
|
127
131
|
@stringio.rewind
|
128
132
|
str = @stringio.read
|
data/lib/loggerx/version.rb
CHANGED
data/lib/loggerx.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: loggerx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ykominami
|
@@ -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
|