Class: StrftimeLogger::Adapter::File

Inherits:
Object
  • Object
show all
Defined in:
lib/strftime_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.



11
12
13
14
15
16
17
# File 'lib/strftime_logger/adapter/file.rb', line 11

def initialize(level, path)
  @level = level
  @path = path
  @timestamp_path = Time.now.strftime(path)
  @mutex = LogFileMutex.new
  @log = open_logfile(@timestamp_path)
end

Instance Method Details

#closeObject



43
44
45
46
47
# File 'lib/strftime_logger/adapter/file.rb', line 43

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

#write(msg) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/strftime_logger/adapter/file.rb', line 19

def write(msg)
  begin
    @mutex.synchronize do
      if @log.nil? || !same_path?
        begin
          @timestamp_path = Time.now.strftime(@path)
          @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