Class: Fairy::Logger
- Inherits:
-
Object
- Object
- Fairy::Logger
- Defined in:
- lib/fairy/logger.rb
Constant Summary collapse
- LOG_FILE =
CONF.LOG_FILE
- FLUSH_INTERVAL =
CONF.LOG_FLUSH_INTERVAL
- MARK_INTERVAL =
CONF.LOG_MARK_INTERVAL
- LOG_ROTATE_INTERVAL =
CONF.LOG_ROTATE_INTERVAL
Instance Method Summary collapse
-
#initialize(path = LOG_FILE) ⇒ Logger
constructor
A new instance of Logger.
- #log_rotate ⇒ Object
- #message(message) ⇒ Object
- #messages(messages) ⇒ Object
- #open_log ⇒ Object
- #start_flusher ⇒ Object
Constructor Details
#initialize(path = LOG_FILE) ⇒ Logger
Returns a new instance of Logger.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fairy/logger.rb', line 15 def initialize(path = LOG_FILE) @log_file_path = path open_log @mutex = Mutex.new @buffered = false start_flusher end |
Instance Method Details
#log_rotate ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/fairy/logger.rb', line 83 def log_rotate @log_out.close log_back = "#{@log_file_path}.BAK-$$" File.rename(@log_file_path, log_back) open_log Thread.start do files = Dir.glob("#{@log_file_path}.[0-9]*").sort{|f1, f2| f2 <=> f1} while files.size >= CONF.LOG_ROTATE_N File.unlink(files.shift) end files.each do |f| fn = f.sub(/\.[0-9]+/){|n| n.succ} File.rename(f, fn) if /.*\.gz$/ !~ fn system("gzip #{fn}") end end File.rename(log_back, "#{@log_file_path}.0") end end |
#message(message) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/fairy/logger.rb', line 64 def () @mutex.synchronize do @log_out.puts @buffered = true end nil end |
#messages(messages) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/fairy/logger.rb', line 72 def () @mutex.synchronize do .each do |m| @log_out.puts m end @buffered = true end nil end |
#open_log ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fairy/logger.rb', line 26 def open_log begin @log_out = File.open(@log_file_path, "a+") rescue Errno::ENOENT ERR::Fail ERR::NoLogDir, @log_file_path rescue raise end @log_open_time = Time.now @log_out.puts @log_open_time.strftime("%Y/%m/%d %H:%M:%S -- LOGGER START --") @log_out.flush @marked_time = @log_open_time end |
#start_flusher ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/fairy/logger.rb', line 41 def start_flusher Thread.start do loop do sleep FLUSH_INTERVAL @mutex.synchronize do if @buffered @log_out.flush @buffered = false end now = Time.now if MARK_INTERVAL && now - @marked_time > MARK_INTERVAL @log_out.puts now.strftime("%Y/%m/%d %H:%M:%S -- MARK --") @log_out.flush @marked_time = now end if LOG_ROTATE_INTERVAL && now - @log_open_time > LOG_ROTATE_INTERVAL log_rotate end end end end end |