log.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package log
  2. import (
  3. "flag"
  4. "fmt"
  5. )
  6. var (
  7. h Handler
  8. level string
  9. file string
  10. stdout bool
  11. defaultLevel int
  12. levelIndex = map[string]int{
  13. "debug": debugLevel,
  14. "DEBUG": debugLevel,
  15. "info": infoLevel,
  16. "INFO": infoLevel,
  17. "warn": warnLevel,
  18. "WARN": warnLevel,
  19. "error": errorLevel,
  20. "ERROR": errorLevel,
  21. }
  22. )
  23. const (
  24. debugLevel = iota
  25. infoLevel
  26. warnLevel
  27. errorLevel
  28. )
  29. func init() {
  30. flag.StringVar(&level, "level", "", "log level")
  31. flag.StringVar(&file, "file", "", "log will print into file {log}")
  32. flag.BoolVar(&stdout, "stdout", true, "log will print into stdout")
  33. }
  34. type Config struct {
  35. Level string `toml:"level"`
  36. File string `toml:"file"`
  37. Stdout bool `toml:"stdout"`
  38. }
  39. func Init(c *Config) {
  40. if c == nil {
  41. c = &Config{
  42. Level: "info",
  43. Stdout: true,
  44. }
  45. }
  46. if file != "" {
  47. c.File = file
  48. }
  49. if level != "" {
  50. c.Level = level
  51. }
  52. var ok bool
  53. defaultLevel, ok = levelIndex[c.Level]
  54. if !ok {
  55. defaultLevel = infoLevel
  56. }
  57. var handlers []Handler
  58. if c.Stdout && stdout {
  59. handlers = append(handlers, NewStdHandler())
  60. }
  61. if c.File != "" {
  62. handlers = append(handlers, NewFileHandler(c.File))
  63. }
  64. h = Handlers(handlers)
  65. }
  66. func Info(args ...interface{}) {
  67. logs("INFO", args...)
  68. }
  69. func Infof(format string, args ...interface{}) {
  70. logf("INFO", format, args...)
  71. }
  72. func Warn(args ...interface{}) {
  73. logs("WARN", args...)
  74. }
  75. func Warnf(format string, args ...interface{}) {
  76. logf("WARN", format, args...)
  77. }
  78. func Error(args ...interface{}) {
  79. logs("ERROR", args...)
  80. }
  81. func Errorf(format string, args ...interface{}) {
  82. logf("ERROR", format, args...)
  83. }
  84. func Panic(v interface{}) {
  85. panic(v)
  86. }
  87. func Panicf(format string, args ...interface{}) {
  88. panic(fmt.Sprintf(format, args...))
  89. }
  90. func logf(lv, format string, args ...interface{}) {
  91. if h == nil {
  92. return
  93. }
  94. if levelIndex[lv] < defaultLevel {
  95. return
  96. }
  97. msg := format
  98. if len(args) > 0 {
  99. msg = fmt.Sprintf(format, args...)
  100. }
  101. h.Log(lv, msg)
  102. }
  103. func logs(lv string, args ...interface{}) {
  104. if h == nil {
  105. return
  106. }
  107. if levelIndex[lv] < defaultLevel {
  108. return
  109. }
  110. h.Log(lv, fmt.Sprint(args...))
  111. }
  112. func Close() (err error) {
  113. if h == nil {
  114. return
  115. }
  116. return h.Close()
  117. }