mlog.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright GoFrame gf 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 mlog
  7. import (
  8. "context"
  9. "github.com/gogf/gf/v2/os/gcmd"
  10. "github.com/gogf/gf/v2/os/genv"
  11. "github.com/gogf/gf/v2/os/glog"
  12. )
  13. const (
  14. headerPrintEnvName = "GF_CLI_MLOG_HEADER"
  15. )
  16. var (
  17. ctx = context.TODO()
  18. logger = glog.New()
  19. )
  20. func init() {
  21. logger.SetStack(false)
  22. if genv.Get(headerPrintEnvName).String() == "1" {
  23. logger.SetHeaderPrint(true)
  24. } else {
  25. logger.SetHeaderPrint(false)
  26. }
  27. if gcmd.GetOpt("debug") != nil || gcmd.GetOpt("gf.debug") != nil {
  28. logger.SetDebug(true)
  29. } else {
  30. logger.SetDebug(false)
  31. }
  32. }
  33. // SetHeaderPrint enables/disables header printing to stdout.
  34. func SetHeaderPrint(enabled bool) {
  35. logger.SetHeaderPrint(enabled)
  36. if enabled {
  37. _ = genv.Set(headerPrintEnvName, "1")
  38. } else {
  39. _ = genv.Set(headerPrintEnvName, "0")
  40. }
  41. }
  42. func Print(v ...interface{}) {
  43. logger.Print(ctx, v...)
  44. }
  45. func Printf(format string, v ...interface{}) {
  46. logger.Printf(ctx, format, v...)
  47. }
  48. func Fatal(v ...interface{}) {
  49. logger.Fatal(ctx, v...)
  50. }
  51. func Fatalf(format string, v ...interface{}) {
  52. logger.Fatalf(ctx, format, v...)
  53. }
  54. func Debug(v ...interface{}) {
  55. logger.Debug(ctx, v...)
  56. }
  57. func Debugf(format string, v ...interface{}) {
  58. logger.Debugf(ctx, format, v...)
  59. }