logger.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package xlog
  2. import (
  3. "io"
  4. "log"
  5. )
  6. // Logger logger
  7. type Logger struct {
  8. logger *log.Logger
  9. }
  10. var (
  11. defLog *Logger
  12. )
  13. // New new logger
  14. func New(w io.Writer) *Logger {
  15. logger := log.New(w, "[INFO]", log.Ldate|log.Ltime)
  16. return &Logger{logger: logger}
  17. }
  18. // DefaultLogger defult logger
  19. func DefaultLogger(w io.Writer) *Logger {
  20. defLog = New(w)
  21. return defLog
  22. }
  23. // Writer returns the output destination for the standard logger.
  24. func (l *Logger) Writer() io.Writer {
  25. return l.logger.Writer()
  26. }
  27. // SetOutput sets the output destination for the standard logger.
  28. func (l *Logger) SetOutput(w io.Writer) {
  29. l.logger.SetOutput(w)
  30. }
  31. // Info log info
  32. func (l *Logger) Info(v ...interface{}) {
  33. l.logger.SetPrefix("[INFO]")
  34. l.logger.Println(v...)
  35. }
  36. // Debug log debug
  37. func (l *Logger) Debug(v ...interface{}) {
  38. l.logger.SetPrefix("[DEBUG]")
  39. l.logger.Println(v...)
  40. }
  41. // Warn log Warn
  42. func (l *Logger) Warn(v ...interface{}) {
  43. l.logger.SetPrefix("[Warn]")
  44. l.logger.Println(v...)
  45. }
  46. // Err log Err
  47. func (l *Logger) Err(v ...interface{}) {
  48. l.logger.SetPrefix("[Err]")
  49. l.logger.Println(v...)
  50. }
  51. // Alert log Alert
  52. func (l *Logger) Alert(v ...interface{}) {
  53. l.logger.SetPrefix("[Alert]")
  54. l.logger.Println(v...)
  55. }
  56. // Fatal log Fatal
  57. func (l *Logger) Fatal(v ...interface{}) {
  58. l.logger.SetPrefix("[Fatal]")
  59. l.logger.Println(v...)
  60. }
  61. // Panic log Panic
  62. func (l *Logger) Panic(v ...interface{}) {
  63. l.logger.SetPrefix("[Panic]")
  64. l.logger.Println(v...)
  65. }
  66. // Writer returns the output destination for the standard logger.
  67. func Writer() io.Writer {
  68. return defLog.Writer()
  69. }
  70. // SetOutput sets the output destination for the standard logger.
  71. func SetOutput(w io.Writer) {
  72. defLog.SetOutput(w)
  73. }
  74. // Info info
  75. func Info(v ...interface{}) {
  76. defLog.Info(v...)
  77. }
  78. // Debug debug
  79. func Debug(v ...interface{}) {
  80. defLog.Debug(v...)
  81. }
  82. // Warn warn
  83. func Warn(v ...interface{}) {
  84. defLog.Warn(v...)
  85. }
  86. // Err err
  87. func Err(v ...interface{}) {
  88. defLog.Err(v...)
  89. }
  90. // Alert alert
  91. func Alert(v ...interface{}) {
  92. defLog.Alert(v...)
  93. }
  94. // Fatal fatal
  95. func Fatal(v ...interface{}) {
  96. defLog.Fatal(v...)
  97. }
  98. // Panic panic
  99. func Panic(v ...interface{}) {
  100. defLog.Panic(v...)
  101. }