Last active
October 24, 2020 19:05
-
-
Save kwstannard/613cebba33eaf0bf767d473d9e5eaa57 to your computer and use it in GitHub Desktop.
a logger with no if statements
This file contains hidden or 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
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