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
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/logging-2.2.2/examples/lazy.rb
logging-2.2.2 examples/lazy.rb