util.go 590 B

1234567891011121314151617181920212223242526272829303132333435
  1. package logx
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strings"
  6. "time"
  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 time.Now().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. }