glog.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 implements powerful and easy-to-use leveled logging functionality.
  7. package glog
  8. import (
  9. "context"
  10. "github.com/gogf/gf/v2/internal/command"
  11. "github.com/gogf/gf/v2/os/grpool"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. )
  14. // ILogger is the API interface for logger.
  15. type ILogger interface {
  16. Print(ctx context.Context, v ...interface{})
  17. Printf(ctx context.Context, format string, v ...interface{})
  18. Debug(ctx context.Context, v ...interface{})
  19. Debugf(ctx context.Context, format string, v ...interface{})
  20. Info(ctx context.Context, v ...interface{})
  21. Infof(ctx context.Context, format string, v ...interface{})
  22. Notice(ctx context.Context, v ...interface{})
  23. Noticef(ctx context.Context, format string, v ...interface{})
  24. Warning(ctx context.Context, v ...interface{})
  25. Warningf(ctx context.Context, format string, v ...interface{})
  26. Error(ctx context.Context, v ...interface{})
  27. Errorf(ctx context.Context, format string, v ...interface{})
  28. Critical(ctx context.Context, v ...interface{})
  29. Criticalf(ctx context.Context, format string, v ...interface{})
  30. Panic(ctx context.Context, v ...interface{})
  31. Panicf(ctx context.Context, format string, v ...interface{})
  32. Fatal(ctx context.Context, v ...interface{})
  33. Fatalf(ctx context.Context, format string, v ...interface{})
  34. }
  35. const (
  36. commandEnvKeyForDebug = "gf.glog.debug"
  37. )
  38. var (
  39. // Ensure Logger implements ILogger.
  40. _ ILogger = &Logger{}
  41. // Default logger object, for package method usage.
  42. defaultLogger = New()
  43. // Goroutine pool for async logging output.
  44. // It uses only one asynchronous worker to ensure log sequence.
  45. asyncPool = grpool.New(1)
  46. // defaultDebug enables debug level or not in default,
  47. // which can be configured using command option or system environment.
  48. defaultDebug = true
  49. )
  50. func init() {
  51. defaultDebug = gconv.Bool(command.GetOptWithEnv(commandEnvKeyForDebug, "true"))
  52. SetDebug(defaultDebug)
  53. }
  54. // DefaultLogger returns the default logger.
  55. func DefaultLogger() *Logger {
  56. return defaultLogger
  57. }
  58. // SetDefaultLogger sets the default logger for package glog.
  59. // Note that there might be concurrent safety issue if calls this function
  60. // in different goroutines.
  61. func SetDefaultLogger(l *Logger) {
  62. defaultLogger = l
  63. }