Sha256: b1ac9ded4dda5adf023000ef433ed75d8a7b0d59a78d8f9a66561b677ab70eb8

Contents?: true

Size: 1.66 KB

Versions: 42

Compression:

Stored size: 1.66 KB

Contents

# :stopdoc:
#
# It happens sometimes that it is very expensive to construct a logging
# message; for example, if a large object structure has to be traversed
# during execution of an `object.to_s` method. It would be convenient to
# delay creation of the message until the log event actually takes place.
#
# For example, with a logger configured only to show WARN messages and higher,
# creating the log message for an INFO message would be wasteful. The INFO log
# event would never be generated in this case.
#
# Log message creation can be performed lazily by wrapping the expensive
# message generation code in a block and passing that to the logging method.

  require 'logging'

  Logging.logger.root.appenders = Logging.appenders.stdout
  Logging.logger.root.level = :info

  # We use this dummy method in order to see if the method gets called, but in practice,
  # this method might do complicated string operations to construct a log message.
  def expensive_method
    puts "Called!"
    "Expensive message"
  end

  log = Logging.logger['Lazy']

  # If you log this message the usual way, expensive_method gets called before
  # debug, hence the Logging framework has no chance to stop it from being executed
  # immediately.
  log.info("Normal")
  log.debug(expensive_method)

  # If we put the message into a block, then the block is not executed, if
  # the message is not needed with the current log level.
  log.info("Block unused")
  log.debug { expensive_method }

  # If the log message is needed with the current log level, then the block is of
  # course executed and the log message appears as expected.
  log.info("Block used")
  log.warn { expensive_method }

# :startdoc:

Version data entries

42 entries across 34 versions & 3 rubygems

Version Path
logging-2.4.0 examples/lazy.rb
logging-2.3.1 examples/lazy.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/logging-2.3.0/examples/lazy.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/logging-2.3.0/examples/lazy.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/logging-2.3.0/examples/lazy.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/logging-2.3.0/examples/lazy.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/logging-2.3.0/examples/lazy.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/logging-2.3.0/examples/lazy.rb
logging-2.3.0 examples/lazy.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb