Class: Rspider::Logger
- Inherits:
-
Object
- Object
- Rspider::Logger
- 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
-
#max ⇒ Object
Returns the value of attribute max.
-
#msgs ⇒ Object
Returns the value of attribute msgs.
Instance Method Summary collapse
-
#flush_msg ⇒ Object
sync the logs to hard disk.
-
#initialize(path) ⇒ Logger
constructor
path: the file path to storage the logs.
-
#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.
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
#max ⇒ Object
Returns the value of attribute max.
23 24 25 |
# File 'lib/rspider/Logger.rb', line 23 def max @max end |
#msgs ⇒ Object
Returns the value of attribute msgs.
23 24 25 |
# File 'lib/rspider/Logger.rb', line 23 def msgs @msgs end |
Instance Method Details
#flush_msg ⇒ Object
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 |