monolog.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. if format[len(format)-1] != '\n' {
  74. fmt.Fprint(me.file, "\n")
  75. }
  76. }
  77. func NewFileLogger(filename string) (logger Logger, err error) {
  78. file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &FileLogger{filename, file}, nil
  83. }
  84. func NewStderrLogger() (logger Logger, err error) {
  85. return &FileLogger{"/dev/stderr", os.Stderr}, nil
  86. }
  87. func NewStdoutLogger() (logger Logger, err error) {
  88. return &FileLogger{"/dev/stderr", os.Stdout}, nil
  89. }
  90. type Logbook struct {
  91. loggers []Logger
  92. levels map[string]bool
  93. }
  94. func NewLog() *Logbook {
  95. loggers := make([]Logger, 32)
  96. levels := make(map[string]bool)
  97. return &Logbook{loggers, levels}
  98. }
  99. func (me *Logbook) AddLogger(logger Logger) {
  100. me.loggers = append(me.loggers, logger)
  101. }
  102. func (me *Logbook) EnableLevel(level string) {
  103. me.levels[level] = true
  104. }
  105. func (me *Logbook) DisableLevel(level string) {
  106. me.levels[level] = false
  107. }
  108. func (me * Logbook) GetLevels() map[string] bool {
  109. return me.levels
  110. }
  111. func enableDisableSplitter(c rune) bool {
  112. ok := (!unicode.IsLetter(c))
  113. ok = ok && (!unicode.IsNumber(c))
  114. ok = ok && (c != '_')
  115. return ok
  116. }
  117. func (me *Logbook) EnableLevels(list string) {
  118. to_enable := strings.FieldsFunc(list, enableDisableSplitter)
  119. for _, level := range to_enable {
  120. me.EnableLevel(level)
  121. }
  122. }
  123. func (me *Logbook) DisableLevels(list string) {
  124. to_disable := strings.FieldsFunc(list, enableDisableSplitter)
  125. for _, level := range to_disable {
  126. me.DisableLevel(level)
  127. }
  128. }
  129. func (me *Logbook) LogVa(name string, file string, line int, format string, args ...interface{}) {
  130. enabled, ok := me.levels[name]
  131. if (!ok) || (!enabled) {
  132. return
  133. }
  134. for _, logger := range me.loggers {
  135. if logger != nil {
  136. logger.Log(name, file, line, format, args...)
  137. }
  138. }
  139. }
  140. func (me *Logbook) Close() {
  141. for index, logger := range me.loggers {
  142. if logger != nil {
  143. logger.Close()
  144. me.loggers[index] = nil
  145. }
  146. }
  147. }
  148. func (lb *Logbook) EnabledLevels() []string {
  149. var res []string
  150. for name, ll := range lb.levels {
  151. if ll {
  152. res = append(res, name)
  153. }
  154. }
  155. return res
  156. }
  157. var DefaultLog *Logbook
  158. func init() {
  159. DefaultLog = NewLog()
  160. // runtime.SetFinalizer(DefaultLog, DefaultLog.Close)
  161. }
  162. func EnableLevel(level string) {
  163. DefaultLog.EnableLevel(level)
  164. }
  165. func DisableLevel(level string) {
  166. DefaultLog.DisableLevel(level)
  167. }
  168. func EnableLevels(list string) {
  169. DefaultLog.EnableLevels(list)
  170. }
  171. func DisableLevels(list string) {
  172. DefaultLog.DisableLevels(list)
  173. }
  174. func GetLevels() map[string] bool {
  175. return DefaultLog.GetLevels();
  176. }
  177. func AddLogger(logger Logger, err error) {
  178. if err == nil {
  179. DefaultLog.AddLogger(logger)
  180. }
  181. }
  182. func Setup(name string, stderr bool, stdout bool) {
  183. if name != "" {
  184. AddLogger(NewFileLogger(name))
  185. }
  186. if stderr {
  187. AddLogger(NewStderrLogger())
  188. }
  189. if stdout {
  190. AddLogger(NewStdoutLogger())
  191. }
  192. EnableLevel("INFO")
  193. EnableLevel("WARNING")
  194. EnableLevel("ERROR")
  195. EnableLevel("FATAL")
  196. }
  197. func Close() {
  198. DefaultLog.Close()
  199. }
  200. func WriteLog(depth int, name string, format string, args ...interface{}) {
  201. _, file, line, ok := runtime.Caller(depth)
  202. if !ok {
  203. file = "unknown"
  204. line = 0
  205. }
  206. DefaultLog.LogVa(name, file, line, format, args...)
  207. }
  208. func Log(name string, format string, args ...interface{}) {
  209. WriteLog(2, name, format, args)
  210. }
  211. func Printf(format string, args ...interface{}) {
  212. WriteLog(2, "INFO", format, args...)
  213. }
  214. func Info(format string, args ...interface{}) {
  215. WriteLog(2, "INFO", format, args...)
  216. }
  217. func Warning(format string, args ...interface{}) {
  218. WriteLog(2, "WARNING", format, args...)
  219. }
  220. func Error(format string, args ...interface{}) {
  221. WriteLog(2, "ERROR", format, args...)
  222. }
  223. func Fatal(format string, args ...interface{}) {
  224. WriteLog(2, "FATAL", format, args...)
  225. }
  226. func Debug(format string, args ...interface{}) {
  227. WriteLog(2, "DEBUG", format, args...)
  228. }
  229. func エラ(err error) {
  230. WriteLog(2, "ERROR", "%s", err.Error())
  231. }
  232. func WriteError(err error) {
  233. WriteLog(2, "ERROR", "%s", err.Error())
  234. }