Created
April 20, 2016 15:53
-
-
Save just3ws/fe978a67042eee5e369f2971f2b4e809 to your computer and use it in GitHub Desktop.
Customizing logic for use by logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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