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 7613291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:07:14+00:00 2026-05-31T02:07:14+00:00

I have a Logger instance: require ‘logger’ logger = Logger.new( ‘foo.log’, ‘weekly’ ) I

  • 0

I have a Logger instance:

require 'logger'
logger = Logger.new( 'foo.log', 'weekly' )

I want to redirect runtime errors (stderr output) into the log as well. I found this forum thread which has the advice:

new_fd = logger.get_logger_file_descriptor
$stderr.reopen new_fd

However, Logger does not have an instance method get_logger_file_descriptor, nor can I find any exposed methods around gaining access to the log device or file.

How can I cause all $stderr output to go into the log?

  • 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-31T02:07:16+00:00Added an answer on May 31, 2026 at 2:07 am

    If you’re creating the logger yourself, you can create the File object first, then use it to create the logger and assign it to $stderr:

    log_file = File.new('foo.log', 'a')
    logger = Logger.new(log_file, 'weekly')
    $stderr = log_file   #usually no need to use reopen
    

    Note that this will result in the log output being mixed up with the output of $stderr, which may cause problems if you’re parsing the log file expecting it to be in a certain format (this will happen with your solution too).

    If you don’t have the underlying file but just receive the logger from somewhere else, it’s a bit more tricky. What is needed is an IO like object that can be assigned to $stderr and passes anything written to it to the logger. The IO class in Ruby is unfortunately fairly closely tied to the underlying i/o system (file descriptors and the like), and there’s no general interface that can be used to create input and output streams. (StringIO being the notable exception).

    However most, if not all, of the output methods on IO ultimately go through #write, so by overriding this one method you can get close to what you’re after:

    class IOToLog < IO
    
      def initialize(logger)
        @logger = logger
      end
    
      def write(string)
        #assume anything written to stderr is an error
        @logger.error(message)
      end
    
    end
    
    logger = get_logger_from_somewhere
    
    $stderr = IOToLog.new(logger)
    

    Now anything written to $stderr will end up going to the log file. The formatting however will be a bit odd. Anytime any of the writing methods calls #write a new entry will be made in the logfile. For example, #puts called with an array will call #write for each entry of the array, and again with a newline character between each entry, resulting in 2n – 1 log entries, n – 1 of which will be blank.

    You could make the overridden #write method more complex to handle this, perhaps using an internal buffer, and only call the logger when you think you have a full message. Alternatively you could override the individual methods to write to the logger themselves. If you did this the IOToLog class wouldn’t necessarily have to inherit from IO.

    Your best solution will depend on how you want to standard error output to appear in the logfile, how your program uses $stderr, and how much work you want to do implementing methods from IO.

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

Sidebar

Related Questions

I have a python logger set up, using python's logging module. I want to
I have some files: core.php : require_once 'logger.php'; require_once 'smth_else.php'; $Logger = new Logger();
We have a sharepoint instance on our network that we get automatically logged into
I have a logger system which basically is a fancy way of writing my
I have a logger in a c++ application that uses defines as follows: #define
We have lots of logging calls in our app. Our logger takes a System.Type
I have a property on my classes for logging service. private ILogger logger =
I was looking at the Ruby logging library Logging.logger method and have a question
I have code like: @notifications = Notification.find_all_by_user_id(@user.id, :order=>'deliver_by DESC', :conditions=>deliver_by >= '#{Date.today.to_s(:db)}') logger.info @upcoming_reminders[0].inspect
Due to the flooding examples of implementing logger using Singleton pattern, I have just

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.