diff --git a/src/logger.cr b/src/logger.cr index d641ff09cdb4..2aabb74895bf 100644 --- a/src/logger.cr +++ b/src/logger.cr @@ -1,6 +1,7 @@ +require "logger/component" +require "logger/emitter" require "logger/entry" require "logger/filter" -require "logger/emitter" require "logger/severity" class Logger @@ -31,6 +32,10 @@ class Logger log Entry.new(message, Severity::{{ level }}, component, time, line_number, filename) end {% end %} + + def get(component) + Component.new(component.to_s, self) + end end include Base @@ -46,10 +51,6 @@ class Logger class_property filter : FilterType? class_property emitter : EmitterType? = IOEmitter.new - def self.get(component) - Logger.new(component.to_s, nil, Forwarder.new(self)) - end - {% for level in Severity.constants %} {{ level }} = Severity::{{ level }} {% end %} diff --git a/src/logger/component.cr b/src/logger/component.cr new file mode 100644 index 000000000000..bab07a880ce4 --- /dev/null +++ b/src/logger/component.cr @@ -0,0 +1,21 @@ +require "./severity" + +class Logger + class Component + getter logger : Base + getter name : String + + def initialize(@name, @logger) + end + + {% for level in Severity.constants %} + def {{ level.downcase.id }}(*, time = Time.now, line_number = __LINE__, filename = __FILE__, &message : -> String) + @logger.log Entry.new(message, Severity::{{ level }}, name, time, line_number, filename) + end + + def {{ level.downcase.id }}(message, *, time = Time.now, line_number = __LINE__, filename = __FILE__) + @logger.log Entry.new(message, Severity::{{ level }}, name, time, line_number, filename) + end + {% end %} + end +end diff --git a/src/logger/emitter.cr b/src/logger/emitter.cr index f758cd4693df..f9ac7bc3c1b6 100644 --- a/src/logger/emitter.cr +++ b/src/logger/emitter.cr @@ -21,17 +21,4 @@ class Logger @formatter.call(@io, entry) end end - - class Forwarder - include Emitter - - getter dest : Base - - def initialize(@dest) - end - - def call(entry : Entry) - dest.log(entry) - end - end end