-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a better logger that shows timing diffs (#629)
Been meaning to do this - hopefully this better-formatted logging is easier to see where the performance bottlenecks are.
- Loading branch information
1 parent
12c000f
commit d3da552
Showing
16 changed files
with
136 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'logger' | ||
require 'forwardable' | ||
|
||
# This is a wrapper around the regular Ruby logger | ||
# It allows us to keep track of how long a task took since its last invocation | ||
class TimingLogger | ||
extend Forwardable | ||
|
||
attr_accessor :logger | ||
|
||
def_delegators :logger, # <-- inner object, v-- delegated methods --v | ||
:<<, :add, :close, :datetime_format, :datetime_format=, :debug, :debug?, | ||
:error, :error?, :fatal, :fatal?, :info?, :level=, :log, :reopen, | ||
:sev_threshold=, :unknown, :warn, :warn? | ||
|
||
def initialize(out = STDOUT, progname = '') | ||
@logger = Logger.new(out, progname: progname) | ||
@last_info = Time.current | ||
@logger.formatter = proc do |severity, _datetime, progname, msg| | ||
severity = "\e[31mWARN\e[0m" if severity == 'WARN' | ||
t = Time.current.strftime("%Y-%m-%d %T") | ||
preamble = '%-50s' % "[#{severity} #{t} #{progname}]" | ||
"#{preamble} #{msg}\n" | ||
end | ||
|
||
end | ||
|
||
def info(msg = '') | ||
now = Time.current | ||
time_took = now - @last_info | ||
time_took_str = "%2d" % time_took.seconds | ||
@logger.info " #{time_took_str}s | #{msg}" | ||
@last_info = now | ||
end | ||
|
||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require 'test_helper' | ||
require 'timing_logger' | ||
|
||
class TimingLoggerTest < ActiveSupport::TestCase | ||
|
||
test 'log once' do | ||
out = StringIO.new | ||
travel_to Time.zone.parse('1984-05-19 07:35:03') | ||
log = TimingLogger.new(out, 'prog') | ||
travel_to Time.zone.parse('1984-05-19 07:35:13') # 10s later | ||
log.info "log once" | ||
|
||
expected = <<~EOS | ||
[INFO 1984-05-19 07:35:13 prog] 10s | log once | ||
EOS | ||
|
||
assert_equal expected, out.string | ||
end | ||
|
||
test 'log with warnings in between' do | ||
out = StringIO.new | ||
|
||
travel_to Time.zone.parse('1984-05-19 07:35:03') | ||
log = TimingLogger.new(out, 'prog') | ||
|
||
travel_to Time.zone.parse('1984-05-19 07:35:13') # 10s later | ||
log.info "info first" | ||
|
||
travel_to Time.zone.parse('1984-05-19 07:35:15') # 10s later | ||
log.warn "WARNING" | ||
|
||
travel_to Time.zone.parse('1984-05-19 07:37:13') # 120s since last info | ||
log.info "info second blah blah" | ||
|
||
expected = <<~EOS | ||
[INFO 1984-05-19 07:35:13 prog] 10s | info first | ||
[\e[31mWARN\e[0m 1984-05-19 07:35:15 prog] WARNING | ||
[INFO 1984-05-19 07:37:13 prog] 120s | info second blah blah | ||
EOS | ||
|
||
|
||
assert_equal expected, out.string | ||
end | ||
|
||
end |