log.go 798 B

12345678910111213141516171819202122232425262728293031323334
  1. package sqlw
  2. import (
  3. "fmt"
  4. "os"
  5. "regexp"
  6. "strings"
  7. "time"
  8. "golang-fave/engine/consts"
  9. )
  10. func log(query string, s time.Time, e error, transaction bool) {
  11. msg := query
  12. if reg, err := regexp.Compile("[\\s\\t]+"); err == nil {
  13. msg = strings.Trim(reg.ReplaceAllString(msg, " "), " ")
  14. }
  15. if reg, err := regexp.Compile("[\\s\\t]+;$"); err == nil {
  16. msg = reg.ReplaceAllString(msg, ";")
  17. }
  18. eStr := " (nil)"
  19. if e != nil {
  20. eStr = " \033[0m\033[0;31m(" + e.Error() + ")"
  21. }
  22. if consts.IS_WIN {
  23. fmt.Fprintln(os.Stdout, "[SQL] "+msg+eStr+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds()))
  24. } else {
  25. color := "0;33"
  26. if transaction {
  27. color = "1;33"
  28. }
  29. fmt.Fprintln(os.Stdout, "\033["+color+"m[SQL] "+msg+eStr+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds())+"\033[0m")
  30. }
  31. }