Class: ReeLogger::FileAppender

Inherits:
Appender
  • Object
show all
Includes:
Ree::LinkDSL
Defined in:
lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb

Constant Summary collapse

DEFAULTS =
{
  auto_flush: false,
  log_file_count: 10,
  log_file_size: 1048576 # 1.megabyte
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Appender

#formatter, #level

Instance Method Summary collapse

Constructor Details

#initialize(level, formatter, file_path, **opts) ⇒ FileAppender

Returns a new instance of FileAppender.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb', line 29

def initialize(level, formatter, file_path, **opts)
  super(
    level,
    formatter || DefaultFormatter.new
  )

  opts = DEFAULTS.merge(opts)

  unless File.exist?(file_path)
    FileUtils.mkdir_p(Pathname.new(file_path).parent.to_s)
    FileUtils.touch(file_path)
  end

  @auto_flush = opts[:auto_flush]
  @file = File.open(file_path, File::WRONLY | File::APPEND)

  @logger = Logger.new(
    @file, opts[:log_file_count], opts[:log_file_size]
  )
end

Instance Attribute Details

#auto_flushObject (readonly)

Returns the value of attribute auto_flush.



17
18
19
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb', line 17

def auto_flush
  @auto_flush
end

#fileObject (readonly)

Returns the value of attribute file.



17
18
19
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb', line 17

def file
  @file
end

#loggerObject (readonly)

Returns the value of attribute logger.



17
18
19
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb', line 17

def logger
  @logger
end

Instance Method Details

#append(event, progname = nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/ree_lib/packages/ree_logger/package/ree_logger/appenders/file_appender.rb', line 51

def append(event, progname = nil)
  message = @formatter.format(event, progname)
  logger << (message + "\n")
  file.flush if auto_flush

  nil
end