Class: LogHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/common/socket/log_helper.rb,
lib/common/socket/rlib/log_helper.rb

Overview

class for scan local log file support log level : [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]

Class Method Summary collapse

Class Method Details

.get_lines(log_path, log_name) ⇒ Object

get the total lines of the log file params log_path: logfile path log_name: logfile name return: the total lines of the log file



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/common/socket/log_helper.rb', line 21

def LogHelper.get_lines(log_path, log_name)
	log_file = log_path + "/" + log_name
	
	#param check
	if File::exists?(log_file) && File::file?(log_file) then
	  File.open(log_file) do |f|
	    lines = f.readlines.size
	    return lines
	  end
	    else
	      raise "logfile #{log_file} not exists or is not a regular file"
	end	
end

.get_match_lines(log_path, log_name, start_lines, log_level, pattern) ⇒ Object

LogHelper.get_match_lines “/home/space/space/log/”, “log1”, 1, [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”], “time”



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
110
111
# File 'lib/common/socket/log_helper.rb', line 75

def LogHelper.get_match_lines log_path, log_name, start_lines, log_level, pattern
	contents = get_tail_contents log_path, log_name, start_lines
	lines = []

	if contents == nil 
		return lines
	else
		contents.each{|line|
			checkable = false
			if log_level.size > 0
				log_level.each{|level|
					if line.include? level
						checkable = true
						break
					end
				}
			else
				checkable = true
			end


			if checkable
				#match = line.scan pattern
				#if match != nil && match.size > 0
				#	lines << line
				#end
                     			#match = pattern.match line
				match = line.match pattern
                     			if match != nil
					lines << line
				end
			end
		}

		return lines
	end
end

.get_match_times(log_path, log_name, start_lines, log_level, pattern) ⇒ Object

get the match times with the giving log level and regex pattern

-log_path: -log_name: -start_lines:from which line we search the log content, newlog=[start_lines, log_end] -log_level: an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels -pattern: a string or a regex pattern return: int, the match times with the giving log level and regex pattern

example: LogHelper.get_match_times(“./”, “chenjie.log”, 1, nil, “openapptrans”) LogHelper.get_match_times(“./”, “chenjie.log”, 1, nil, /(d4)-(d2)-(d2)/)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/common/socket/log_helper.rb', line 127

def LogHelper.get_match_times(log_path, log_name, start_lines, log_level, pattern)
	contents = get_tail_contents(log_path, log_name, start_lines)
	count = 0
	if contents == nil then 
	  return 0
	    else
	  contents.each{|line|
	    checkable = false
	    if log_level.size > 0 then 
	      #log_level is not nil, only the line contains the log_level, then should be check
	      log_level.each{|level|
	        if line.include?(level) then 
	          checkable = true
	          break;
	        end
	      }
	    	else
	      #log_level is nil, the line should be check
	      checkable = true  
	    end
	
	    if checkable then
	      matches = line.scan(pattern)
	      count += matches.size
	    end
	  }
	end
	
	return count
end

.get_match_value(log_path, log_name, start_lines, log_level, pattern) ⇒ Object

get the first match value, nil if no match found param log_path: log_name: last_lines:the lines read from tail to head log_level: an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels pattern: a string or a regex pattern return: a string firstly matches the pattern, nil if no match found

example: LogHelper.get_match_value(“./”, “chenjie.log”, 1, nil, /(d4)-(d2)-(d2)/) LogHelper.get_match_value(“./”, “chenjie.log”, 1, nil, “openapptrans”)



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/common/socket/log_helper.rb', line 219

def LogHelper.get_match_value(log_path, log_name, start_lines, log_level, pattern)
	contents = get_tail_contents(log_path, log_name, start_lines)
	if contents == nil then
	  return nil
	    else
	  contents.each{|line|
	    checkable = false
	    if log_level.size > 0 then
	      #log_level is not nil, only the line contains the log_level, then should be check
	      log_level.each{|level|
	        if line.include?(level) then
	          checkable = true
	          break;
	        end
	      }
	    	else
	      #log_level is nil, the line should be check
	      checkable = true
	    end
	
	    if checkable then
	      match = pattern.match(line)
	      if match != nil then 
	        return match[0]
	      end
	    end
	  }
	end
	
	return nil
end

.get_match_values(log_path, log_name, start_lines, log_level, pattern) ⇒ Object

get the match values with the giving log level and regex pattern param log_path: log_name: last_lines:the lines read from tail to head log_level: an array whoes value from [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]; nil for all levels pattern: a string or a regex pattern return: an array contains the match values with the giving log level and regex pattern

example: LogHelper.get_match_values(“./”, “chenjie.log”, 1, nil, /(d4)-(d2)-(d2)/) LogHelper.get_match_values(“./”, “chenjie.log”, 1, nil, “openapptrans”)



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/common/socket/log_helper.rb', line 172

def LogHelper.get_match_values(log_path, log_name, start_lines, log_level, pattern)
	contents = get_tail_contents(log_path, log_name, start_lines)
	values = Array.new
	if contents == nil then
	  return 0
	    else
	  contents.each{|line|
	    checkable = false
	    if log_level.size > 0 then
	      #log_level is not nil, only the line contains the log_level, then should be check
	      log_level.each{|level|
	        if line.include?(level) then
	          checkable = true
	          break;
	        end
	      }
	    	else
	      #log_level is nil, the line should be check
	      checkable = true
	    end
	
	    if checkable then
	      matches = line.scan(pattern)
	      matches.each{|m|
	        values << m
	      }
	    end
	  }
	end
	
	return values
end

.get_tail_contents(log_path, log_name, start_lines) ⇒ Object

get the last n lines’s contents of the log file param log_path log_name start_lines: from which line we search the log content, newlog=[start_lines, log_end] return: an array contains the last n lines’ contents



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/common/socket/log_helper.rb', line 46

def LogHelper.get_tail_contents(log_path, log_name, start_lines)
	last_lines = LogHelper.get_lines(log_path, log_name) - start_lines + 1
	#param check
	if last_lines <= 0 then 
	  raise "params error: last_lines should bigger than 0"
	end
	log_file = log_path + "/" + log_name
	
	contents = Array.new
	if File::exists?(log_file) && File::file?(log_file) then
	  system "tail -#{last_lines} #{log_file} > tmp_file"
	  File.open("tmp_file") do |f|
	    contents =  f.readlines
	    #remove the tmp file
	    system "rm tmp_file"
	    return contents
	  end
	else
	  raise "file #{log_file} not exists or is not a regular file"
	end
  
end

.level_decode(code) ⇒ Object

turn log_level code into log level array(helper method) used to solve remote parameter passing problem(pass string format parameter) param code: “0:0:0:0” to “1:1:1:1” return: [] to [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/common/socket/log_helper.rb', line 260

def LogHelper.level_decode(code)
	levels = Array.new
	arr = code.split(":")
	
	i = 0
	arr.each{|a|
	  if a == "1" then 
	    levels << LOG_LEVEL[i] 
	  end
	  i += 1
	}
	return levels
end

.level_encode(arr) ⇒ Object

translate log_level array into log level code(helper method) used to solve remote parameter passing problem(pass string format parameter) param code: “0:0:0:0” to “1:1:1:1” return: [] to [“DEBUG”, “NOTICE”, “WARNING”, “FATAL”]



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/common/socket/log_helper.rb', line 283

def LogHelper.level_encode arr
	code = ""
	if arr.include?("DEBUG") then
	  code += "1:"
	else
	  code += "0:"
	end
	
	if arr.include?("NOTICE") then
	      code += "1:"
	else
	      code += "0:"
	end
	
	if arr.include?("WARNING") then
	      code += "1:"
	else
	      code += "0:"
	end
	    
	if arr.include?("FATAL") then
	      code += "1"
	else
	      code += "0"
	end
	    
	return code
end