simple_cloud_logging 1.2.2 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +502 -0
- data/examples/assertion1.rb +26 -0
- data/examples/assertion2.rb +26 -0
- data/examples/assertion3.rb +26 -0
- data/examples/assertion4.rb +27 -0
- data/examples/examples.rb +81 -0
- data/examples/max_size.rb +18 -0
- data/examples/no-colors.rb +29 -0
- data/examples/show_caller.rb +26 -0
- data/examples/show_level.rb +26 -0
- data/lib/baselogger.rb +263 -111
- data/lib/dummylogger.rb +25 -26
- data/lib/locallogger.rb +63 -37
- data/lib/simple_cloud_logging.rb +35 -50
- metadata +39 -14
- data/examples/example01.rb +0 -27
- data/examples/example02.rb +0 -20
- data/examples/example03.rb +0 -24
- data/examples/example04.rb +0 -40
- data/lib/remotelogger.rb +0 -85
@@ -0,0 +1,29 @@
|
|
1
|
+
# overwrite the instance methods green, red, blue and yellow of the class String to make them non-effect.
|
2
|
+
# if you require the colorize gem after the logger setup, colors will be activated again.
|
3
|
+
|
4
|
+
require_relative '../lib/simple_cloud_logging'
|
5
|
+
|
6
|
+
# create a logger
|
7
|
+
l = BlackStack::LocalLogger.new("no-colors.log")
|
8
|
+
|
9
|
+
BlackStack::Logger.set(
|
10
|
+
colorize: false,
|
11
|
+
)
|
12
|
+
|
13
|
+
begin
|
14
|
+
l.blank_line
|
15
|
+
l.logs "Looking for number 3... "
|
16
|
+
i = 0
|
17
|
+
while i < 6
|
18
|
+
l.logs "#{i}... "
|
19
|
+
if i == 3
|
20
|
+
l.yes
|
21
|
+
else
|
22
|
+
l.no
|
23
|
+
end
|
24
|
+
i += 1
|
25
|
+
end
|
26
|
+
l.done
|
27
|
+
rescue => e
|
28
|
+
l.error(e)
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../lib/simple_cloud_logging'
|
2
|
+
|
3
|
+
# create a logger
|
4
|
+
l = BlackStack::LocalLogger.new("show_caller.log")
|
5
|
+
|
6
|
+
BlackStack::Logger.set(
|
7
|
+
show_nesting_caller: true,
|
8
|
+
)
|
9
|
+
|
10
|
+
begin
|
11
|
+
l.blank_line
|
12
|
+
l.logs "Looking for number 3... "
|
13
|
+
i = 0
|
14
|
+
while i < 6
|
15
|
+
l.logs "#{i}... "
|
16
|
+
if i == 3
|
17
|
+
l.yes
|
18
|
+
else
|
19
|
+
l.no
|
20
|
+
end
|
21
|
+
i += 1
|
22
|
+
end
|
23
|
+
l.done
|
24
|
+
rescue => e
|
25
|
+
l.error(e)
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../lib/simple_cloud_logging'
|
2
|
+
|
3
|
+
# create a logger
|
4
|
+
l = BlackStack::LocalLogger.new("show_level.log")
|
5
|
+
|
6
|
+
BlackStack::Logger.set(
|
7
|
+
show_nesting_level: true,
|
8
|
+
)
|
9
|
+
|
10
|
+
begin
|
11
|
+
l.blank_line
|
12
|
+
l.logs "Looking for number 3... "
|
13
|
+
i = 0
|
14
|
+
while i < 6
|
15
|
+
l.logs "#{i}... "
|
16
|
+
if i == 3
|
17
|
+
l.yes
|
18
|
+
else
|
19
|
+
l.no
|
20
|
+
end
|
21
|
+
i += 1
|
22
|
+
end
|
23
|
+
l.done
|
24
|
+
rescue => e
|
25
|
+
l.error(e)
|
26
|
+
end
|
data/lib/baselogger.rb
CHANGED
@@ -1,112 +1,264 @@
|
|
1
|
-
module BlackStack
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
self.
|
110
|
-
|
111
|
-
|
1
|
+
module BlackStack
|
2
|
+
|
3
|
+
module Logger
|
4
|
+
DEFAULT_MIN_SIZE = 10*1024*1024
|
5
|
+
DEFAULT_MAX_SIZE = 20*1024*1024
|
6
|
+
DEFAULT_SHOW_NESTING_LEVEL = false
|
7
|
+
DEFAULT_SHOW_NESTING_CALLER = false
|
8
|
+
DEFAULT_COLORIZE = true
|
9
|
+
DEFAULT_NESTING_ASSERTION = true
|
10
|
+
|
11
|
+
@@min_size = DEFAULT_MIN_SIZE
|
12
|
+
@@max_size = DEFAULT_MAX_SIZE
|
13
|
+
@@show_nesting_level = DEFAULT_SHOW_NESTING_LEVEL
|
14
|
+
@@show_nesting_caller = DEFAULT_SHOW_NESTING_CALLER
|
15
|
+
@@colorize = DEFAULT_COLORIZE
|
16
|
+
@@nesting_assertion = DEFAULT_NESTING_ASSERTION
|
17
|
+
|
18
|
+
def self.set(
|
19
|
+
min_size: DEFAULT_MIN_SIZE,
|
20
|
+
max_size: DEFAULT_MAX_SIZE,
|
21
|
+
show_nesting_level: DEFAULT_SHOW_NESTING_LEVEL,
|
22
|
+
show_nesting_caller: DEFAULT_SHOW_NESTING_CALLER,
|
23
|
+
colorize: DEFAULT_COLORIZE,
|
24
|
+
nesting_assertion: DEFAULT_NESTING_ASSERTION
|
25
|
+
)
|
26
|
+
err = []
|
27
|
+
|
28
|
+
# min_size must be a positive integer
|
29
|
+
err << "min_byes must be apositive integer." if !min_size.is_a?(Integer) || min_size.to_i < 0
|
30
|
+
err << "max_byes must be apositive integer." if !max_size.is_a?(Integer) || max_size.to_i < 0
|
31
|
+
err << "min_byes must be lower than max_size." if min_size.is_a?(Integer) && max_size.is_a?(Integer) && !(min_size < max_size)
|
32
|
+
err << "show_nesting_level must be a boolean." if ![true, false].include?(show_nesting_level)
|
33
|
+
err << "show_nesting_caller must be a boolean." if ![true, false].include?(show_nesting_caller)
|
34
|
+
err << "colorize must be a boolean." if ![true, false].include?(colorize)
|
35
|
+
err << "nesting_assertion must be a boolean." if ![true, false].include?(nesting_assertion)
|
36
|
+
|
37
|
+
@@min_size = min_size
|
38
|
+
@@max_size = max_size
|
39
|
+
@@show_nesting_level = show_nesting_level
|
40
|
+
@@show_nesting_caller = show_nesting_caller
|
41
|
+
@@colorize = colorize
|
42
|
+
@@nesting_assertion = nesting_assertion
|
43
|
+
|
44
|
+
if !colorize
|
45
|
+
# overwrite the instance methods green, red, blue and yellow of the class String from inside the constructor of another class, to make them non-effect.
|
46
|
+
String.class_eval do
|
47
|
+
define_method(:green) do
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
define_method(:red) do
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
define_method(:blue) do
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method(:yellow) do
|
60
|
+
self
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.min_size()
|
67
|
+
@@min_size
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.max_size()
|
71
|
+
@@max_size
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.show_nesting_level()
|
75
|
+
@@show_nesting_level
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.show_nesting_caller()
|
79
|
+
@@show_nesting_caller
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.colorize()
|
83
|
+
@@colorize
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.nesting_assertion()
|
87
|
+
@@nesting_assertion
|
88
|
+
end
|
89
|
+
end # module Logger
|
90
|
+
|
91
|
+
class LogNestingError < StandardError
|
92
|
+
attr_reader :message
|
93
|
+
|
94
|
+
def initialize(message)
|
95
|
+
@message = message
|
96
|
+
end
|
97
|
+
def to_s
|
98
|
+
@message
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class BaseLogger
|
103
|
+
NEWLINE = "\n\r"
|
104
|
+
attr_accessor :filename, :level, :level_children_lines, :level_open_callers
|
105
|
+
|
106
|
+
def initialize_attributes()
|
107
|
+
self.level = 0
|
108
|
+
self.level_children_lines = {}
|
109
|
+
self.level_open_callers = {}
|
110
|
+
end
|
111
|
+
|
112
|
+
def initialize(the_filename=nil)
|
113
|
+
self.filename = the_filename
|
114
|
+
self.initialize_attributes
|
115
|
+
end
|
116
|
+
|
117
|
+
def reset()
|
118
|
+
self.initialize_attributes
|
119
|
+
end
|
120
|
+
|
121
|
+
def log(s, datetime=nil)
|
122
|
+
t = !datetime.nil? ? datetime : Time.now
|
123
|
+
ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
|
124
|
+
ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
|
125
|
+
ltext = ltime + ": " + s + NEWLINE
|
126
|
+
# print
|
127
|
+
print ltext
|
128
|
+
# return
|
129
|
+
ltext
|
130
|
+
end
|
131
|
+
|
132
|
+
def blank_line
|
133
|
+
self.log('')
|
134
|
+
end
|
135
|
+
|
136
|
+
def logs(s, datetime=nil)
|
137
|
+
|
138
|
+
# Nesting assertion:
|
139
|
+
# - How to find out from which line number the method was called in Ruby?
|
140
|
+
# - Referneces:
|
141
|
+
# - https://stackoverflow.com/questions/37564928/how-to-find-out-from-which-line-number-the-method-was-called-in-ruby
|
142
|
+
# - https://ruby-doc.org/core-2.2.3/Thread/Backtrace/Location.html
|
143
|
+
#binding.pry if s == "Looking for number 3... "
|
144
|
+
caller = caller_locations(0..).last
|
145
|
+
#binding.pry if s == '1... '
|
146
|
+
#binding.pry if s == '4... '
|
147
|
+
# if the parent level was called from the same line, I am missing to close the parent.
|
148
|
+
if self.level_open_callers[self.level-1].to_s == caller.to_s
|
149
|
+
if Logger.nesting_assertion
|
150
|
+
raise LogNestingError.new("Log nesting assertion: You missed to close the log-level that you opened at #{caller.to_s}.")
|
151
|
+
end
|
152
|
+
else
|
153
|
+
self.level_open_callers[self.level] = caller.to_s
|
154
|
+
end
|
155
|
+
|
156
|
+
t = !datetime.nil? ? datetime : Time.now
|
157
|
+
ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
|
158
|
+
ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
|
159
|
+
ltime += " - caller #{caller.to_s.blue}" if Logger.show_nesting_caller
|
160
|
+
|
161
|
+
#binding.pry if self.level>0
|
162
|
+
# start in a new line, if this line is the first child of the parent level has opened lines
|
163
|
+
ltext = ""
|
164
|
+
ltext += NEWLINE if self.level > 0 && self.level_children_lines[self.level].to_i == 0
|
165
|
+
ltext += ltime + ": "
|
166
|
+
|
167
|
+
# increase the number of children of the parent level
|
168
|
+
self.level_children_lines[self.level] = self.level_children_lines[self.level].to_i + 1
|
169
|
+
|
170
|
+
self.level += 1
|
171
|
+
|
172
|
+
i=1
|
173
|
+
while (i<self.level)
|
174
|
+
ltext += "> "
|
175
|
+
i+=1
|
176
|
+
end
|
177
|
+
|
178
|
+
ltext += s
|
179
|
+
|
180
|
+
# print
|
181
|
+
print ltext
|
182
|
+
|
183
|
+
# return
|
184
|
+
ltext
|
185
|
+
end
|
186
|
+
|
187
|
+
def logf(s, datetime=nil)
|
188
|
+
ltext = ""
|
189
|
+
|
190
|
+
# clear the caller who opened the level that I am closing
|
191
|
+
self.level_open_callers[self.level-1] = nil
|
192
|
+
|
193
|
+
# if the parent level has children
|
194
|
+
if self.level_children_lines[self.level].to_i > 0
|
195
|
+
t = !datetime.nil? ? datetime : Time.now
|
196
|
+
ltime = t.strftime("%Y-%m-%d %H:%M:%S").to_s
|
197
|
+
ltime += " - level #{self.level.to_s.blue}" if Logger.show_nesting_level
|
198
|
+
|
199
|
+
ltext += "#{ltime}: "
|
200
|
+
|
201
|
+
i=1
|
202
|
+
while (i<self.level)
|
203
|
+
ltext += "> "
|
204
|
+
i+=1
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|
208
|
+
|
209
|
+
# since I am closing a level, set the number of children to 0
|
210
|
+
self.level_children_lines[self.level] = 0
|
211
|
+
|
212
|
+
# nesting assertion
|
213
|
+
if self.level <= 0
|
214
|
+
# force the level to 2, so I can use the loger to trace the error after raising the exceptiopn.
|
215
|
+
self.level = 1
|
216
|
+
# raise the exception
|
217
|
+
if Logger.nesting_assertion
|
218
|
+
raise LogNestingError.new("Log nesting assertion: You are closing 2 times the level started, or you missed to open that lavel, or you closed the another level in the middle 2 times.")
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
self.level -= 1
|
223
|
+
ltext += s + NEWLINE
|
224
|
+
|
225
|
+
# print
|
226
|
+
print ltext
|
227
|
+
# return
|
228
|
+
ltext
|
229
|
+
end
|
230
|
+
|
231
|
+
def done(details: nil)
|
232
|
+
if details.nil?
|
233
|
+
self.logf("done".green)
|
234
|
+
else
|
235
|
+
self.logf("done".green + " (#{details.to_s})")
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
def skip(details: nil)
|
240
|
+
if details.nil?
|
241
|
+
self.logf("skip".yellow)
|
242
|
+
else
|
243
|
+
self.logf("skip".yellow + " (#{details.to_s})")
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def error(e=nil)
|
248
|
+
self.logf("error".red) if e.nil?
|
249
|
+
self.logf("error: #{e.to_console}.".red) if !e.nil?
|
250
|
+
end
|
251
|
+
|
252
|
+
def yes()
|
253
|
+
self.logf("yes".green)
|
254
|
+
end
|
255
|
+
|
256
|
+
def ok()
|
257
|
+
self.logf("ok".green)
|
258
|
+
end
|
259
|
+
|
260
|
+
def no()
|
261
|
+
self.logf("no".yellow)
|
262
|
+
end
|
263
|
+
end # class BaseLogger
|
112
264
|
end # module BlackStack
|
data/lib/dummylogger.rb
CHANGED
@@ -1,27 +1,26 @@
|
|
1
|
-
module BlackStack
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# call the
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def logs
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def logf
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end # class LocalLogger
|
1
|
+
module BlackStack
|
2
|
+
class DummyLogger < BaseLogger
|
3
|
+
|
4
|
+
# call the parent class to set the attributes
|
5
|
+
# call the save method to store the new attributes into the data file
|
6
|
+
def reset()
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def log(s, datetime=nil)
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
def logs(s, datetime=nil)
|
15
|
+
end # def logs
|
16
|
+
|
17
|
+
#
|
18
|
+
def logf(s, datetime=nil)
|
19
|
+
end # def logf
|
20
|
+
|
21
|
+
#
|
22
|
+
def release()
|
23
|
+
end
|
24
|
+
|
25
|
+
end # class LocalLogger
|
27
26
|
end # module BlackStack
|
data/lib/locallogger.rb
CHANGED
@@ -1,38 +1,64 @@
|
|
1
|
-
module BlackStack
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
# call the
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
1
|
+
module BlackStack
|
2
|
+
class LocalLogger < BaseLogger
|
3
|
+
|
4
|
+
# call the parent class to set the attributes
|
5
|
+
# call the save method to store the new attributes into the data file
|
6
|
+
def reset()
|
7
|
+
super
|
8
|
+
BlackStack::LocalLoggerFactory::save(self.filename, self)
|
9
|
+
end
|
10
|
+
|
11
|
+
# store the min allowed bytes in the variable min
|
12
|
+
# store the max allowed bytes in the variable max
|
13
|
+
# get number of bytes of filename and store it the variable n
|
14
|
+
# if number of bytes (n) is higer than max, then truncate the first (max-min) bytes in the file.
|
15
|
+
# finally, add the text into the variable s at the end of the file.
|
16
|
+
def write(s)
|
17
|
+
# store the min allowed bytes in the variable min
|
18
|
+
min = Logger.min_size
|
19
|
+
# store the max allowed bytes in the variable max
|
20
|
+
max = Logger.max_size
|
21
|
+
# get number of bytes of filename and store it the variable n
|
22
|
+
n = File.exists?(self.filename) ? File.size(self.filename) : 0
|
23
|
+
# if number of bytes (n) is higer than max, then truncate the first (max-min) bytes in the file.
|
24
|
+
if n > max
|
25
|
+
# Read the content of the file
|
26
|
+
content = File.read(self.filename)
|
27
|
+
# Calculate the number of bytes to truncate
|
28
|
+
truncate_bytes = n - (max - min)
|
29
|
+
# Truncate the first (max-min) bytes in the file
|
30
|
+
truncated_content = content[truncate_bytes..-1]
|
31
|
+
# Write the truncated content back to the file
|
32
|
+
File.open(self.filename, 'w') { |file| file.write(truncated_content) }
|
33
|
+
end
|
34
|
+
#
|
35
|
+
File.open(self.filename, 'a') { |file| file.write(s) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def log(s, datetime=nil)
|
39
|
+
ltext = super(s, datetime)
|
40
|
+
self.write(ltext)
|
41
|
+
ltext
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
def logs(s, datetime=nil)
|
46
|
+
ltext = super(s, datetime)
|
47
|
+
self.write(ltext)
|
48
|
+
ltext
|
49
|
+
end # def logs
|
50
|
+
|
51
|
+
#
|
52
|
+
def logf(s, datetime=nil)
|
53
|
+
ltext = super(s, datetime)
|
54
|
+
self.write(ltext)
|
55
|
+
ltext
|
56
|
+
end # def logf
|
57
|
+
|
58
|
+
#
|
59
|
+
def release()
|
60
|
+
BlackStack::LocalLoggerFactory.release(self.filename)
|
61
|
+
end
|
62
|
+
|
63
|
+
end # class LocalLogger
|
38
64
|
end # module BlackStack
|