Class: DailyLogger::Adapter::File

Inherits:
Object
  • Object
show all
Defined in:
lib/daily_logger/adapter/file.rb

Defined Under Namespace

Classes: LogFileMutex

Instance Method Summary collapse

Constructor Details

#initialize(level, path) ⇒ File

Returns a new instance of File.



12
13
14
15
16
17
18
19
# 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

Instance Method Details

#closeObject



46
47
48
49
50
# File 'lib/daily_logger/adapter/file.rb', line 46

def close
  if !@log.nil? && !@log.closed?
    @log.close
  end
end

#write(level, msg) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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