Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created April 23, 2014 20:12
Show Gist options
  • Save cheeyeo/11230625 to your computer and use it in GitHub Desktop.
Save cheeyeo/11230625 to your computer and use it in GitHub Desktop.
Ruby string template
require "socket"
class MyLogger
attr_accessor :format
def initialize
@format = '%<severity>s %<time>s %<host>s %<pid>s %<message>s'
end
def error(message)
data = {
message: message,
time: Time.now,
host: Socket.gethostname,
pid: $$,
severity: "ERROR"
}
printf @format, data
end
end
logger = MyLogger.new
logger.format = "%<severity>.1s %{message} %{time} %{host}"
logger.error "Klingons off the starboard bow!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment