log.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. var logger = NewDefault(newStdHandler())
  7. // SetDefaultLogger changes the global logger
  8. func SetDefaultLogger(l *Logger) {
  9. logger = l
  10. }
  11. // SetLevel changes the logger level
  12. func SetLevel(level Level) {
  13. logger.SetLevel(level)
  14. }
  15. // SetLevelByName changes the logger level by name
  16. func SetLevelByName(name string) {
  17. logger.SetLevelByName(name)
  18. }
  19. // Fatal records the log with fatal level and exits
  20. func Fatal(args ...interface{}) {
  21. logger.Output(2, LevelFatal, fmt.Sprint(args...))
  22. os.Exit(1)
  23. }
  24. // Fatalf records the log with fatal level and exits
  25. func Fatalf(format string, args ...interface{}) {
  26. logger.Output(2, LevelFatal, fmt.Sprintf(format, args...))
  27. os.Exit(1)
  28. }
  29. // Fatalln records the log with fatal level and exits
  30. func Fatalln(args ...interface{}) {
  31. logger.Output(2, LevelFatal, fmt.Sprintln(args...))
  32. os.Exit(1)
  33. }
  34. // Panic records the log with fatal level and panics
  35. func Panic(args ...interface{}) {
  36. msg := fmt.Sprint(args...)
  37. logger.Output(2, LevelError, msg)
  38. panic(msg)
  39. }
  40. // Panicf records the log with fatal level and panics
  41. func Panicf(format string, args ...interface{}) {
  42. msg := fmt.Sprintf(format, args...)
  43. logger.Output(2, LevelError, msg)
  44. panic(msg)
  45. }
  46. // Panicln records the log with fatal level and panics
  47. func Panicln(args ...interface{}) {
  48. msg := fmt.Sprintln(args...)
  49. logger.Output(2, LevelError, msg)
  50. panic(msg)
  51. }
  52. // Print records the log with trace level
  53. func Print(args ...interface{}) {
  54. logger.Output(2, LevelTrace, fmt.Sprint(args...))
  55. }
  56. // Print records the log with trace level
  57. func PrintJson(body interface{}) {
  58. logger.OutputJson(2, LevelTrace, body)
  59. }
  60. // Printf records the log with trace level
  61. func Printf(format string, args ...interface{}) {
  62. logger.Output(2, LevelTrace, fmt.Sprintf(format, args...))
  63. }
  64. // Println records the log with trace level
  65. func Println(args ...interface{}) {
  66. logger.Output(2, LevelTrace, fmt.Sprintln(args...))
  67. }
  68. // Debug records the log with debug level
  69. func Debug(args ...interface{}) {
  70. logger.Output(2, LevelDebug, fmt.Sprint(args...))
  71. }
  72. // Debug records the log with debug level
  73. func DebugJson(body interface{}) {
  74. logger.OutputJson(2, LevelDebug, body)
  75. }
  76. // Debugf records the log with debug level
  77. func Debugf(format string, args ...interface{}) {
  78. logger.Output(2, LevelDebug, fmt.Sprintf(format, args...))
  79. }
  80. // Debugln records the log with debug level
  81. func Debugln(args ...interface{}) {
  82. logger.Output(2, LevelDebug, fmt.Sprintln(args...))
  83. }
  84. // Error records the log with error level
  85. func Error(args ...interface{}) {
  86. logger.Output(2, LevelError, fmt.Sprint(args...))
  87. }
  88. // Error records the log with error level
  89. func ErrorJson(body interface{}) {
  90. logger.OutputJson(2, LevelError, body)
  91. }
  92. // Errorf records the log with error level
  93. func Errorf(format string, args ...interface{}) {
  94. logger.Output(2, LevelError, fmt.Sprintf(format, args...))
  95. }
  96. // Errorln records the log with error level
  97. func Errorln(args ...interface{}) {
  98. logger.Output(2, LevelError, fmt.Sprintln(args...))
  99. }
  100. // Info records the log with info level
  101. func Info(args ...interface{}) {
  102. logger.Output(2, LevelInfo, fmt.Sprint(args...))
  103. }
  104. // Info records the log with info level by json format
  105. func InfoJson(body interface{}) {
  106. logger.OutputJson(2, LevelInfo, body)
  107. }
  108. // Infof records the log with info level
  109. func Infof(format string, args ...interface{}) {
  110. logger.Output(2, LevelInfo, fmt.Sprintf(format, args...))
  111. }
  112. // Infoln records the log with info level
  113. func Infoln(args ...interface{}) {
  114. logger.Output(2, LevelInfo, fmt.Sprintln(args...))
  115. }
  116. // Warn records the log with warn level
  117. func Warn(args ...interface{}) {
  118. logger.Output(2, LevelWarn, fmt.Sprint(args...))
  119. }
  120. // Warnf records the log with warn level
  121. func Warnf(format string, args ...interface{}) {
  122. logger.Output(2, LevelWarn, fmt.Sprintf(format, args...))
  123. }
  124. // Warnln records the log with warn level
  125. func Warnln(args ...interface{}) {
  126. logger.Output(2, LevelWarn, fmt.Sprintln(args...))
  127. }