glog_config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "io"
  9. )
  10. // SetConfig set configurations for the logger.
  11. func SetConfig(config Config) error {
  12. return logger.SetConfig(config)
  13. }
  14. // SetConfigWithMap set configurations with map for the logger.
  15. func SetConfigWithMap(m map[string]interface{}) error {
  16. return logger.SetConfigWithMap(m)
  17. }
  18. // SetPath sets the directory path for file logging.
  19. func SetPath(path string) error {
  20. return logger.SetPath(path)
  21. }
  22. // GetPath returns the logging directory path for file logging.
  23. // It returns empty string if no directory path set.
  24. func GetPath() string {
  25. return logger.GetPath()
  26. }
  27. // SetFile sets the file name `pattern` for file logging.
  28. // Datetime pattern can be used in `pattern`, eg: access-{Ymd}.log.
  29. // The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
  30. func SetFile(pattern string) {
  31. logger.SetFile(pattern)
  32. }
  33. // SetLevel sets the default logging level.
  34. func SetLevel(level int) {
  35. logger.SetLevel(level)
  36. }
  37. // GetLevel returns the default logging level value.
  38. func GetLevel() int {
  39. return logger.GetLevel()
  40. }
  41. // SetWriter sets the customized logging `writer` for logging.
  42. // The `writer` object should implements the io.Writer interface.
  43. // Developer can use customized logging `writer` to redirect logging output to another service,
  44. // eg: kafka, mysql, mongodb, etc.
  45. func SetWriter(writer io.Writer) {
  46. logger.SetWriter(writer)
  47. }
  48. // GetWriter returns the customized writer object, which implements the io.Writer interface.
  49. // It returns nil if no customized writer set.
  50. func GetWriter() io.Writer {
  51. return logger.GetWriter()
  52. }
  53. // SetDebug enables/disables the debug level for default logger.
  54. // The debug level is enabled in default.
  55. func SetDebug(debug bool) {
  56. logger.SetDebug(debug)
  57. }
  58. // SetAsync enables/disables async logging output feature for default logger.
  59. func SetAsync(enabled bool) {
  60. logger.SetAsync(enabled)
  61. }
  62. // SetStdoutPrint sets whether ouptput the logging contents to stdout, which is true in default.
  63. func SetStdoutPrint(enabled bool) {
  64. logger.SetStdoutPrint(enabled)
  65. }
  66. // SetHeaderPrint sets whether output header of the logging contents, which is true in default.
  67. func SetHeaderPrint(enabled bool) {
  68. logger.SetHeaderPrint(enabled)
  69. }
  70. // SetPrefix sets prefix string for every logging content.
  71. // Prefix is part of header, which means if header output is shut, no prefix will be output.
  72. func SetPrefix(prefix string) {
  73. logger.SetPrefix(prefix)
  74. }
  75. // SetFlags sets extra flags for logging output features.
  76. func SetFlags(flags int) {
  77. logger.SetFlags(flags)
  78. }
  79. // GetFlags returns the flags of logger.
  80. func GetFlags() int {
  81. return logger.GetFlags()
  82. }
  83. // SetCtxKeys sets the context keys for logger. The keys is used for retrieving values
  84. // from context and printing them to logging content.
  85. //
  86. // Note that multiple calls of this function will overwrite the previous set context keys.
  87. func SetCtxKeys(keys ...interface{}) {
  88. logger.SetCtxKeys(keys...)
  89. }
  90. // GetCtxKeys retrieves and returns the context keys for logging.
  91. func GetCtxKeys() []interface{} {
  92. return logger.GetCtxKeys()
  93. }
  94. // PrintStack prints the caller stack,
  95. // the optional parameter `skip` specify the skipped stack offset from the end point.
  96. func PrintStack(skip ...int) {
  97. logger.PrintStack(skip...)
  98. }
  99. // GetStack returns the caller stack content,
  100. // the optional parameter `skip` specify the skipped stack offset from the end point.
  101. func GetStack(skip ...int) string {
  102. return logger.GetStack(skip...)
  103. }
  104. // SetStack enables/disables the stack feature in failure logging outputs.
  105. func SetStack(enabled bool) {
  106. logger.SetStack(enabled)
  107. }
  108. // SetLevelStr sets the logging level by level string.
  109. func SetLevelStr(levelStr string) error {
  110. return logger.SetLevelStr(levelStr)
  111. }
  112. // SetLevelPrefix sets the prefix string for specified level.
  113. func SetLevelPrefix(level int, prefix string) {
  114. logger.SetLevelPrefix(level, prefix)
  115. }
  116. // SetLevelPrefixes sets the level to prefix string mapping for the logger.
  117. func SetLevelPrefixes(prefixes map[int]string) {
  118. logger.SetLevelPrefixes(prefixes)
  119. }
  120. // GetLevelPrefix returns the prefix string for specified level.
  121. func GetLevelPrefix(level int) string {
  122. return logger.GetLevelPrefix(level)
  123. }
  124. // SetHandlers sets the logging handlers for default logger.
  125. func SetHandlers(handlers ...Handler) {
  126. logger.SetHandlers(handlers...)
  127. }
  128. //SetWriterColorEnable sets the file logging with color
  129. func SetWriterColorEnable(enabled bool) {
  130. logger.SetWriterColorEnable(enabled)
  131. }