Someting like a tee functionality in logger.
相关问题
- I want to trace logs using a Macro multi parameter
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- Error message 'No handlers could be found for
相关文章
- how do I log requests and responses for debugging
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
Here's another implementation, inspired by @jonas054's answer.
This uses a pattern similar to
Delegator
. This way you don't have to list all the methods you want to delegate, since it will delegate all methods that are defined in any of the target objects:You should be able to use this with Logger as well.
delegate_to_all.rb is available from here: https://gist.github.com/TylerRick/4990898
For those who like it simple:
source
Or print the message in the Logger formatter:
I'm actually using this technique to print to a log file, a cloud logger service (logentries) and if it's dev environment - also print to STDOUT.
Are you restricted to the standard logger?
If not you may use log4r:
One advantage: You could also define different log-levels for stdout and file.
I think your STDOUT is used for critical runtime info and errors raised.
So I use
to log debug and regular logging, and then wrote a few
where I need to see STDOUT information that my scripts were running at all!
Bah, just my 10 cents :-)
@jonas054's answer above is great, but it pollutes the
MultiDelegator
class with every new delegate. If you useMultiDelegator
several times, it will keep adding methods to the class, which is undesirable. (See bellow for example)Here is the same implementation, but using anonymous classes so the methods don't pollute the delegator class.
Here is an example of the method pollution with the original implementation, contrasted with the modified implementation:
All is good above.
tee
has awrite
method, but nosize
method as expected. Now, consider when we create another delegate:Oh no,
tee2
responds tosize
as expected, but it also responds towrite
because of the first delegate. Eventee
now responds tosize
because of the method pollution.Contrast this to the anonymous class solution, everything is as expected: