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

  • Home
  • SEARCH
  • 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 3490052
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:28:10+00:00 2026-05-18T11:28:10+00:00

How do I proxy the ruby logger and keep performance? So, we have an

  • 0

How do I proxy the ruby logger and keep performance?

So, we have an requirement at work, quite reasonable. When a program is sent the signal HUP
the log is flushed and restarted.

class LocalObject

  attr_accessor :logger

  def initialize context
    # one less method call! Yea! performance++
    @logger = context.logger
  end

  def something
    @logger.info "Hello world"
  end

end

The problem, is that if context.logger is reset, then @logger still points to the old one.

So, I thought I would proxy logger:

class LoggerProxy
  attr_accessor :logger

  def debug *args
    @logger.send :debug, args
  end

  def info *args
    @logger.send :info, args
  end
end

context.logger = LoggerProxy.new
context.logger.logger = Logger.new 'my_file.log'

Signal.trap('HUP') { 
  context.logger.logger = Logger.new 'my_file.log'
}
...
@logger = context.logger
@logger.info "Hello world"

This works fine, except I’ve traded one method call for 2 method calls (1 accessor; which returns the logger). I still have to call LoggerProxy.:debug, :info, …, which in turn calls the original logger! Ergo, 2 methods calls, where there was one.

I don’t want to monkey with Logger class, or overload it, because I want to use other loggers in the future, syslog, roll my own, or some such.

Is there a way reduce the number of method calls for performance?

-daniel

Update: in response to a question about performance, here is the sample test.

require 'logger'
require 'benchmark';

class MyLogger

  attr_accessor :logger

  def info msg
    @logger.info msg
  end

end

myLogger = Logger.new '/dev/null' # dev null to avoid IO issues
myLoggerProxy = MyLogger.new
myLoggerProxy.logger = myLogger

n = 100000
Benchmark.bm do | benchmarker |
  # plain logger
  benchmarker.report { n.times { myLogger.info 'opps' } }

  # via accessor
  benchmarker.report { n.times { myLoggerProxy.logger.info 'opps' } }

  # via proxy
  benchmarker.report { n.times { myLoggerProxy.info 'opps' } }
end


      user     system      total        real
  1.580000   0.150000   1.730000 (  1.734956)
  1.600000   0.150000   1.750000 (  1.747969)
  1.610000   0.160000   1.770000 (  1.767886)
  • 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-18T11:28:10+00:00Added an answer on May 18, 2026 at 11:28 am

    First: your question smells like you’re optimizing prematurely. You should only optimize if you know your code is too slow. (And your benchmark show only a tiny difference)

    That said, you could make the Context notify every proxy if logger is ever updated:

    class ProxyLogger
      attr_accessor :logger
    
      def initialize(context)
        context.register(self)
      end
    end
    
    class Context
      attr_accessor :logger
    
      def initialize
        @proxies = []
      end
    
      def logger=(val)
        @logger = val
        @proxies.each { |p| p.logger = val }
      end
    
      def register(proxy)
        @proxies << proxy
      end
    end
    

    but again, this does not really seem worth the additional complexity.

    (related: this is a very nice presentation showing @tenderlove optimizing the ARel gem)

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

Sidebar

Related Questions

At home we have a proxy server. At work we don't. Firefox irritates in
I have this vendor-supplied TLB file, which I've used to generate a Ruby proxy
I want to proxy all requests to Mongreel except for a few ruby apps
We have a reverse proxy here, running Apache in version 2.2.x Essentially I want
I'm really asking this by proxy, another team at work has had a change
We have a SQUID reverse proxy and a MOSS 2007 portal. All sites are
What is a good approach to a client proxy written in ruby that I
I would like to use the net/imap library in ruby behind a authenticated proxy,
I have the Ruby on Rails-based application Redmine (based on the BitNami redmine package)
Can a (||any) proxy server cache content that is requested by a client over

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.