class DailyLogger::Adapter::File
Public Class Methods
new(level, path)
click to toggle source
# File lib/daily_logger/adapter/file.rb, line 12 def initialize(level, path) @path = path @today = Time.now.strftime "%Y%m%d" @timestamp_path = "#{@path}.#{level}.log.#{@today}" # DIRの判定はここでするか? @mutex = LogFileMutex.new @log = open_logfile(@timestamp_path) end
Public Instance Methods
close()
click to toggle source
# File lib/daily_logger/adapter/file.rb, line 46 def close if !@log.nil? && !@log.closed? @log.close end end
write(level, msg)
click to toggle source
# File lib/daily_logger/adapter/file.rb, line 21 def write(level, msg) begin @mutex.synchronize do if @log.nil? || !same_date? begin @today = Time.now.strftime "%Y%m%d" @timestamp_path = "#{@path}.#{level}.log.#{@today}" @log.close rescue nil @log = create_logfile(@timestamp_path) rescue warn("log shifting failed. #{$!}") end end begin @log.write msg rescue warn("log writing failed. #{$!}") end end rescue Exception => ignored warn("log writing failed. #{ignored}") end end
Private Instance Methods
create_logfile(filename)
click to toggle source
# File lib/daily_logger/adapter/file.rb, line 63 def create_logfile(filename) begin f = ::File.open filename, (::File::WRONLY | ::File::APPEND | ::File::CREAT | ::File::EXCL) #f.binmode f.sync = true rescue Errno::EEXIST f = open_logfile(filename) end f end
open_logfile(filename)
click to toggle source
ファイルがない場合はnilを返す
# File lib/daily_logger/adapter/file.rb, line 54 def open_logfile(filename) return nil unless ::FileTest.exist? filename # ファイルは作らない f = ::File.open filename, (::File::WRONLY | ::File::APPEND) #f.binmode f.sync = true f end
same_date?()
click to toggle source
# File lib/daily_logger/adapter/file.rb, line 74 def same_date? @today == Time.now.strftime("%Y%m%d") end