log.go 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package utils
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. const (
  7. color_red = uint8(iota + 91)
  8. color_green
  9. color_yellow
  10. color_blue
  11. color_magenta //洋红
  12. // info = "[INFO]"
  13. // trac = "[TRAC]"
  14. // erro = "[ERRO]"
  15. // warn = "[WARN]"
  16. // succ = "[SUCC]"
  17. )
  18. // 根据 https://github.com/liyu4/chill 修改的
  19. // see complete color rules in document in https://en.wikipedia.org/wiki/ANSI_escape_code#cite_note-ecma48-13
  20. func TraceLog(format string, a ...interface{}) {
  21. fmt.Println(formatLog(yellow(fmt.Sprintf(format, a...))))
  22. }
  23. func InfoLog(format string, a ...interface{}) {
  24. fmt.Println(formatLog(blue(fmt.Sprintf(format, a...))))
  25. }
  26. func SuccessLog(format string, a ...interface{}) {
  27. fmt.Println(formatLog(green(fmt.Sprintf(format, a...))))
  28. }
  29. func WarningLog(format string, a ...interface{}) {
  30. fmt.Println(formatLog(magenta(fmt.Sprintf(format, a...))))
  31. }
  32. func ErrorLog(format string, a ...interface{}) {
  33. fmt.Println(formatLog(red(fmt.Sprintf(format, a...))))
  34. }
  35. func red(s string) string {
  36. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_red, s)
  37. }
  38. func green(s string) string {
  39. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_green, s)
  40. }
  41. func yellow(s string) string {
  42. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_yellow, s)
  43. }
  44. func blue(s string) string {
  45. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_blue, s)
  46. }
  47. func magenta(s string) string {
  48. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_magenta, s)
  49. }
  50. func formatLog(prefix string) string {
  51. return time.Now().Format("[2006/01/02 15:04:05]") + " " + prefix + " "
  52. }