monolog.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* A package that provides advanced logging facilities. */
  2. package monolog
  3. import (
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. "strings"
  9. "time"
  10. "unicode"
  11. )
  12. type Logger interface {
  13. Log(level string, file string, line int, format string, args ...interface{})
  14. Close()
  15. }
  16. func GetCallerName(depth int) {
  17. pc := make([]uintptr, depth+1)
  18. runtime.Callers(depth, pc)
  19. for i, v := range pc {
  20. fun := runtime.FuncForPC(v)
  21. if fun != nil {
  22. fmt.Printf("GetCallerName %d %s\n", i, fun.Name())
  23. } else {
  24. fmt.Printf("GetCallerName %d nil\n", i)
  25. }
  26. }
  27. }
  28. func (me *FileLogger) WriteLog(depth int, level string, format string, args ...interface{}) {
  29. _, file, line, ok := runtime.Caller(depth)
  30. if !ok {
  31. file = "unknown"
  32. line = 0
  33. }
  34. me.Log(level, file, line, format, args...)
  35. }
  36. func (me *FileLogger) NamedLog(name string, format string, args ...interface{}) {
  37. me.WriteLog(2, name, format, args...)
  38. }
  39. func (me *FileLogger) Info(format string, args ...interface{}) {
  40. me.WriteLog(2, "INFO", format, args...)
  41. }
  42. func (me *FileLogger) Warning(format string, args ...interface{}) {
  43. me.WriteLog(2, "WARNING", format, args...)
  44. }
  45. func (me *FileLogger) Error(format string, args ...interface{}) {
  46. me.WriteLog(2, "ERROR", format, args...)
  47. }
  48. func (me *FileLogger) Fatal(format string, args ...interface{}) {
  49. me.WriteLog(2, "FATAL", format, args...)
  50. }
  51. func (me *FileLogger) Debug(format string, args ...interface{}) {
  52. me.WriteLog(2, "DEBUG", format, args...)
  53. }
  54. type FileLogger struct {
  55. filename string
  56. file *os.File
  57. }
  58. func (me *FileLogger) Close() {
  59. if me.file != nil {
  60. me.file.Close()
  61. }
  62. me.file = nil
  63. }
  64. func (me *FileLogger) Log(level string, file string, line int, format string, args ...interface{}) {
  65. fileshort := filepath.Base(file)
  66. now := time.Now().Format(time.RFC3339)
  67. fmt.Fprintf(me.file, "%s: %s: %s: %d: ", now, level, fileshort, line)
  68. if args != nil && len(args) > 0 {
  69. fmt.Fprintf(me.file, format, args...)
  70. } else {
  71. fmt.Fprint(me.file, format)
  72. }
  73. fmt.Fprint(me.file, "\n")
  74. }
  75. func NewFileLogger(filename string) (logger Logger, err error) {
  76. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return &FileLogger{filename, file}, nil
  81. }
  82. func NewStderrLogger() (logger Logger, err error) {
  83. return &FileLogger{"/dev/stderr", os.Stderr}, nil
  84. }
  85. func NewStdoutLogger() (logger Logger, err error) {
  86. return &FileLogger{"/dev/stderr", os.Stdout}, nil
  87. }
  88. type Logbook struct {
  89. loggers []Logger
  90. levels map[string]bool
  91. }
  92. func NewLog() *Logbook {
  93. loggers := make([]Logger, 32)
  94. levels := make(map[string]bool)
  95. return &Logbook{loggers, levels}
  96. }
  97. func (me *Logbook) AddLogger(logger Logger) {
  98. me.loggers = append(me.loggers, logger)
  99. }
  100. func (me *Logbook) EnableLevel(level string) {
  101. me.levels[level] = true
  102. }
  103. func (me *Logbook) DisableLevel(level string) {
  104. me.levels[level] = false
  105. }
  106. func (me * Logbook) GetLevels() map[string] bool {
  107. return me.levels
  108. }
  109. func enableDisableSplitter(c rune) bool {
  110. ok := (!unicode.IsLetter(c))
  111. ok = ok && (!unicode.IsNumber(c))
  112. ok = ok && (c != '_')
  113. return ok
  114. }
  115. func (me *Logbook) EnableLevels(list string) {
  116. to_enable := strings.FieldsFunc(list, enableDisableSplitter)
  117. for _, level := range to_enable {
  118. me.EnableLevel(level)
  119. }
  120. }
  121. func (me *Logbook) DisableLevels(list string) {
  122. to_disable := strings.FieldsFunc(list, enableDisableSplitter)
  123. for _, level := range to_disable {
  124. me.DisableLevel(level)
  125. }
  126. }
  127. func (me *Logbook) LogVa(name string, file string, line int, format string, args ...interface{}) {
  128. enabled, ok := me.levels[name]
  129. if (!ok) || (!enabled) {
  130. return
  131. }
  132. for _, logger := range me.loggers {
  133. if logger != nil {
  134. logger.Log(name, file, line, format, args...)
  135. }
  136. }
  137. }
  138. func (me *Logbook) Close() {
  139. for index, logger := range me.loggers {
  140. if logger != nil {
  141. logger.Close()
  142. me.loggers[index] = nil
  143. }
  144. }
  145. }
  146. func (lb *Logbook) EnabledLevels() []string {
  147. var res []string
  148. for name, ll := range lb.levels {
  149. if ll {
  150. res = append(res, name)
  151. }
  152. }
  153. return res
  154. }
  155. var DefaultLog *Logbook
  156. func init() {
  157. DefaultLog = NewLog()
  158. // runtime.SetFinalizer(DefaultLog, DefaultLog.Close)
  159. }
  160. func EnableLevel(level string) {
  161. DefaultLog.EnableLevel(level)
  162. }
  163. func DisableLevel(level string) {
  164. DefaultLog.DisableLevel(level)
  165. }
  166. func EnableLevels(list string) {
  167. DefaultLog.EnableLevels(list)
  168. }
  169. func DisableLevels(list string) {
  170. DefaultLog.DisableLevels(list)
  171. }
  172. func GetLevels() map[string] bool {
  173. return DefaultLog.GetLevels();
  174. }
  175. func AddLogger(logger Logger, err error) {
  176. if err == nil {
  177. DefaultLog.AddLogger(logger)
  178. }
  179. }
  180. func Setup(name string, stderr bool, stdout bool) {
  181. if name != "" {
  182. AddLogger(NewFileLogger(name))
  183. }
  184. if stderr {
  185. AddLogger(NewStderrLogger())
  186. }
  187. if stdout {
  188. AddLogger(NewStdoutLogger())
  189. }
  190. EnableLevel("INFO")
  191. EnableLevel("WARNING")
  192. EnableLevel("ERROR")
  193. EnableLevel("FATAL")
  194. }
  195. func Close() {
  196. DefaultLog.Close()
  197. }
  198. func WriteLog(depth int, name string, format string, args ...interface{}) {
  199. _, file, line, ok := runtime.Caller(depth)
  200. if !ok {
  201. file = "unknown"
  202. line = 0
  203. }
  204. DefaultLog.LogVa(name, file, line, format, args...)
  205. }
  206. func Log(name string, format string, args ...interface{}) {
  207. WriteLog(2, name, format, args)
  208. }
  209. func Printf(format string, args ...interface{}) {
  210. WriteLog(2, "INFO", format, args...)
  211. }
  212. func Info(format string, args ...interface{}) {
  213. WriteLog(2, "INFO", format, args...)
  214. }
  215. func Warning(format string, args ...interface{}) {
  216. WriteLog(2, "WARNING", format, args...)
  217. }
  218. func Error(format string, args ...interface{}) {
  219. WriteLog(2, "ERROR", format, args...)
  220. }
  221. func Fatal(format string, args ...interface{}) {
  222. WriteLog(2, "FATAL", format, args...)
  223. }
  224. func Debug(format string, args ...interface{}) {
  225. WriteLog(2, "DEBUG", format, args...)
  226. }
  227. func エラ(err error) {
  228. WriteLog(2, "ERROR", "%s", err.Error())
  229. }
  230. func WriteError(err error) {
  231. WriteLog(2, "ERROR", "%s", err.Error())
  232. }