intlog.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). 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 intlog provides internal logging for GoFrame development usage only.
  7. package intlog
  8. import (
  9. "fmt"
  10. "github.com/gogf/gf/debug/gdebug"
  11. "github.com/gogf/gf/os/gcmd"
  12. "path/filepath"
  13. "time"
  14. )
  15. const (
  16. gFILTER_KEY = "/internal/intlog"
  17. )
  18. var (
  19. // isGFDebug marks whether printing GoFrame debug information.
  20. isGFDebug = false
  21. )
  22. func init() {
  23. // Debugging configured.
  24. if !gcmd.GetWithEnv("GF_DEBUG").IsEmpty() {
  25. isGFDebug = true
  26. return
  27. }
  28. }
  29. // SetEnabled enables/disables the internal logging manually.
  30. // Note that this function is not concurrent safe, be aware of the DATA RACE.
  31. func SetEnabled(enabled bool) {
  32. // If they're the same, it does not write the <isGFDebug> but only reading operation.
  33. if isGFDebug != enabled {
  34. isGFDebug = enabled
  35. }
  36. }
  37. // IsEnabled checks and returns whether current process is in GF development.
  38. func IsEnabled() bool {
  39. return isGFDebug
  40. }
  41. // Print prints <v> with newline using fmt.Println.
  42. // The parameter <v> can be multiple variables.
  43. func Print(v ...interface{}) {
  44. if !isGFDebug {
  45. return
  46. }
  47. fmt.Println(append([]interface{}{now(), "[INTE]", file()}, v...)...)
  48. }
  49. // Printf prints <v> with format <format> using fmt.Printf.
  50. // The parameter <v> can be multiple variables.
  51. func Printf(format string, v ...interface{}) {
  52. if !isGFDebug {
  53. return
  54. }
  55. fmt.Printf(now()+" [INTE] "+file()+" "+format+"\n", v...)
  56. }
  57. // Error prints <v> with newline using fmt.Println.
  58. // The parameter <v> can be multiple variables.
  59. func Error(v ...interface{}) {
  60. if !isGFDebug {
  61. return
  62. }
  63. array := append([]interface{}{now(), "[INTE]", file()}, v...)
  64. array = append(array, "\n"+gdebug.StackWithFilter(gFILTER_KEY))
  65. fmt.Println(array...)
  66. }
  67. // Errorf prints <v> with format <format> using fmt.Printf.
  68. func Errorf(format string, v ...interface{}) {
  69. if !isGFDebug {
  70. return
  71. }
  72. fmt.Printf(
  73. now()+" [INTE] "+file()+" "+format+"\n%s\n",
  74. append(v, gdebug.StackWithFilter(gFILTER_KEY))...,
  75. )
  76. }
  77. // now returns current time string.
  78. func now() string {
  79. return time.Now().Format("2006-01-02 15:04:05.000")
  80. }
  81. // file returns caller file name along with its line number.
  82. func file() string {
  83. _, p, l := gdebug.CallerWithFilter(gFILTER_KEY)
  84. return fmt.Sprintf(`%s:%d`, filepath.Base(p), l)
  85. }