Is it possible to dynamically change the LogDevice of Ruby Logger?
If so, it would allow for some un-obtrusive changes to my existing codebase.
Currently Ruby Logger uses StringIO as the LogDevice:
@logDevice = StringIO.new("", "r+")
@log = Logger.new(@logDevice) // a reference to this is used by many objects
// both are instance vars
...
@log.info('some log') // Logging activity
...
// Before program ends, transmit logs to a server
Can LogDevice be dynamically changed to continue logging to a file?
(dynamic change because initially the filename is not known.)
Or if log device cannot be change can the StringIO object start writing to a file?
Instead of doing the above, I could write to a temporary log file, but wanted to check if the above can be done because it would be a less obstrusive change to the existing codebase.
The object you give to the logger just has to implement the ‘write’ and ‘close’ methods, so you can easily write your own ‘io’:
end
create the logger with an instance of that and keep a reference to the instance. Then, whenever you know the filename, just set it with the ‘file=’ method.