Issue #11955 has been updated by Richard Schneeman.


Sorry for the delay, bugs.ruby-lang.org does not send me emails for some reason.

The first patch I attached returned `@logdev.dev` which is the IO object. It was discussed that to do this Logger must know too much about the logdev interface and it would be simpler to expose the logdev instead.

Either approach will work for my use cases. Now that 2.4.0 preview is out is there a feature freeze? Is there any chance this will come out in time for christmas?


----------------------------------------
Feature #11955: Expose Object that Receives logs in Logger
https://bugs.ruby-lang.org/issues/11955#change-59285

* Author: Richard Schneeman
* Status: Assigned
* Priority: Normal
* Assignee: Naotoshi Seo
----------------------------------------
I need to be able to perform logic based on the destination of a current logger, this is currently not possible without `instance_variable_get`. Why would you need to see what destination a logger is going to? There is a common pattern in long lived programs like webservers. You want logs on disk for later reference, but you also want them in STDOUT to make development and debugging easier. Rails does this in development mode. Since there is no way to see if a logger is already going to STDOUT, it gets extended to log to STDOUT so logs show up twice.

While that example was complicated the logic I want is very simple: if you have a logger that is logging to STDOUT, do nothing, otherwise log to STDOUT and current logger. You cannot do this today without exposing the destination of the logger. This patch exposes the logger destination and allows us to write code like this:


```ruby
def make_sure_logging_to_stdout(logger)
  unless logger.destination == STDOUT   # <==== Cannot do this today
    stdout_logger = ::Logger.new(STDOUT)
    logger.extend(Module do
      def add((*args, &block)
        stdout_logger.add(*args, &block)
        super(*args, &block)
      end
    end)
  end
end

logger = Logger.new(STDOUT)
make_sure_logging_to_stdout(logger)

logger.fatal("An error has occured")
```

We should be able to inspect the destination of a logger, this patch enables this functionality.


---Files--------------------------------
ruby-changes.patch (1.04 KB)
ruby-changes.patch (2.17 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>