Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have ruby logger log output to stdout as well as file?

Tags:

logging

ruby

Someting like a tee functionality in logger.

like image 421
Manish Sapariya Avatar asked Jun 20 '11 05:06

Manish Sapariya


People also ask

What is stdout logging?

The stdout. log file is a console logging file that has poor log management which causes the file to grow too large. From PingFederate 9.0 onwards: 1) If an OS service start up script is being used or <pfinstall>/sbin/pingfederate-run.sh, output can be sent to stdout.

How do I create a log file in Ruby?

Since the logger library comes with Ruby, there's no need to install any gems or other libraries. To begin using the logger library, simply require 'logger' and create a new Logger object. Any messages written to the Logger object will be written to the log file.

Where are Ruby logs stored?

In a Rails app, logs are stored under the /log folder.


1 Answers

You can write a pseudo IO class that will write to multiple IO objects. Something like:

class MultiIO   def initialize(*targets)      @targets = targets   end    def write(*args)     @targets.each {|t| t.write(*args)}   end    def close     @targets.each(&:close)   end end 

Then set that as your log file:

log_file = File.open("log/debug.log", "a") Logger.new MultiIO.new(STDOUT, log_file) 

Every time Logger calls puts on your MultiIO object, it will write to both STDOUT and your log file.

Edit: I went ahead and figured out the rest of the interface. A log device must respond to write and close (not puts). As long as MultiIO responds to those and proxies them to the real IO objects, this should work.

like image 102
David Avatar answered Sep 23 '22 21:09

David