Class: Rspider::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rspider/Logger.rb

Overview

This class is written for Logging messages when program running.

Examples:

  • $LOGGER=Logger.new(“/var/run/log/cns.log”)

  • $LOGGER.log_msg(“Program inited!”)

  • .…

  • $LOGGER.log_msg(“Sockets inited”)

  • $LOGGER.flush_msg();

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Logger

path: the file path to storage the logs



26
27
28
29
30
31
# File 'lib/rspider/Logger.rb', line 26

def initialize(path)
	@max=10
	@log_path=path
	@msgs=[]
	puts "logs created :#{path}" if $DEBUG
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



23
24
25
# File 'lib/rspider/Logger.rb', line 23

def max
  @max
end

#msgsObject

Returns the value of attribute msgs.



23
24
25
# File 'lib/rspider/Logger.rb', line 23

def msgs
  @msgs
end

Instance Method Details

#flush_msgObject

sync the logs to hard disk



40
41
42
43
44
45
46
47
# File 'lib/rspider/Logger.rb', line 40

def flush_msg()
	open(@log_path,"a+") do |f|
#				f.puts "Generated at :"+Time.now.to_s+"\n"
		f.puts @msgs.join("\n")
	end
	@msgs=[]
	puts "logs flushed ." if $DEBUG
end

#log_msg(log, level = "NOTE") ⇒ Object

add a new log the log may not be sync to disk The Logger syncs every 100 messages automaticly



35
36
37
38
# File 'lib/rspider/Logger.rb', line 35

def log_msg(log,level="NOTE")
	@msgs.push(level+":"+Time.now.to_i.to_s+":"+log)
	flush_msg if (@msgs.length>@max) or level=="ERROR"
end