Skip to content

Instantly share code, notes, and snippets.

@just3ws
Created April 20, 2016 15:53
Show Gist options
  • Save just3ws/fe978a67042eee5e369f2971f2b4e809 to your computer and use it in GitHub Desktop.
Save just3ws/fe978a67042eee5e369f2971f2b4e809 to your computer and use it in GitHub Desktop.
Customizing logic for use by logger
# Simple logger
logger = Logger.new(STDOUT)
# Some object
Foo = Struct.new(:hello, :goodbye)
foo = Foo.new
foo.hello = 'Hola'
foo.goodbye = 'Adiós'
# Unmodified
logger.debug(foo)
logger.info(foo)
# Custom log view method
module LogView
def inspect
"HELLO=#{hello}, GOODBYE=#{goodbye}"
end
end
# Extend the instance
foo.extend(LogView)
logger.debug(foo)
logger.info(foo)
# Reset
foo = nil
# Decorate the instance
class FooLogView < SimpleDelegator
include LogView
end
foo = Foo.new
foo.hello = 'Bonjour'
foo.goodbye = 'Au Revoir'
flv = FooLogView.new(foo)
logger.debug(flv)
logger.info(flv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment