Skip to content

Instantly share code, notes, and snippets.

@kwstannard
Last active October 24, 2020 19:05
Show Gist options
  • Save kwstannard/613cebba33eaf0bf767d473d9e5eaa57 to your computer and use it in GitHub Desktop.
Save kwstannard/613cebba33eaf0bf767d473d9e5eaa57 to your computer and use it in GitHub Desktop.
a logger with no if statements
require 'active_support/core_ext/string'
module Logger
class Null
def initialize
@blob = {}
end
def unknown(*); end
def fatal(*); end
def error(*); end
def warn(*); end
def info(*); end
def debug(*); end
end
%w(unknown fatal error warn info debug).reduce(Null) do |ancestor, name|
klass = Class.new(ancestor) do
define_method(:initialize) do
super()
@blob[name] = []
end
define_method(name) {|msg| @blob[name] << msg}
end
const_set(name.classify, klass)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment