Sha256: 6d2fc088d67465855bf68324751d4cfc5b2997523ac58f2cd9d043034231c8ed

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

# :stopdoc:
#
# Because of the global interpreter lock, Kernel#fork is the best way
# to acheive true concurrency in Ruby scripts. However, there are pecularities
# when using frok and passing file descriptors between process. These
# pecularities affect the logging framework.
#
# In short, always reopen file descriptors in the child process after fork has
# been called. The RollingFile appender uses flock to safely coordinate the
# rolling of the log file when multiple processes are writing to the same
# file. If the file descriptor is opened in the parent and multiple children
# are forked, then each child will use the same file descriptor lock; when one
# child locks the file any other child will also have the lock. This creates a
# race condition in the rolling code. The solution is to reopen the file to
# obtain a new file descriptor in each of the children.
#

  require 'logging'

  log = Logging.logger['example']
  log.add_appenders(
      Logging.appenders.rolling_file('roller.log', :age => 'daily')
  )
  log.level = :debug

  # Create four child processes and reopen the "roller.log" file descriptor in
  # each child. Now log rolling will work safely.
  4.times do
    fork {
      Logging.reopen
      log.info "This is child process #{Process.pid}"
    }
  end

  log.info "This is the parent process #{Process.pid}"

# :startdoc:

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
logging-1.6.1 examples/fork.rb
logging-1.6.0 examples/fork.rb
logging-1.5.2 examples/fork.rb
logging-1.5.1 examples/fork.rb
logging-1.5.0 examples/fork.rb
logging-1.4.3 examples/fork.rb
sgeorgi-logging-1.4.2 examples/fork.rb
logging-1.4.2 examples/fork.rb
logging-1.4.1 examples/fork.rb
logging-1.4.0 examples/fork.rb
logging-1.3.0 examples/fork.rb