Set logger.progname on a per thread basis

I have this :

class Stress
  def initialize(user, pass)
    @user = user
    @pass = pass
    @agent = Mechanize.new do |a|
      a.user_agent_alias = 'Windows Mozilla'
      a.history.max_size = 0
      a.log = my_log
      a.log.progname = @user
    end
  end
  def browse
  @agent.log.progname = @user
  # open/close page
  end
end

my_log = Logger.new('dump.log')
my_log.level = Logger::DEBUG
atom = Mutex.new

for i in (Attempts_start..Attempts_end)
  threads << Thread.new(Creden_base + i.to_s) do |user|
    stress = Stress.new(user, user)
    for j in (0..Attempts_req) do
        atom.synchronize {stress.browse} # has to be atomic
    end
  end
end

The above code correctly identifies the different threads by the user by
setting the progname, but the problem is I have to use the Mutex class
to synchronize it, thus loosing the parallel computing since I have to
wait for the request to be sent and received before continuing.

Is there a way to do this without using the Mutex class. Have the
progname set on a per thread basis while running the threads in
real-time parallel.

On Fri, Nov 25, 2011 at 3:26 AM, Kassym D. [email protected]
wrote:

a.log.progname = @user

atom = Mutex.new
The above code correctly identifies the different threads by the user by
setting the progname, but the problem is I have to use the Mutex class
to synchronize it, thus loosing the parallel computing since I have to
wait for the request to be sent and received before continuing.

Is there a way to do this without using the Mutex class. Have the
progname set on a per thread basis while running the threads in
real-time parallel.

That depends on the implementation of the Logger you are using (which
one btw.?). However, your lock has far too big scope. You only need
to lock access to the shared resource (the Logger instance) - not the
whole invocation of Stress#browse since you an instance of Stress per
thread.

Kind regards

robert

Sorry I should have specified.

In each thread I’m creating a new instance of Mechanize and login with a
different user. I send get requests from each user and I’m using the
built-in Mechanize logger. On DEBUG mode the log can generate up to 20
lines of log per request. Since there can be multiple requests going at
once I want a way to differentiate between them.

The easiest way I thought of doing this was to change the progname on a
per thread basis, but for that to be thread safe I need to synchronize
the whole method that uses Mechanize (Browse).

Thanks!

I don’t see any reference to thread local in the built in Ruby logger.I
will look at log4j.

Thanks

This seems to be what I want to achieve. How would I go about
implementing this :

http://rubyforge.org/tracker/index.php?func=detail&aid=7592&group_id=426&atid=1700

On Fri, Nov 25, 2011 at 7:23 PM, Kassym D. [email protected]
wrote:

This seems to be what I want to achieve. How would I go about
implementing this :

http://rubyforge.org/tracker/index.php?func=detail&aid=7592&group_id=426&atid=1700

$ ri19 Logger

Then look for “logger.formatter” which allows you do create a custom
format. Then just use Thread.current.object_id or anything else you
store in a thread when generating the message.

Kind regards

robert

On Fri, Nov 25, 2011 at 2:57 PM, Kassym D. [email protected]
wrote:

the whole method that uses Mechanize (Browse).
I’d check whether you can insert a thread local into the log. Java’s
log4j has such a feature and I guess the logger you are using might
have as well. Way better than to synchronize the whole call.

Kind regards

robert