monolog.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* A package that provides advanced logging facilities. */
  2. package monolog
  3. import (
  4. "os"
  5. "fmt"
  6. "time"
  7. "runtime"
  8. "path/filepath"
  9. "unicode"
  10. "strings"
  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 enableDisableSplitter(c rune) (bool) {
  107. ok := (!unicode.IsLetter(c))
  108. ok = ok && (!unicode.IsNumber(c))
  109. ok = ok && (c != '_')
  110. return ok
  111. }
  112. func (me * Logbook) EnableLevels(list string) {
  113. to_enable := strings.FieldsFunc(list, enableDisableSplitter)
  114. for _, level := range to_enable {
  115. me.EnableLevel(level)
  116. }
  117. }
  118. func (me * Logbook) DisableLevels(list string) {
  119. to_disable := strings.FieldsFunc(list, enableDisableSplitter)
  120. for _, level := range to_disable {
  121. me.DisableLevel(level)
  122. }
  123. }
  124. func (me * Logbook) LogVa(name string, file string, line int, format string, args...interface{}) {
  125. enabled, ok := me.levels[name]
  126. if (!ok) || (!enabled) {
  127. return
  128. }
  129. for _ , logger := range me.loggers {
  130. if (logger != nil) {
  131. logger.Log(name, file, line, format, args...)
  132. }
  133. }
  134. }
  135. func (me * Logbook) Close() {
  136. for index , logger := range me.loggers {
  137. if logger != nil {
  138. logger.Close()
  139. me.loggers[index] = nil
  140. }
  141. }
  142. }
  143. var DefaultLog * Logbook
  144. func init() {
  145. DefaultLog = NewLog()
  146. // runtime.SetFinalizer(DefaultLog, DefaultLog.Close)
  147. }
  148. func EnableLevel(level string) {
  149. DefaultLog.EnableLevel(level)
  150. }
  151. func DisableLevel(level string) {
  152. DefaultLog.DisableLevel(level)
  153. }
  154. func EnableLevels(list string) {
  155. DefaultLog.EnableLevels(list)
  156. }
  157. func DisableLevels(list string) {
  158. DefaultLog.DisableLevels(list)
  159. }
  160. func AddLogger(logger Logger, err error) {
  161. if err == nil {
  162. DefaultLog.AddLogger(logger)
  163. }
  164. }
  165. func Setup(name string, stderr bool, stdout bool) {
  166. if name != "" {
  167. AddLogger(NewFileLogger(name))
  168. }
  169. if stderr {
  170. AddLogger(NewStderrLogger())
  171. }
  172. if stdout {
  173. AddLogger(NewStdoutLogger())
  174. }
  175. EnableLevel("INFO")
  176. EnableLevel("WARNING")
  177. EnableLevel("ERROR")
  178. EnableLevel("FATAL")
  179. }
  180. func Close() {
  181. DefaultLog.Close()
  182. }
  183. func WriteLog(depth int, name string, format string, args ... interface{}) {
  184. _ ,file, line, ok := runtime.Caller(depth)
  185. if !ok {
  186. file = "unknown"
  187. line = 0
  188. }
  189. DefaultLog.LogVa(name, file, line, format, args...)
  190. }
  191. func Log(name string, format string, args ...interface{}) {
  192. WriteLog(2, name, format, args)
  193. }
  194. func Info(format string, args ...interface{}) {
  195. WriteLog(2, "INFO", format, args...)
  196. }
  197. func Warning(format string, args ...interface{}) {
  198. WriteLog(2, "WARNING", format, args...)
  199. }
  200. func Error(format string, args ...interface{}) {
  201. WriteLog(2, "ERROR", format, args...)
  202. }
  203. func Fatal(format string, args ...interface{}) {
  204. WriteLog(2, "FATAL", format, args...)
  205. }
  206. func Debug(format string, args ...interface{}) {
  207. WriteLog(2, "DEBUG", format, args...)
  208. }
  209. func エラ(err error) {
  210. WriteLog(2, "ERROR", "%s", err.Error())
  211. }
  212. func WriteError(err error) {
  213. WriteLog(2, "ERROR", "%s", err.Error())
  214. }