log.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package log
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. _log "log"
  7. "os"
  8. "strings"
  9. "source.uit.com/mikevoets/inf-3200-1/tools/log/config"
  10. "source.uit.com/mikevoets/inf-3200-1/tools/log/errors"
  11. )
  12. func log_file(p config.LogPrefix) *os.File {
  13. os.MkdirAll(config.LOG_DIR, 0777)
  14. f, err := os.OpenFile(
  15. fmt.Sprintf("%s%c%s%s.log", config.LOG_DIR, os.PathSeparator, strings.ToLower(string(p)), config.FILE_EXTRA),
  16. config.FILE_MASK,
  17. config.FILE_PERM,
  18. )
  19. if err != nil {
  20. _log.Fatal(err.Error())
  21. return nil
  22. }
  23. return f
  24. }
  25. func log_new(l config.LogLevel, h config.LogHandle, p config.LogPrefix) *_log.Logger {
  26. if (config.LOG_LEVEL_FILE&l) == config.LOG_LEVEL_FILE && (config.LOG_LEVEL_OUTPUT&l) == config.LOG_LEVEL_OUTPUT {
  27. f := log_file(p)
  28. return _log.New(
  29. io.MultiWriter(f, h),
  30. fmt.Sprintf("%s ", p),
  31. config.LOG_FORMAT,
  32. )
  33. }
  34. if (config.LOG_LEVEL_FILE & l) == config.LOG_LEVEL_FILE {
  35. f := log_file(p)
  36. return _log.New(
  37. f, fmt.Sprintf("%s ", p),
  38. config.LOG_FORMAT,
  39. )
  40. }
  41. if (config.LOG_LEVEL_OUTPUT & l) == config.LOG_LEVEL_OUTPUT {
  42. return _log.New(
  43. h, fmt.Sprintf("%s ", p),
  44. config.LOG_FORMAT,
  45. )
  46. }
  47. return _log.New(
  48. config.DISCARD_HANDLE, fmt.Sprintf("%s ", p),
  49. config.LOG_FORMAT,
  50. )
  51. }
  52. func init() {
  53. flag.Var(&config.LOG_LEVEL_OUTPUT, "llo", "Minimum log level that will be written to stdout.")
  54. flag.Var(&config.LOG_LEVEL_FILE, "llf", "Minimum log level that will be written to file")
  55. }
  56. func log(t config.LogLevel) *_log.Logger {
  57. var l *_log.Logger
  58. switch t {
  59. case config.TRACE:
  60. if config.Trace == nil {
  61. config.Trace = log_new(t, config.HANDLE_TRACE, config.PREFIX_TRACE)
  62. }
  63. l = config.Trace
  64. case config.INFO:
  65. if config.Info == nil {
  66. config.Info = log_new(t, config.HANDLE_INFO, config.PREFIX_INFO)
  67. }
  68. l = config.Info
  69. case config.WARNING:
  70. if config.Warning == nil {
  71. config.Warning = log_new(t, config.HANDLE_WARNING, config.PREFIX_WARNING)
  72. }
  73. l = config.Warning
  74. case config.ERROR:
  75. if config.Error == nil {
  76. config.Error = log_new(t, config.HANDLE_ERROR, config.PREFIX_ERROR)
  77. }
  78. l = config.Error
  79. case config.PANIC:
  80. if config.Panic == nil {
  81. config.Panic = log_new(t, config.HANDLE_ERROR, config.PREFIX_PANIC)
  82. }
  83. l = config.Panic
  84. }
  85. if l == nil {
  86. _log.Panic(errors.UNKNOWN_LOGGER.Error())
  87. }
  88. return l
  89. }
  90. func LLO() *config.LogLevel {
  91. return &config.LOG_LEVEL_OUTPUT
  92. }
  93. func LLF() *config.LogLevel {
  94. return &config.LOG_LEVEL_FILE
  95. }
  96. func Trace(m string) {
  97. log(config.TRACE).Print(m)
  98. }
  99. func Tracef(f string, v ...interface{}) {
  100. log(config.TRACE).Printf(f, v...)
  101. }
  102. func Traceln(v ...interface{}) {
  103. log(config.TRACE).Println(v...)
  104. }
  105. func Info(m string) {
  106. log(config.INFO).Print(m)
  107. }
  108. func Infof(f string, v ...interface{}) {
  109. log(config.INFO).Printf(f, v...)
  110. }
  111. func Infoln(v ...interface{}) {
  112. log(config.INFO).Println(v...)
  113. }
  114. func Warning(m string) {
  115. log(config.WARNING).Print(m)
  116. }
  117. func Warningf(f string, v ...interface{}) {
  118. log(config.WARNING).Printf(f, v...)
  119. }
  120. func Warningln(v ...interface{}) {
  121. log(config.WARNING).Println(v...)
  122. }
  123. func Error(m string) {
  124. log(config.ERROR).Fatal(m)
  125. }
  126. func Errorf(f string, v ...interface{}) {
  127. log(config.ERROR).Fatalf(f, v...)
  128. }
  129. func Errorln(v ...interface{}) {
  130. log(config.ERROR).Fatalln(v...)
  131. }
  132. func Panic(m string) {
  133. log(config.PANIC).Panic(m)
  134. }
  135. func Panicf(f string, v ...interface{}) {
  136. log(config.PANIC).Panicf(f, v...)
  137. }
  138. func Panicln(v ...interface{}) {
  139. log(config.PANIC).Panicln(v...)
  140. }