log.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package utils
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. )
  7. const (
  8. color_red = uint8(iota + 91)
  9. color_green
  10. color_yellow
  11. color_blue
  12. color_magenta //洋红
  13. // info = "[INFO]"
  14. // trac = "[TRAC]"
  15. // erro = "[ERRO]"
  16. // warn = "[WARN]"
  17. // succ = "[SUCC]"
  18. )
  19. // 根据 https://github.com/liyu4/chill 修改的
  20. // see complete color rules in document in https://en.wikipedia.org/wiki/ANSI_escape_code#cite_note-ecma48-13
  21. func TraceLog(format string, a ...interface{}) {
  22. fmt.Println(formatLog(yellow(fmt.Sprintf(format, a...))))
  23. }
  24. func InfoLog(format string, a ...interface{}) {
  25. fmt.Println(formatLog(blue(fmt.Sprintf(format, a...))))
  26. }
  27. func SuccessLog(format string, a ...interface{}) {
  28. fmt.Println(formatLog(green(fmt.Sprintf(format, a...))))
  29. }
  30. func WarningLog(format string, a ...interface{}) {
  31. fmt.Println(formatLog(magenta(fmt.Sprintf(format, a...))))
  32. }
  33. func ErrorLog(format string, a ...interface{}) {
  34. fmt.Println(formatLog(red(fmt.Sprintf(format, a...))))
  35. }
  36. func red(s string) string {
  37. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_red, s)
  38. }
  39. func green(s string) string {
  40. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_green, s)
  41. }
  42. func yellow(s string) string {
  43. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_yellow, s)
  44. }
  45. func blue(s string) string {
  46. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_blue, s)
  47. }
  48. func magenta(s string) string {
  49. return fmt.Sprintf("\x1b[%dm%s\x1b[0m", color_magenta, s)
  50. }
  51. func formatLog(prefix string) string {
  52. return time.Now().Format("[2006/01/02 15:04:05]") + " " + prefix + " "
  53. }
  54. func Mkdir(dir string) {
  55. // 创建文件夹
  56. exist, err := PathExists(dir)
  57. if err != nil {
  58. fmt.Println(err.Error())
  59. } else {
  60. if exist {
  61. fmt.Println(dir + "文件夹已存在!")
  62. } else {
  63. // 文件夹名称,权限
  64. err := os.Mkdir(dir, os.ModePerm)
  65. if err != nil {
  66. fmt.Println(dir+"文件夹创建失败:", err.Error())
  67. } else {
  68. fmt.Println(dir + "文件夹创建成功!")
  69. }
  70. }
  71. }
  72. }
  73. func PathExists(path string) (bool, error) {
  74. _, err := os.Stat(path)
  75. if err == nil {
  76. return true, nil
  77. }
  78. if os.IsNotExist(err) {
  79. return false, nil
  80. }
  81. return false, err
  82. }