log.go 698 B

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