-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Better Logger #6901
[WIP] Better Logger #6901
Changes from 1 commit
48eaeb8
80e94c3
fcaf32f
9618593
ecc2c0e
37f14e6
14d176d
2305179
f81ab7c
c6c4a17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,32 +7,29 @@ class Logger | |
alias FilterType = Filter | Severity | (Entry -> Bool) | ||
alias EmitterType = Emitter | (Entry -> Nil) | ||
|
||
module Base | ||
abstract def component : String | ||
abstract def filter : FilterType? | ||
abstract def emitters : Array(EmitterType) | ||
|
||
def log(entry : Entry) | ||
case ff = filter | ||
when Filter, Proc(Entry, Bool) | ||
return unless ff.call(entry) | ||
when Severity | ||
return unless entry.severity >= ff | ||
end | ||
emitters.each &.call(entry) | ||
property component : String | ||
property filter : FilterType? | ||
property emitters : Array(EmitterType) | ||
|
||
def log(entry : Entry) | ||
case ff = filter | ||
when Filter, Proc(Entry, Bool) | ||
return unless ff.call(entry) | ||
when Severity | ||
return unless entry.severity >= ff | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? The expressions are semantically equal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "if X is inferior than Y" is simpler to understand than "unless X is superior or equal than Y", my opinion. |
||
end | ||
|
||
{% for level in Severity.constants %} | ||
def {{ level.downcase.id }}(message, *, time = Time.now, line_number = __LINE__, filename = __FILE__) | ||
log Entry.new(message, Severity::{{ level }}, component, time, line_number, filename) | ||
end | ||
{% end %} | ||
emitters.each &.call(entry) | ||
end | ||
|
||
include Base | ||
getter component : String | ||
getter filter : FilterType? | ||
getter emitters : Array(EmitterType) | ||
{% for level in Severity.constants %} | ||
def {{ level.downcase.id }}(message, *, time = Time.now, line_number = __LINE__, filename = __FILE__) | ||
log Entry.new(message, Severity::{{ level }}, component, time, line_number, filename) | ||
end | ||
{% end %} | ||
|
||
def get(component) : Logger | ||
Logger.new(component.to_s, nil, Forwarder.new(self)) | ||
end | ||
|
||
def initialize(@component, @filter, @emitters) | ||
end | ||
|
@@ -41,13 +38,10 @@ class Logger | |
new(component, filter, [emitter] of EmitterType) | ||
end | ||
|
||
extend Base | ||
class_property component = "" | ||
class_property filter : FilterType? | ||
class_property emitters : Array(EmitterType) = [IOEmitter.new] of EmitterType | ||
ROOT = new("", nil, IOEmitter.new) | ||
|
||
def self.get(component) | ||
Logger.new(component.to_s, nil, Forwarder.new(self)) | ||
def self.get(component) : Logger | ||
ROOT.get component | ||
end | ||
|
||
{% for level in Severity.constants %} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did the ivar accessors became properties? I don't think they need to be mutable.
getter
should be enough.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made
filter
a property sinceLogger::ROOT.filter = ...
needs to be possible, and then brought the rest along for consistency. But I agree immutability would be better in other cases. I think that's an argument in favor of makingLogger.get
return a class that is thinner thanLogger
.component
can be made immutable, in any case.emitters
doesn't really matter since the array will always be mutable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I see the last commit. Yeah, when we're not using thin wrappers,
filter
should probably be mutable. Butcomponent
andemitters
do not.