Module: Lumberjack::IOCompatibility
- Defined in:
- lib/lumberjack/io_compatibility.rb
Overview
IOCompatibility provides methods that allow a logger to be used as an IO-like stream. This enables loggers to be used anywhere an IO object is expected, such as for redirecting standard output/error or integrating with libraries that expect stream objects.
When used as a stream, all written values are logged with UNKNOWN severity and include timestamps and other standard log entry metadata. This is particularly useful for:
-
Capturing output from external libraries or subprocesses
-
Redirecting STDOUT/STDERR to logs
-
Providing a logging destination that conforms to IO interface expectations
The module implements the essential IO methods like write, puts, print, printf, flush, and close to provide broad compatibility with Ruby’s IO ecosystem.
Instance Method Summary collapse
-
#close ⇒ nil
Close the stream.
-
#closed? ⇒ Boolean
Check if the stream is closed.
-
#flush ⇒ nil
Flush any buffered output.
-
#print(*args) ⇒ nil
Concatentate strings into a single log entry.
-
#printf(format, *args) ⇒ nil
Write a formatted string to the log using sprintf-style formatting.
-
#puts(*args) ⇒ nil
Write multiple values to the log, each as a separate log entry with UNKNOWN severity.
-
#set_encoding(_encoding) ⇒ nil
private
Set the encoding for the stream.
-
#tty? ⇒ Boolean
private
Check if the stream is connected to a terminal (TTY).
-
#write(value) ⇒ Integer
Write a value to the log as a log entry.
Instance Method Details
#close ⇒ nil
Close the stream. This method is provided for IO compatibility but is a no-op. To actually close a logger, call close on the logger object itself, which will close the underlying logging device.
102 103 |
# File 'lib/lumberjack/io_compatibility.rb', line 102 def close end |
#closed? ⇒ Boolean
Check if the stream is closed. Always returns false since loggers using this module don’t maintain a closed state through this interface.
109 110 111 |
# File 'lib/lumberjack/io_compatibility.rb', line 109 def closed? false end |
#flush ⇒ nil
Flush any buffered output. This method is provided for IO compatibility but is a no-op since log entries are typically written immediately to the underlying device. The actual flushing behavior depends on the logging device being used.
94 95 |
# File 'lib/lumberjack/io_compatibility.rb', line 94 def flush end |
#print(*args) ⇒ nil
Concatentate strings into a single log entry. This mimics IO#print behavior by writing arguments without separators. If no arguments are given, writes the value of the global $_ variable.
65 66 67 68 69 70 71 72 |
# File 'lib/lumberjack/io_compatibility.rb', line 65 def print(*args) if args.empty? write($_) else write(args.join("")) end nil end |
#printf(format, *args) ⇒ nil
Write a formatted string to the log using sprintf-style formatting. The formatted result is logged as a single entry with UNKNOWN severity.
84 85 86 87 |
# File 'lib/lumberjack/io_compatibility.rb', line 84 def printf(format, *args) write(format % args) nil end |
#puts(*args) ⇒ nil
Write multiple values to the log, each as a separate log entry with UNKNOWN severity. This method mimics the behavior of IO#puts by writing each argument on a separate line.
49 50 51 52 53 54 |
# File 'lib/lumberjack/io_compatibility.rb', line 49 def puts(*args) args.each do |arg| write(arg) end nil end |
#set_encoding(_encoding) ⇒ nil
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.
Set the encoding for the stream. This method is provided for IO compatibility but is a no-op since loggers handle encoding internally through their devices and formatters.
130 131 |
# File 'lib/lumberjack/io_compatibility.rb', line 130 def set_encoding(_encoding) end |
#tty? ⇒ Boolean
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.
Check if the stream is connected to a terminal (TTY). Always returns false since loggers are not terminal devices, even when they write to STDOUT/STDERR. This method is required for complete IO compatibility.
119 120 121 |
# File 'lib/lumberjack/io_compatibility.rb', line 119 def tty? false end |
#write(value) ⇒ Integer
Write a value to the log as a log entry. The value will be recorded with UNKNOWN severity, ensuring it always appears in the log regardless of the current log level.
37 38 39 40 41 42 |
# File 'lib/lumberjack/io_compatibility.rb', line 37 def write(value) return 0 if value.nil? || value == "" self << value 1 end |