sublogger.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // mauLogger - A logger for Go programs
  2. // Copyright (C) 2016-2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package maulogger
  17. import (
  18. "fmt"
  19. )
  20. type Sublogger struct {
  21. topLevel *BasicLogger
  22. parent Logger
  23. Module string
  24. DefaultLevel Level
  25. }
  26. // Sub creates a Sublogger
  27. func (log *BasicLogger) Sub(module string) Logger {
  28. return &Sublogger{
  29. topLevel: log,
  30. parent: log,
  31. Module: module,
  32. DefaultLevel: LevelInfo,
  33. }
  34. }
  35. func (log *Sublogger) GetParent() Logger {
  36. return log.parent
  37. }
  38. // Sub creates a Sublogger
  39. func (log *Sublogger) Sub(module string) Logger {
  40. return &Sublogger{
  41. topLevel: log.topLevel,
  42. parent: log,
  43. Module: fmt.Sprintf("%s/%s", log.Module, module),
  44. DefaultLevel: log.DefaultLevel,
  45. }
  46. }
  47. // SetModule changes the module name of this Sublogger
  48. func (log *Sublogger) SetModule(mod string) {
  49. log.Module = mod
  50. }
  51. // SetDefaultLevel changes the default logging level of this Sublogger
  52. func (log *Sublogger) SetDefaultLevel(lvl Level) {
  53. log.DefaultLevel = lvl
  54. }
  55. // SetParent changes the parent of this Sublogger
  56. func (log *Sublogger) SetParent(parent *BasicLogger) {
  57. log.topLevel = parent
  58. }
  59. //Write ...
  60. func (log *Sublogger) Write(p []byte) (n int, err error) {
  61. log.topLevel.Raw(log.DefaultLevel, log.Module, string(p))
  62. return len(p), nil
  63. }
  64. // Log formats the given parts with fmt.Sprint and logs the result with the given level
  65. func (log *Sublogger) Log(level Level, parts ...interface{}) {
  66. log.topLevel.Raw(level, "", fmt.Sprint(parts...))
  67. }
  68. // Logln formats the given parts with fmt.Sprintln and logs the result with the given level
  69. func (log *Sublogger) Logln(level Level, parts ...interface{}) {
  70. log.topLevel.Raw(level, "", fmt.Sprintln(parts...))
  71. }
  72. // Logf formats the given message and args with fmt.Sprintf and logs the result with the given level
  73. func (log *Sublogger) Logf(level Level, message string, args ...interface{}) {
  74. log.topLevel.Raw(level, "", fmt.Sprintf(message, args...))
  75. }
  76. // Logfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the given level
  77. func (log *Sublogger) Logfln(level Level, message string, args ...interface{}) {
  78. log.topLevel.Raw(level, log.Module, fmt.Sprintf(message+"\n", args...))
  79. }
  80. // Debug formats the given parts with fmt.Sprint and logs the result with the Debug level
  81. func (log *Sublogger) Debug(parts ...interface{}) {
  82. log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprint(parts...))
  83. }
  84. // Debugln formats the given parts with fmt.Sprintln and logs the result with the Debug level
  85. func (log *Sublogger) Debugln(parts ...interface{}) {
  86. log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintln(parts...))
  87. }
  88. // Debugf formats the given message and args with fmt.Sprintf and logs the result with the Debug level
  89. func (log *Sublogger) Debugf(message string, args ...interface{}) {
  90. log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintf(message, args...))
  91. }
  92. // Debugfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Debug level
  93. func (log *Sublogger) Debugfln(message string, args ...interface{}) {
  94. log.topLevel.Raw(LevelDebug, log.Module, fmt.Sprintf(message+"\n", args...))
  95. }
  96. // Info formats the given parts with fmt.Sprint and logs the result with the Info level
  97. func (log *Sublogger) Info(parts ...interface{}) {
  98. log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprint(parts...))
  99. }
  100. // Infoln formats the given parts with fmt.Sprintln and logs the result with the Info level
  101. func (log *Sublogger) Infoln(parts ...interface{}) {
  102. log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintln(parts...))
  103. }
  104. // Infof formats the given message and args with fmt.Sprintf and logs the result with the Info level
  105. func (log *Sublogger) Infof(message string, args ...interface{}) {
  106. log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintf(message, args...))
  107. }
  108. // Infofln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Info level
  109. func (log *Sublogger) Infofln(message string, args ...interface{}) {
  110. log.topLevel.Raw(LevelInfo, log.Module, fmt.Sprintf(message+"\n", args...))
  111. }
  112. // Warn formats the given parts with fmt.Sprint and logs the result with the Warn level
  113. func (log *Sublogger) Warn(parts ...interface{}) {
  114. log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprint(parts...))
  115. }
  116. // Warnln formats the given parts with fmt.Sprintln and logs the result with the Warn level
  117. func (log *Sublogger) Warnln(parts ...interface{}) {
  118. log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintln(parts...))
  119. }
  120. // Warnf formats the given message and args with fmt.Sprintf and logs the result with the Warn level
  121. func (log *Sublogger) Warnf(message string, args ...interface{}) {
  122. log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintf(message, args...))
  123. }
  124. // Warnfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Warn level
  125. func (log *Sublogger) Warnfln(message string, args ...interface{}) {
  126. log.topLevel.Raw(LevelWarn, log.Module, fmt.Sprintf(message+"\n", args...))
  127. }
  128. // Error formats the given parts with fmt.Sprint and logs the result with the Error level
  129. func (log *Sublogger) Error(parts ...interface{}) {
  130. log.topLevel.Raw(LevelError, log.Module, fmt.Sprint(parts...))
  131. }
  132. // Errorln formats the given parts with fmt.Sprintln and logs the result with the Error level
  133. func (log *Sublogger) Errorln(parts ...interface{}) {
  134. log.topLevel.Raw(LevelError, log.Module, fmt.Sprintln(parts...))
  135. }
  136. // Errorf formats the given message and args with fmt.Sprintf and logs the result with the Error level
  137. func (log *Sublogger) Errorf(message string, args ...interface{}) {
  138. log.topLevel.Raw(LevelError, log.Module, fmt.Sprintf(message, args...))
  139. }
  140. // Errorfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Error level
  141. func (log *Sublogger) Errorfln(message string, args ...interface{}) {
  142. log.topLevel.Raw(LevelError, log.Module, fmt.Sprintf(message+"\n", args...))
  143. }
  144. // Fatal formats the given parts with fmt.Sprint and logs the result with the Fatal level
  145. func (log *Sublogger) Fatal(parts ...interface{}) {
  146. log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprint(parts...))
  147. }
  148. // Fatalln formats the given parts with fmt.Sprintln and logs the result with the Fatal level
  149. func (log *Sublogger) Fatalln(parts ...interface{}) {
  150. log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintln(parts...))
  151. }
  152. // Fatalf formats the given message and args with fmt.Sprintf and logs the result with the Fatal level
  153. func (log *Sublogger) Fatalf(message string, args ...interface{}) {
  154. log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintf(message, args...))
  155. }
  156. // Fatalfln formats the given message and args with fmt.Sprintf, appends a newline and logs the result with the Fatal level
  157. func (log *Sublogger) Fatalfln(message string, args ...interface{}) {
  158. log.topLevel.Raw(LevelFatal, log.Module, fmt.Sprintf(message+"\n", args...))
  159. }