logger.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package myth
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. // Logger logger
  8. type Logger struct {
  9. logger *log.Logger
  10. }
  11. var (
  12. defLog *Logger
  13. defFP *os.File
  14. )
  15. // NewDefaultLogger defult logger
  16. func NewDefaultLogger(path string) (*Logger, error) {
  17. var err error
  18. fp := &os.File{}
  19. if fp, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666); err != nil {
  20. return nil, err
  21. }
  22. defLog = NewLogger(fp)
  23. defFP = fp
  24. return defLog, nil
  25. }
  26. // ReopenDefaultLogger re open default logger
  27. func ReopenDefaultLogger(path string) (*Logger, error) {
  28. var err error
  29. fp := &os.File{}
  30. if fp, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666); err != nil {
  31. return nil, err
  32. }
  33. if defLog == nil {
  34. defLog = NewLogger(fp)
  35. }
  36. fpo := defFP
  37. defFP = fp
  38. defLog.logger = log.New(fp, "[INFO]", log.LstdFlags)
  39. fpo.Close()
  40. return defLog, nil
  41. }
  42. // CloseDefaultLogger close
  43. func CloseDefaultLogger() error {
  44. return defFP.Close()
  45. }
  46. // LogDebug debug
  47. func LogDebug(v ...interface{}) {
  48. defLog.logger.SetPrefix("[DEBUG]")
  49. defLog.logger.Println(v...)
  50. }
  51. // LogInfo info
  52. func LogInfo(v ...interface{}) {
  53. defLog.logger.SetPrefix("[INFO]")
  54. defLog.logger.Println(v...)
  55. }
  56. // LogWarn warn
  57. func LogWarn(v ...interface{}) {
  58. defLog.logger.SetPrefix("[Warn]")
  59. defLog.logger.Println(v...)
  60. }
  61. // LogErr err
  62. func LogErr(v ...interface{}) {
  63. defLog.logger.SetPrefix("[Err]")
  64. defLog.logger.Println(v...)
  65. }
  66. // LogAlert alert
  67. func LogAlert(v ...interface{}) {
  68. defLog.logger.SetPrefix("[Alert]")
  69. defLog.logger.Println(v...)
  70. }
  71. // NewLogger new logger
  72. func NewLogger(out io.Writer) *Logger {
  73. logger := log.New(out, "[INFO]", log.Ldate|log.Ltime)
  74. return &Logger{logger: logger}
  75. }
  76. // Info log info
  77. func (l *Logger) Info(v ...interface{}) {
  78. l.logger.SetPrefix("[INFO]")
  79. l.logger.Println(v...)
  80. }
  81. // Debug log debug
  82. func (l *Logger) Debug(v ...interface{}) {
  83. l.logger.SetPrefix("[DEBUG]")
  84. l.logger.Println(v...)
  85. }
  86. // Warn log Warn
  87. func (l *Logger) Warn(v ...interface{}) {
  88. l.logger.SetPrefix("[Warn]")
  89. l.logger.Println(v...)
  90. }
  91. // Err log Err
  92. func (l *Logger) Err(v ...interface{}) {
  93. l.logger.SetPrefix("[Err]")
  94. l.logger.Println(v...)
  95. }
  96. // Alert log Alert
  97. func (l *Logger) Alert(v ...interface{}) {
  98. l.logger.SetPrefix("[Alert]")
  99. l.logger.Println(v...)
  100. }