Class: Lumberjack::Device::LogFile

Inherits:
Device::Writer
  • Object
show all
Defined in:
lib/lumberjack/device/log_file.rb

Overview

A file-based logging device that extends the Writer device with automatic log rotation capabilities. This device wraps Ruby's standard Logger::LogDevice to provide file size-based and time-based log rotation while maintaining compatibility with the Lumberjack device interface.

The device supports all the rotation features available in Ruby's Logger, including maximum file size limits, automatic rotation based on age, and automatic cleanup of old log files. This makes it suitable for production environments where log management is crucial.

Examples:

Basic file logging

device = Lumberjack::Device::LogFile.new("/var/log/app.log")

With size-based rotation (10MB files, keep 5 old files)

device = Lumberjack::Device::LogFile.new(
  "/var/log/app.log",
  shift_size: 10 * 1024 * 1024,  # 10MB
  shift_age: 5                    # Keep 5 old files
)

With daily rotation

device = Lumberjack::Device::LogFile.new(
  "/var/log/app.log",
  shift_age: "daily"
)

With weekly rotation

device = Lumberjack::Device::LogFile.new(
  "/var/log/app.log",
  shift_age: "weekly"
)

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(stream, options = {}) ⇒ LogFile

Initialize a new LogFile device with automatic log rotation capabilities. This constructor wraps Ruby's Logger::LogDevice while filtering options to only pass supported parameters, ensuring compatibility across Ruby versions.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lumberjack/device/log_file.rb', line 53

def initialize(stream, options = {})
  # Filter options to only include keyword arguments supported by Logger::LogDevice#initialize
  supported_kwargs = ::Logger::LogDevice.instance_method(:initialize).parameters
    .select { |type, _| type == :key || type == :keyreq }
    .map { |_, name| name }

  filtered_options = options.slice(*supported_kwargs)

  logdev = ::Logger::LogDevice.new(stream, **filtered_options)

  super(logdev, options)
end

Instance Method Details

#devIO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Expose the underlying stream.



79
80
81
# File 'lib/lumberjack/device/log_file.rb', line 79

def dev
  stream.dev
end

#pathString

Get the file system path of the current log file. This method provides access to the actual file path being written to, which is useful for monitoring, log analysis tools, or other file-based operations.



71
72
73
# File 'lib/lumberjack/device/log_file.rb', line 71

def path
  stream.filename
end

#reopen(logdev = nil) ⇒ Object



83
84
85
# File 'lib/lumberjack/device/log_file.rb', line 83

def reopen(logdev = nil)
  stream.reopen(logdev)
end