# Redirecting scripted ActiveRecord o/p to a log

**URL:** <https://www.ruby-forum.com/t/redirecting-scripted-activerecord-o-p-to-a-log/91989>\
**Category:** Rails\
**Created:** [April 14, 2007, 12:58am UTC](https://www.ruby-forum.com/t/redirecting-scripted-activerecord-o-p-to-a-log/91989 "2007-04-14T00:58:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [April 14, 2007, 12:58am UTC](https://www.ruby-forum.com/t/redirecting-scripted-activerecord-o-p-to-a-log/91989/1 "2007-04-14T00:58:24Z")

</div>

I have a script which uses ActiveRecord to create a table  
if it is not already there.

When I run it … I get the following to STDOUT

– create\_table(“cnc\_services”, {:force=\>true})  
-\> 0.0660s

Well and good, but I want that to go to a log file, so I do this

…  
require\_gem “activerecord”  
require ‘logger’  
$logger = Logger.new(’/var/log/db\_check\_schema.log’, 3, 500000)  
$logger.level = Logger::INFO  
$logger.datetime\_format = “%Y-%m-%d %H:%M:%S”  
ActiveRecord::Base.logger = $logger # \<— Try to get AR to log to our  
log

ActiveRecord::Base.establish\_connection …  
…  
class CncService \< ActiveRecord::Base  
end

unless CncService.table\_exists?  
ActiveRecord::Schema.define() do  
create\_table “cnc\_services”, :force =\> true do |t|  
# column definitions  
end  
end  
end

Well, I still get

– create\_table(“cnc\_services”, {:force=\>true})  
-\> 0.0660s

on STDOUT, and nothing in my log file.

An explicit call to the logger, like  
$logger.info “text”  
goes there of course.

BTW, I am using Postgres here (if it matters).

So, what is the trick?

Thanks much,  
– Mike B.

---

<div class="post-metadata">

**Author:** ![asplake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/2bfe46/32.png) [@asplake](https://www.ruby-forum.com/u/asplake)\
**Post date:** [April 16, 2007, 7:42am UTC](https://www.ruby-forum.com/t/redirecting-scripted-activerecord-o-p-to-a-log/91989/2 "2007-04-16T07:42:53Z")

</div>

Mike B. wrote:

> I have a script which uses ActiveRecord to create a table  
> if it is not already there.
> 
> When I run it … I get the following to STDOUT
> 
> – create\_table(“cnc\_services”, {:force=\>true})  
> -\> 0.0660s
> 
> Well and good, but I want that to go to a log file, so I do this
> 
> …  
> require\_gem “activerecord”  
> require ‘logger’  
> $logger = Logger.new(’/var/log/db\_check\_schema.log’, 3, 500000)  
> $logger.level = Logger::INFO  
> $logger.datetime\_format = “%Y-%m-%d %H:%M:%S”  
> ActiveRecord::Base.logger = $logger # \<— Try to get AR to log to our  
> log
> 
> ActiveRecord::Base.establish\_connection …  
> …  
> class CncService \< ActiveRecord::Base  
> end
> 
> unless CncService.table\_exists?  
> ActiveRecord::Schema.define() do  
> create\_table “cnc\_services”, :force =\> true do |t|  
> # column definitions  
> end  
> end  
> end
> 
> Well, I still get
> 
> – create\_table(“cnc\_services”, {:force=\>true})  
> -\> 0.0660s
> 
> on STDOUT, and nothing in my log file.
> 
> An explicit call to the logger, like  
> $logger.info “text”  
> goes there of course.
> 
> BTW, I am using Postgres here (if it matters).
> 
> So, what is the trick?
> 
> Thanks much,  
> – Mike B.

It seems that that “create\_table …” output, whereever it is coming  
from  
is being written to STDOUT not the logger.

Well, I still wanted it in my log, so I ended up doing this …

$logger = Logger.new(’/var/log/testrun.log’, 3, 500000)  
$logger.level = Logger::INFO # DEBUG if you want it all  
$logger.datetime\_format = “%Y-%m-%d %H:%M:%S”

# Give our logger a singleton write method

# that allows it to take the place of stdout.

def $logger.write(data); self.info(data.chomp); end  
$old\_stdout, $stdout = $stdout, $logger

# Make singleton method(s) on the logger

# to also copy o/p to the original stdout

class Logger  
alias orig\_info info  
end  
def $logger.info(logdata)  
$old\_stdout.puts logdata  
orig\_info logdata  
end

– Mike B.
