util.go 628 B

123456789101112131415161718192021222324252627282930313233343536
  1. package logx
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strings"
  6. "github.com/zeromicro/go-zero/core/timex"
  7. )
  8. func getCaller(callDepth int) string {
  9. _, file, line, ok := runtime.Caller(callDepth)
  10. if !ok {
  11. return ""
  12. }
  13. return prettyCaller(file, line)
  14. }
  15. func getTimestamp() string {
  16. return timex.Time().Format(timeFormat)
  17. }
  18. func prettyCaller(file string, line int) string {
  19. idx := strings.LastIndexByte(file, '/')
  20. if idx < 0 {
  21. return fmt.Sprintf("%s:%d", file, line)
  22. }
  23. idx = strings.LastIndexByte(file[:idx], '/')
  24. if idx < 0 {
  25. return fmt.Sprintf("%s:%d", file, line)
  26. }
  27. return fmt.Sprintf("%s:%d", file[idx+1:], line)
  28. }