clog.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2017 Unknwon
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. // Clog is a channel-based logging package for Go.
  15. package clog
  16. import (
  17. "fmt"
  18. "os"
  19. "path/filepath"
  20. "runtime"
  21. "strings"
  22. )
  23. const (
  24. _VERSION = "1.2.0"
  25. )
  26. // Version returns current version of the package.
  27. func Version() string {
  28. return _VERSION
  29. }
  30. type (
  31. MODE string
  32. LEVEL int
  33. )
  34. const (
  35. CONSOLE MODE = "console"
  36. FILE MODE = "file"
  37. SLACK MODE = "slack"
  38. DISCORD MODE = "discord"
  39. )
  40. const (
  41. TRACE LEVEL = iota
  42. INFO
  43. WARN
  44. ERROR
  45. FATAL
  46. )
  47. var formats = map[LEVEL]string{
  48. TRACE: "[TRACE] ",
  49. INFO: "[ INFO] ",
  50. WARN: "[ WARN] ",
  51. ERROR: "[ERROR] ",
  52. FATAL: "[FATAL] ",
  53. }
  54. // isValidLevel returns true if given level is in the valid range.
  55. func isValidLevel(level LEVEL) bool {
  56. return level >= TRACE && level <= FATAL
  57. }
  58. // Message represents a log message to be processed.
  59. type Message struct {
  60. Level LEVEL
  61. Body string
  62. }
  63. func Write(level LEVEL, skip int, format string, v ...interface{}) {
  64. msg := &Message{
  65. Level: level,
  66. }
  67. // Only error and fatal information needs locate position for debugging.
  68. // But if skip is 0 means caller doesn't care so we can skip.
  69. if msg.Level >= ERROR && skip > 0 {
  70. pc, file, line, ok := runtime.Caller(skip)
  71. if ok {
  72. // Get caller function name.
  73. fn := runtime.FuncForPC(pc)
  74. var fnName string
  75. if fn == nil {
  76. fnName = "?()"
  77. } else {
  78. fnName = strings.TrimLeft(filepath.Ext(fn.Name()), ".") + "()"
  79. }
  80. if len(file) > 20 {
  81. file = "..." + file[len(file)-20:]
  82. }
  83. msg.Body = formats[level] + fmt.Sprintf("[%s:%d %s] ", file, line, fnName) + fmt.Sprintf(format, v...)
  84. }
  85. }
  86. if len(msg.Body) == 0 {
  87. msg.Body = formats[level] + fmt.Sprintf(format, v...)
  88. }
  89. for i := range receivers {
  90. if receivers[i].Level() > level {
  91. continue
  92. }
  93. receivers[i].msgChan <- msg
  94. }
  95. }
  96. func Trace(format string, v ...interface{}) {
  97. Write(TRACE, 0, format, v...)
  98. }
  99. func Info(format string, v ...interface{}) {
  100. Write(INFO, 0, format, v...)
  101. }
  102. func Warn(format string, v ...interface{}) {
  103. Write(WARN, 0, format, v...)
  104. }
  105. func Error(skip int, format string, v ...interface{}) {
  106. Write(ERROR, skip, format, v...)
  107. }
  108. func Fatal(skip int, format string, v ...interface{}) {
  109. Write(FATAL, skip, format, v...)
  110. Shutdown()
  111. os.Exit(1)
  112. }
  113. func Shutdown() {
  114. for i := range receivers {
  115. receivers[i].Destroy()
  116. }
  117. // Shutdown the error handling goroutine.
  118. quitChan <- struct{}{}
  119. for {
  120. if len(errorChan) == 0 {
  121. break
  122. }
  123. fmt.Printf("clog: unable to write message: %v\n", <-errorChan)
  124. }
  125. }