glog.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 levelled logging functionality.
  7. package glog
  8. import (
  9. "github.com/gogf/gf/os/gcmd"
  10. "github.com/gogf/gf/os/grpool"
  11. )
  12. const (
  13. commandEnvKeyForDebug = "gf.glog.debug"
  14. )
  15. var (
  16. // Default logger object, for package method usage.
  17. logger = New()
  18. // Goroutine pool for async logging output.
  19. // It uses only one asynchronous worker to ensure log sequence.
  20. asyncPool = grpool.New(1)
  21. // defaultDebug enables debug level or not in default,
  22. // which can be configured using command option or system environment.
  23. defaultDebug = true
  24. )
  25. func init() {
  26. defaultDebug = gcmd.GetOptWithEnv(commandEnvKeyForDebug, true).Bool()
  27. SetDebug(defaultDebug)
  28. }
  29. // DefaultLogger returns the default logger.
  30. func DefaultLogger() *Logger {
  31. return logger
  32. }
  33. // SetDefaultLogger sets the default logger for package glog.
  34. // Note that there might be concurrent safety issue if calls this function
  35. // in different goroutines.
  36. func SetDefaultLogger(l *Logger) {
  37. logger = l
  38. }