1

I have to log activity of application that runs in many threads. What would be best approach to do that? Probably it is not possible that all threads log into one file, because they will lock each other? Is it best to use separate file for each thread then?

I use this within a method that is called by threads at the moment:

     f=File.open(...)
     f.puts "log message"
     f.close

All threads are calling this method. I will append thread number to file name so that every thread creates its own file. This method is called when thread calls it and gives it new information to log. Then method opens file, writes information and closes it again. However is this opening and closing of file time-consuming in time-critical application? Or is this not something to be concerned of?

Tians
  • 443
  • 1
  • 5
  • 14
  • The Ruby standard Logger seems to be [thread-safe](http://stackoverflow.com/a/4660331/2483313). Is there a reason to look for a different solution? – spickermann Jan 07 '16 at 14:17
  • Well I could use it, but do not use it for now, because I don't need all the features that Logger has, so my approach seemed like more lightweight solution. In my case I think that threads are waiting each other when writing in the same file. If Logger would write to same file, would threads not wait each other then? I'm pretty new to this multithreading stuff. – Tians Jan 07 '16 at 14:55

0 Answers0