Class: Lumberjack::Device::LogFile

Inherits:
Writer 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:

Direct Known Subclasses

DateRollingLogFile, SizeRollingLogFile

Constant Summary

Constants inherited from Writer

Writer::EDGE_WHITESPACE_PATTERN

Instance Attribute Summary

Attributes inherited from Writer

#stream

Instance Method Summary collapse

Methods inherited from Writer

#close, #datetime_format, #datetime_format=, #flush, #write

Methods inherited from Lumberjack::Device

#close, #datetime_format, #datetime_format=, #flush, open_device, #write

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.

Parameters:

  • stream (String, IO)

    The log destination. Can be a file path string or an IO object. When a string path is provided, the file will be created if it doesn’t exist, and parent directories will be created as needed.

  • options (Hash) (defaults to: {})

    Configuration options for the log device. All options supported by Logger::LogDevice are accepted, including:

    • :shift_age - Number of old files to keep, or rotation frequency (“daily”, “weekly”, “monthly”)

    • :shift_size - Maximum file size in bytes before rotation

    • :shift_period_suffix - Suffix to add to rotated log files

    • :binmode - Whether to open the log file in binary mode



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.

Returns:

  • (IO)


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.

Returns:

  • (String)

    The absolute file system path of the current log file



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