glog_logger_api.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package glog
  7. import (
  8. "fmt"
  9. "os"
  10. )
  11. // Print prints `v` with newline using fmt.Sprintln.
  12. // The parameter `v` can be multiple variables.
  13. func (l *Logger) Print(v ...interface{}) {
  14. l.printStd(LEVEL_NONE, v...)
  15. }
  16. // Printf prints `v` with format `format` using fmt.Sprintf.
  17. // The parameter `v` can be multiple variables.
  18. func (l *Logger) Printf(format string, v ...interface{}) {
  19. l.printStd(LEVEL_NONE, l.format(format, v...))
  20. }
  21. // Println is alias of Print.
  22. // See Print.
  23. func (l *Logger) Println(v ...interface{}) {
  24. l.Print(v...)
  25. }
  26. // Fatal prints the logging content with [FATA] header and newline, then exit the current process.
  27. func (l *Logger) Fatal(v ...interface{}) {
  28. l.printErr(LEVEL_FATA, v...)
  29. os.Exit(1)
  30. }
  31. // Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.
  32. func (l *Logger) Fatalf(format string, v ...interface{}) {
  33. l.printErr(LEVEL_FATA, l.format(format, v...))
  34. os.Exit(1)
  35. }
  36. // Panic prints the logging content with [PANI] header and newline, then panics.
  37. func (l *Logger) Panic(v ...interface{}) {
  38. l.printErr(LEVEL_PANI, v...)
  39. panic(fmt.Sprint(v...))
  40. }
  41. // Panicf prints the logging content with [PANI] header, custom format and newline, then panics.
  42. func (l *Logger) Panicf(format string, v ...interface{}) {
  43. l.printErr(LEVEL_PANI, l.format(format, v...))
  44. panic(l.format(format, v...))
  45. }
  46. // Info prints the logging content with [INFO] header and newline.
  47. func (l *Logger) Info(v ...interface{}) {
  48. if l.checkLevel(LEVEL_INFO) {
  49. l.printStd(LEVEL_INFO, v...)
  50. }
  51. }
  52. // Infof prints the logging content with [INFO] header, custom format and newline.
  53. func (l *Logger) Infof(format string, v ...interface{}) {
  54. if l.checkLevel(LEVEL_INFO) {
  55. l.printStd(LEVEL_INFO, l.format(format, v...))
  56. }
  57. }
  58. // Debug prints the logging content with [DEBU] header and newline.
  59. func (l *Logger) Debug(v ...interface{}) {
  60. if l.checkLevel(LEVEL_DEBU) {
  61. l.printStd(LEVEL_DEBU, v...)
  62. }
  63. }
  64. // Debugf prints the logging content with [DEBU] header, custom format and newline.
  65. func (l *Logger) Debugf(format string, v ...interface{}) {
  66. if l.checkLevel(LEVEL_DEBU) {
  67. l.printStd(LEVEL_DEBU, l.format(format, v...))
  68. }
  69. }
  70. // Notice prints the logging content with [NOTI] header and newline.
  71. // It also prints caller stack info if stack feature is enabled.
  72. func (l *Logger) Notice(v ...interface{}) {
  73. if l.checkLevel(LEVEL_NOTI) {
  74. l.printStd(LEVEL_NOTI, v...)
  75. }
  76. }
  77. // Noticef prints the logging content with [NOTI] header, custom format and newline.
  78. // It also prints caller stack info if stack feature is enabled.
  79. func (l *Logger) Noticef(format string, v ...interface{}) {
  80. if l.checkLevel(LEVEL_NOTI) {
  81. l.printStd(LEVEL_NOTI, l.format(format, v...))
  82. }
  83. }
  84. // Warning prints the logging content with [WARN] header and newline.
  85. // It also prints caller stack info if stack feature is enabled.
  86. func (l *Logger) Warning(v ...interface{}) {
  87. if l.checkLevel(LEVEL_WARN) {
  88. l.printStd(LEVEL_WARN, v...)
  89. }
  90. }
  91. // Warningf prints the logging content with [WARN] header, custom format and newline.
  92. // It also prints caller stack info if stack feature is enabled.
  93. func (l *Logger) Warningf(format string, v ...interface{}) {
  94. if l.checkLevel(LEVEL_WARN) {
  95. l.printStd(LEVEL_WARN, l.format(format, v...))
  96. }
  97. }
  98. // Error prints the logging content with [ERRO] header and newline.
  99. // It also prints caller stack info if stack feature is enabled.
  100. func (l *Logger) Error(v ...interface{}) {
  101. if l.checkLevel(LEVEL_ERRO) {
  102. l.printErr(LEVEL_ERRO, v...)
  103. }
  104. }
  105. // Errorf prints the logging content with [ERRO] header, custom format and newline.
  106. // It also prints caller stack info if stack feature is enabled.
  107. func (l *Logger) Errorf(format string, v ...interface{}) {
  108. if l.checkLevel(LEVEL_ERRO) {
  109. l.printErr(LEVEL_ERRO, l.format(format, v...))
  110. }
  111. }
  112. // Critical prints the logging content with [CRIT] header and newline.
  113. // It also prints caller stack info if stack feature is enabled.
  114. func (l *Logger) Critical(v ...interface{}) {
  115. if l.checkLevel(LEVEL_CRIT) {
  116. l.printErr(LEVEL_CRIT, v...)
  117. }
  118. }
  119. // Criticalf prints the logging content with [CRIT] header, custom format and newline.
  120. // It also prints caller stack info if stack feature is enabled.
  121. func (l *Logger) Criticalf(format string, v ...interface{}) {
  122. if l.checkLevel(LEVEL_CRIT) {
  123. l.printErr(LEVEL_CRIT, l.format(format, v...))
  124. }
  125. }
  126. // checkLevel checks whether the given `level` could be output.
  127. func (l *Logger) checkLevel(level int) bool {
  128. return l.config.Level&level > 0
  129. }