Forum: Ruby on Rails Redirecting scripted ActiveRecord o/p to a log

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike B. (Guest)
on 2007-04-14 02:58
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.
Mike B. (Guest)
on 2007-04-16 09:42
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.
This topic is locked and can not be replied to.