logger.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. return New(w)
  21. }
  22. // Writer returns the output destination for the standard logger.
  23. func (l *Logger) Writer() io.Writer {
  24. return l.logger.Writer()
  25. }
  26. // SetOutput sets the output destination for the standard logger.
  27. func (l *Logger) SetOutput(w io.Writer) {
  28. l.logger.SetOutput(w)
  29. }
  30. // Info log info
  31. func (l *Logger) Info(v ...interface{}) {
  32. l.logger.SetPrefix("[INFO]")
  33. l.logger.Println(v...)
  34. }
  35. // Debug log debug
  36. func (l *Logger) Debug(v ...interface{}) {
  37. l.logger.SetPrefix("[DEBUG]")
  38. l.logger.Println(v...)
  39. }
  40. // Warn log Warn
  41. func (l *Logger) Warn(v ...interface{}) {
  42. l.logger.SetPrefix("[Warn]")
  43. l.logger.Println(v...)
  44. }
  45. // Err log Err
  46. func (l *Logger) Err(v ...interface{}) {
  47. l.logger.SetPrefix("[Err]")
  48. l.logger.Println(v...)
  49. }
  50. // Alert log Alert
  51. func (l *Logger) Alert(v ...interface{}) {
  52. l.logger.SetPrefix("[Alert]")
  53. l.logger.Println(v...)
  54. }
  55. // Fatal log Fatal
  56. func (l *Logger) Fatal(v ...interface{}) {
  57. l.logger.SetPrefix("[Fatal]")
  58. l.logger.Println(v...)
  59. }
  60. // Panic log Panic
  61. func (l *Logger) Panic(v ...interface{}) {
  62. l.logger.SetPrefix("[Panic]")
  63. l.logger.Println(v...)
  64. }
  65. // Writer returns the output destination for the standard logger.
  66. func Writer() io.Writer {
  67. return defLog.Writer()
  68. }
  69. // SetOutput sets the output destination for the standard logger.
  70. func SetOutput(w io.Writer) {
  71. defLog.SetOutput(w)
  72. }
  73. // Info info
  74. func Info(v ...interface{}) {
  75. defLog.Info(v...)
  76. }
  77. // Debug debug
  78. func Debug(v ...interface{}) {
  79. defLog.Debug(v...)
  80. }
  81. // Warn warn
  82. func Warn(v ...interface{}) {
  83. defLog.Warn(v...)
  84. }
  85. // Err err
  86. func Err(v ...interface{}) {
  87. defLog.Err(v...)
  88. }
  89. // Alert alert
  90. func Alert(v ...interface{}) {
  91. defLog.Alert(v...)
  92. }
  93. // Fatal fatal
  94. func Fatal(v ...interface{}) {
  95. defLog.Fatal(v...)
  96. }
  97. // Panic panic
  98. func Panic(v ...interface{}) {
  99. defLog.Panic(v...)
  100. }