logger.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package log will be removed after switching to use core framework log.
  14. // Do not make further changes here!
  15. package log
  16. import (
  17. "fmt"
  18. "time"
  19. "github.com/onsi/ginkgo"
  20. "k8s.io/kubernetes/test/e2e/framework/ginkgowrapper"
  21. )
  22. func nowStamp() string {
  23. return time.Now().Format(time.StampMilli)
  24. }
  25. func log(level string, format string, args ...interface{}) {
  26. fmt.Fprintf(ginkgo.GinkgoWriter, nowStamp()+": "+level+": "+format+"\n", args...)
  27. }
  28. // Logf logs the info.
  29. func Logf(format string, args ...interface{}) {
  30. log("INFO", format, args...)
  31. }
  32. // Failf logs the fail info.
  33. func Failf(format string, args ...interface{}) {
  34. FailfWithOffset(1, format, args...)
  35. }
  36. // FailfWithOffset calls "Fail" and logs the error at "offset" levels above its caller
  37. // (for example, for call chain f -> g -> FailfWithOffset(1, ...) error would be logged for "f").
  38. func FailfWithOffset(offset int, format string, args ...interface{}) {
  39. msg := fmt.Sprintf(format, args...)
  40. log("FAIL", msg)
  41. ginkgowrapper.Fail(nowStamp()+": "+msg, 1+offset)
  42. }