Class: Lumberjack::Device::Writer
- Inherits:
-
Lumberjack::Device
- Object
- Lumberjack::Device
- Lumberjack::Device::Writer
- Defined in:
- lib/lumberjack/device/writer.rb
Overview
A versatile logging device that writes formatted log entries to IO streams. This device serves as the foundation for most output-based logging, converting LogEntry objects into formatted strings using configurable templates and writing them to any IO-compatible stream.
The Writer device supports extensive customization through templates, encoding options, stream management, and error handling. It can write to files, console output, network streams, or any object that implements the IO interface.
Templates can be either string-based (compiled into Template objects) or callable objects (Procs, lambdas) for maximum flexibility. The device handles character encoding, whitespace normalization, and provides robust error recovery when stream operations fail.
Direct Known Subclasses
Constant Summary collapse
- EDGE_WHITESPACE_PATTERN =
/\A\s|[ \t\f\v][\r\n]*\z/
Instance Attribute Summary collapse
-
#stream ⇒ IO
The underlying stream object that is being written to.
Instance Method Summary collapse
-
#close ⇒ void
Close the underlying stream and release any associated resources.
-
#datetime_format ⇒ String?
Get the current datetime format from the template if supported.
-
#datetime_format=(format) ⇒ void
Set the datetime format on the template if supported.
-
#dev ⇒ IO
private
Access the underlying IO stream for direct manipulation or compatibility with code expecting Logger device interface.
-
#flush ⇒ void
Flush the underlying stream to ensure all buffered data is written to the destination.
-
#initialize(stream, options = {}) ⇒ Writer
constructor
Initialize a new Writer device with configurable formatting and stream options.
-
#path ⇒ String?
Get the file system path of the underlying stream if available.
-
#write(entry) ⇒ void
Write a log entry to the stream with automatic formatting and error handling.
Methods inherited from Lumberjack::Device
Constructor Details
#initialize(stream, options = {}) ⇒ Writer
Initialize a new Writer device with configurable formatting and stream options. The device supports multiple template types, encoding control, and stream behavior configuration for flexible output handling.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lumberjack/device/writer.rb', line 55 def initialize(stream, = {}) @stream = stream @stream.sync = true if @stream.respond_to?(:sync=) && [:autoflush] != false @binmode = [:binmode] if [:standard_logger_formatter] @template = Template::StandardFormatterTemplate.new([:standard_logger_formatter]) else template = [:template] template = TemplateRegistry.template(template, ) if template.is_a?(Symbol) @template = if template.respond_to?(:call) template else Template.new( template, additional_lines: [:additional_lines], time_format: [:time_format], attribute_format: [:attribute_format], colorize: [:colorize] ) end end end |
Instance Attribute Details
#stream ⇒ IO
The underlying stream object that is being written to.
167 168 169 |
# File 'lib/lumberjack/device/writer.rb', line 167 def stream @stream end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Close the underlying stream and release any associated resources. This method ensures all buffered data is flushed before closing the stream, providing clean shutdown behavior for file handles and network connections.
109 110 111 112 |
# File 'lib/lumberjack/device/writer.rb', line 109 def close flush stream.close end |
#datetime_format ⇒ String?
Get the current datetime format from the template if supported. Returns the format string used for timestamp formatting in log entries.
128 129 130 |
# File 'lib/lumberjack/device/writer.rb', line 128 def datetime_format @template.datetime_format if @template.respond_to?(:datetime_format) end |
#datetime_format=(format) ⇒ void
This method returns an undefined value.
Set the datetime format on the template if supported. This allows dynamic reconfiguration of timestamp formatting without recreating the device.
138 139 140 141 142 |
# File 'lib/lumberjack/device/writer.rb', line 138 def datetime_format=(format) if @template.respond_to?(:datetime_format=) @template.datetime_format = format end end |
#dev ⇒ IO
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.
Access the underlying IO stream for direct manipulation or compatibility with code expecting Logger device interface. This method provides the raw stream object for advanced use cases.
150 151 152 |
# File 'lib/lumberjack/device/writer.rb', line 150 def dev stream end |
#flush ⇒ void
This method returns an undefined value.
Flush the underlying stream to ensure all buffered data is written to the destination. This method is safe to call on streams that don’t support flushing, making it suitable for various IO types.
119 120 121 |
# File 'lib/lumberjack/device/writer.rb', line 119 def flush stream.flush if stream.respond_to?(:flush) end |
#path ⇒ String?
Get the file system path of the underlying stream if available. This method is useful for monitoring, log rotation, or any operations that need to work with the actual file path.
160 161 162 |
# File 'lib/lumberjack/device/writer.rb', line 160 def path stream.path if stream.respond_to?(:path) end |
#write(entry) ⇒ void
This method returns an undefined value.
Write a log entry to the stream with automatic formatting and error handling. The entry is converted to a string using the configured template, processed for encoding and whitespace, and written to the stream with robust error recovery.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/lumberjack/device/writer.rb', line 90 def write(entry) string = (entry.is_a?(LogEntry) ? @template.call(entry) : entry) return if string.nil? if !@binmode && string.encoding != Encoding::UTF_8 string = string.encode("UTF-8", invalid: :replace, undef: :replace) end string = string.strip if string.match?(EDGE_WHITESPACE_PATTERN) return if string.length == 0 || string == Lumberjack::LINE_SEPARATOR write_to_stream(string) end |