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
One more way. If you're using tagged logging and need tags in another logfile as well, you could do it in this way
After this you'll get uuid tags in alternative logger
Hope that helps someone.
While I quite like the other suggestions, I found I had this same issue but wanted the ability to have different logging levels for STDERR and the file (like I could with the bigger logging frameworks like NLog). I ended up with a routing strategy that multiplexes at the logger level rather than at the IO level, so that each logger could then operate at independent log-levels:
Quick and dirty (ref: https://coderwall.com/p/y_b3ra/log-to-stdout-and-a-file-at-the-same-time)
I went to the same idea of "Delegating all methods to sub-elements" that other people already explored, but am returning for each of them the return value of the last call of the method. If I didn't, it broke
logger-colors
which were expecting anInteger
and map was returning anArray
.This will redelegate every method to all targets, and return only the return value of the last call.
Also, if you want colors, STDOUT or STDERR must be put last, since it's the only two were colors are supposed to be output. But then, it will also output colors to your file.
You can write a pseudo
IO
class that will write to multipleIO
objects. Something like:Then set that as your log file:
Every time
Logger
callsputs
on yourMultiIO
object, it will write to bothSTDOUT
and your log file.Edit: I went ahead and figured out the rest of the interface. A log device must respond to
write
andclose
(notputs
). As long asMultiIO
responds to those and proxies them to the real IO objects, this should work.I like the MultiIO approach. It works well with Ruby Logger. If you use pure IO it stops working because it lacks some methods that IO objects are expected to have. Pipes were mentioned before here: How can I have ruby logger log output to stdout as well as file?. Here is what works best for me.
Note I know this doesn't answer the question directly but it is strongly related. Whenever I searched for output to multiple IOs I came across this thread.So, I hope you find this useful too.