-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1160 from joe-kimmel-vmw/so-many-loggers-we-ran-o…
…ut-of-flannel-shirts timestamp logs for entry/exit for all the top-level Lifecycle package…
- Loading branch information
Showing
14 changed files
with
131 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// Package log has logging interfaces for convenience in lifecycle | ||
package log | ||
|
||
import "github.com/apex/log" | ||
|
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,33 @@ | ||
package log | ||
|
||
import "time" | ||
|
||
// Chronit is, I guess, short for chronological unit because it measures time or something | ||
type Chronit struct { | ||
StartTime time.Time | ||
EndTime time.Time | ||
Log Logger | ||
FunctionName string | ||
} | ||
|
||
// NewMeasurement initializes a chronological measuring tool, logs out the start time, and returns a function you can defer that will log the end time | ||
func NewMeasurement(funcName string, lager Logger) func() { | ||
c := Chronit{Log: lager, FunctionName: funcName} | ||
c.RecordStart() | ||
return func() { | ||
c.RecordEnd() | ||
} | ||
} | ||
|
||
// RecordStart grabs the current time and logs it, but it will be called for you if you use the NewMeasurement convenience function. | ||
func (c *Chronit) RecordStart() { | ||
c.StartTime = time.Now() | ||
c.Log.Infof("Timer: %s started at %s", c.FunctionName, c.StartTime.Format(time.RFC3339)) | ||
} | ||
|
||
// RecordEnd is called in the function returned by NewMeasurement. | ||
// the EndTime will be populated just in case you'll keep the object in scope for later. | ||
func (c *Chronit) RecordEnd() { | ||
c.EndTime = time.Now() | ||
c.Log.Infof("Timer: %s ran for %v and ended at %s", c.FunctionName, c.EndTime.Sub(c.StartTime), c.EndTime.Format(time.RFC3339)) | ||
} |
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,83 @@ | ||
package log_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
|
||
"github.com/buildpacks/lifecycle/log" | ||
h "github.com/buildpacks/lifecycle/testhelpers" | ||
) | ||
|
||
type mockLog struct { | ||
callCount map[string]int | ||
} | ||
|
||
func (m mockLog) incr(key string) { | ||
val, ok := m.callCount[key] | ||
if !ok { | ||
m.callCount[key] = 1 | ||
} else { | ||
m.callCount[key] = val + 1 | ||
} | ||
} | ||
|
||
func (m mockLog) Debug(msg string) { | ||
m.incr("Debug") | ||
} | ||
func (m mockLog) Debugf(fmt string, v ...interface{}) { | ||
m.incr("Debug") | ||
} | ||
func (m mockLog) Info(msg string) { | ||
m.incr("Info") | ||
} | ||
func (m mockLog) Infof(fmt string, v ...interface{}) { | ||
m.incr("Info") | ||
} | ||
func (m mockLog) Warn(msg string) { | ||
m.incr("Warn") | ||
} | ||
func (m mockLog) Warnf(fmt string, v ...interface{}) { | ||
m.incr("Warn") | ||
} | ||
func (m mockLog) Error(msg string) { | ||
m.incr("Error") | ||
} | ||
func (m mockLog) Errorf(fmt string, v ...interface{}) { | ||
m.incr("Error") | ||
} | ||
|
||
func TestTimeLog(t *testing.T) { | ||
spec.Run(t, "Exporter", testTimeLog, spec.Parallel(), spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testTimeLog(t *testing.T, when spec.G, it spec.S) { | ||
when("we use the time log", func() { | ||
it("the granular api works step by step", func() { | ||
logger := mockLog{callCount: map[string]int{}} | ||
c1 := log.Chronit{} | ||
nullTime := time.Time{} | ||
h.AssertEq(t, c1.StartTime, nullTime) | ||
h.AssertEq(t, c1.EndTime, nullTime) | ||
|
||
c1.Log = logger | ||
c1.RecordStart() | ||
h.AssertEq(t, logger.callCount["Info"], 1) | ||
h.AssertEq(t, c1.StartTime == nullTime, false) | ||
h.AssertEq(t, c1.EndTime, nullTime) | ||
|
||
c1.RecordEnd() | ||
h.AssertEq(t, logger.callCount["Info"], 2) | ||
h.AssertEq(t, c1.EndTime == nullTime, false) | ||
}) | ||
it("the convenience functions call the logger", func() { | ||
logger := mockLog{callCount: map[string]int{}} | ||
endfunc := log.NewMeasurement("value", logger) | ||
h.AssertEq(t, logger.callCount["Info"], 1) | ||
endfunc() | ||
h.AssertEq(t, logger.callCount["Info"], 2) | ||
}) | ||
}) | ||
} |
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