Skip to content

Instantly share code, notes, and snippets.

@KelseyDH
Created January 27, 2018 08:26
Show Gist options
  • Save KelseyDH/c7aabc918644e0536a7ee865c39132b9 to your computer and use it in GitHub Desktop.
Save KelseyDH/c7aabc918644e0536a7ee865c39132b9 to your computer and use it in GitHub Desktop.
How to write a Rails Service Object with logging
# # A Plain Old Ruby Object (poro) with Rails logger support:
class ServiceObject
def initialize(foo)
@logger = Logger.new(STDOUT)
@foo = foo
end
def call
do_something
end
private
def do_something
@logger.debug { "do_something called on #{@foo.class}" }
end
end
class Bar
end
# # Usage example:
MyService.new(Bar.new).call
#=> [2018-01-27] DEBUG -- : do_something called! With Bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment