Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:05:30+00:00 2026-05-23T07:05:30+00:00

From my script/ directory in a rails app, I am trying to capture the

  • 0

From my script/ directory in a rails app, I am trying to capture the output produced from a lib file and instead of displaying it on the screen, i want it to be saved to a file. I have a script that should display output to screen when it calls “puts” but before calling a method that produces output, it should set something so that the function called should log to a file when it uses puts. I have been experimenting withe the following two files in my rails application but they do not seem to work. (The stderr output is captured to the file correctly, but the otuput produced from a simple call to “puts” is not saved to the file “a.log” in the following code:)

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require File.join(RAILS_ROOT, 'lib/mylogger.rb')

puts "logtest" # should output logtest on the screen

STDERR.puts "logtest" # should also output on the screen

x = STDOUT
y = STDERR
STDOUT = File.new("a.log", "w")
STDERR = File.new("b.log", "w")
m = MyLogger.new
m.run
STDOUT = x
STDERR = y
puts "logtest" # should log to the screen
STDERR.puts "logtest" # should log to the screen

and the mylogger.rb file is as follows:

class MyLogger

  def run
    1.upto 1_000_000 do |x|
      puts "mylogger #{x}" # should log to the file a.log, this does not work
      STDERR.puts "mylogger #{x}" # should log to the file b.log, this works
    end
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:05:31+00:00Added an answer on May 23, 2026 at 7:05 am

    I changed the line

    STDOUT = File.new("a.log", "w")
    

    to

    $stdout = File.new("a.log", "w")
    

    and i changed

    STDOUT = x
    

    to

    $stdout = STDOUT
    

    and it worked

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want run all script from the directory . Like , The directory contains
i write a script to list my file from Update directory with this option:
I have this PHP script which i'm grabbing images from a directory and displaying
Every time I try to run any class from my rails 2.2 app's lib
I have a script that I'm running from the home directory to search for
I'm using a script to move files from one directory to another. It's also
What is current directory of shell script? Is this current directory from which I
How do I find out the current runtime directory from within a Python script?
I'm trying to initiate a Rails 3 console from Netbeans 6.9.1 (just upgraded) and
I'm trying to create a autostart script to start a Rails Instance Server when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.