log.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package log
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. _log "log"
  7. "os"
  8. "strings"
  9. "git.giaever.org/joachimmg/go-log.git/config"
  10. "git.giaever.org/joachimmg/go-log.git/errors"
  11. )
  12. // Init flags
  13. func init() {
  14. flag.Var(&config.Llo, "llo", "Minimum log level that will be written to stdout.")
  15. flag.Var(&config.Llf, "llf", "Minimum log level that will be written to file")
  16. flag.Var(&config.Ldir, "ldir", "Directory to log to")
  17. }
  18. // Loggers
  19. var (
  20. logTrace *_log.Logger
  21. logInfo *_log.Logger
  22. logWarning *_log.Logger
  23. logError *_log.Logger
  24. logPanic *_log.Logger
  25. )
  26. // Create log-file in director: Also creates the directory tree.
  27. func log_file(p config.LogPrefix) *os.File {
  28. os.MkdirAll(config.Ldir.String(), 0777)
  29. f, err := os.OpenFile(
  30. fmt.Sprintf("%s%c%s.log", config.Ldir, os.PathSeparator, strings.ToLower(string(p))),
  31. config.FileMask,
  32. config.FilePerm,
  33. )
  34. if err != nil {
  35. _log.Fatal(err.Error())
  36. return nil
  37. }
  38. return f
  39. }
  40. // Create new logger
  41. func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Logger {
  42. if (config.Llf&l) == config.Llf && (config.Llo&l) == config.Llo {
  43. f := log_file(p)
  44. return _log.New(
  45. io.MultiWriter(f, h),
  46. fmt.Sprintf("%s ", p),
  47. config.LogFormat,
  48. )
  49. }
  50. if (config.Llf & l) == config.Llf {
  51. f := log_file(p)
  52. return _log.New(
  53. f, fmt.Sprintf("%s ", p),
  54. config.LogFormat,
  55. )
  56. }
  57. if (config.Llo & l) == config.Llo {
  58. return _log.New(
  59. h, fmt.Sprintf("%s ", p),
  60. config.LogFormat,
  61. )
  62. }
  63. return _log.New(
  64. config.DiscardHandle, fmt.Sprintf("%s ", p),
  65. config.LogFormat,
  66. )
  67. }
  68. // Internal log function based on log level
  69. func log(t config.LogLevel) *_log.Logger {
  70. var l *_log.Logger
  71. switch t {
  72. case config.Trace:
  73. if logTrace == nil {
  74. logTrace = log_new(t, config.HandleTrace, config.PrefixTrace)
  75. }
  76. l = logTrace
  77. case config.Info:
  78. if logInfo == nil {
  79. logInfo = log_new(t, config.HandleInfo, config.PrefixInfo)
  80. }
  81. l = logInfo
  82. case config.Warning:
  83. if logWarning == nil {
  84. logWarning = log_new(t, config.HandleWarning, config.PrefixWarning)
  85. }
  86. l = logWarning
  87. case config.Error:
  88. if logError == nil {
  89. logError = log_new(t, config.HandleError, config.PrefixError)
  90. }
  91. l = logError
  92. case config.Panic:
  93. if logPanic == nil {
  94. logPanic = log_new(t, config.HandlePanic, config.PrefixPanic)
  95. }
  96. l = logPanic
  97. }
  98. if l == nil {
  99. _log.Panic(errors.UnknownLogger.Error())
  100. }
  101. return l
  102. }
  103. // Llo returns output log level config
  104. func Llo() *config.LogLevel {
  105. return &config.Llo
  106. }
  107. // Llf return file log level config
  108. func Llf() *config.LogLevel {
  109. return &config.Llf
  110. }
  111. // Ldir return log directory config
  112. func Ldir() *config.LogDir {
  113. return &config.Ldir
  114. }
  115. // Trace
  116. func Trace(m string) {
  117. log(config.Trace).Print(m)
  118. }
  119. // Tracef: format
  120. func Tracef(f string, v ...interface{}) {
  121. log(config.Trace).Printf(f, v...)
  122. }
  123. // Traceln: format standard
  124. func Traceln(v ...interface{}) {
  125. log(config.Trace).Println(v...)
  126. }
  127. // Info
  128. func Info(m string) {
  129. log(config.Info).Print(m)
  130. }
  131. // Infof: format
  132. func Infof(f string, v ...interface{}) {
  133. log(config.Info).Printf(f, v...)
  134. }
  135. // Info: format standard
  136. func Infoln(v ...interface{}) {
  137. log(config.Info).Println(v...)
  138. }
  139. // Warning
  140. func Warning(m string) {
  141. log(config.Warning).Print(m)
  142. }
  143. // Warningf: format
  144. func Warningf(f string, v ...interface{}) {
  145. log(config.Warning).Printf(f, v...)
  146. }
  147. // Warningln: format standard
  148. func Warningln(v ...interface{}) {
  149. log(config.Warning).Println(v...)
  150. }
  151. // Error
  152. func Error(m string) {
  153. log(config.Error).Fatal(m)
  154. }
  155. // Errorf: format
  156. func Errorf(f string, v ...interface{}) {
  157. log(config.Error).Fatalf(f, v...)
  158. }
  159. // Errorln: format standard
  160. func Errorln(v ...interface{}) {
  161. log(config.Error).Fatalln(v...)
  162. }
  163. // Panic
  164. func Panic(m string) {
  165. log(config.Panic).Panic(m)
  166. }
  167. // Panicf: format
  168. func Panicf(f string, v ...interface{}) {
  169. log(config.Panic).Panicf(f, v...)
  170. }
  171. // Panicln: format standard
  172. func Panicln(v ...interface{}) {
  173. log(config.Panic).Panicln(v...)
  174. }