log.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package log
  2. /*
  3. log module is used to write log info to log file
  4. open a log file when log was created, and close it when log was destroyed
  5. */
  6. import (
  7. "fmt"
  8. go_log "log"
  9. "os"
  10. "time"
  11. )
  12. type Log struct {
  13. Level int
  14. //File of log
  15. File *os.File
  16. path string
  17. }
  18. const (
  19. LOG_LEVEL_DEBUG = 0
  20. LOG_LEVEL_INFO = 1
  21. LOG_LEVEL_WARN = 2
  22. LOG_LEVEL_ERROR = 3
  23. )
  24. const (
  25. LOG_LEVEL_DEBUG_COLOR = "\033[34m"
  26. LOG_LEVEL_INFO_COLOR = "\033[32m"
  27. LOG_LEVEL_WARN_COLOR = "\033[33m"
  28. LOG_LEVEL_ERROR_COLOR = "\033[31m"
  29. LOG_LEVEL_COLOR_END = "\033[0m"
  30. )
  31. func (l *Log) Debug(format string, stdout bool, v ...interface{}) {
  32. if l.Level <= LOG_LEVEL_DEBUG {
  33. l.writeLog("DEBUG", format, stdout, v...)
  34. }
  35. }
  36. func (l *Log) Info(format string, stdout bool, v ...interface{}) {
  37. if l.Level <= LOG_LEVEL_INFO {
  38. l.writeLog("INFO", format, stdout, v...)
  39. }
  40. }
  41. func (l *Log) Warn(format string, stdout bool, v ...interface{}) {
  42. if l.Level <= LOG_LEVEL_WARN {
  43. l.writeLog("WARN", format, stdout, v...)
  44. }
  45. }
  46. func (l *Log) Error(format string, stdout bool, v ...interface{}) {
  47. if l.Level <= LOG_LEVEL_ERROR {
  48. l.writeLog("ERROR", format, stdout, v...)
  49. }
  50. }
  51. func (l *Log) Panic(format string, stdout bool, v ...interface{}) {
  52. l.writeLog("PANIC", format, stdout, v...)
  53. panic("")
  54. }
  55. func (l *Log) writeLog(level string, format string, stdout bool, v ...interface{}) {
  56. //if the next day is coming, reopen file
  57. if time.Now().Format("/2006-01-02.log") != l.File.Name() {
  58. l.File.Close()
  59. l.OpenFile()
  60. }
  61. //test if file is closed
  62. if l.File == nil {
  63. //open file
  64. err := l.OpenFile()
  65. if err != nil {
  66. panic(err)
  67. }
  68. }
  69. //write log
  70. format = fmt.Sprintf("["+level+"]"+format, v...)
  71. if show_log && stdout {
  72. if level == "DEBUG" {
  73. logger.Output(4, LOG_LEVEL_DEBUG_COLOR+format+LOG_LEVEL_COLOR_END)
  74. } else if level == "INFO" {
  75. logger.Output(4, LOG_LEVEL_INFO_COLOR+format+LOG_LEVEL_COLOR_END)
  76. } else if level == "WARN" {
  77. logger.Output(4, LOG_LEVEL_WARN_COLOR+format+LOG_LEVEL_COLOR_END)
  78. } else if level == "ERROR" {
  79. logger.Output(4, LOG_LEVEL_ERROR_COLOR+format+LOG_LEVEL_COLOR_END)
  80. } else if level == "PANIC" {
  81. logger.Output(4, LOG_LEVEL_ERROR_COLOR+format+LOG_LEVEL_COLOR_END)
  82. }
  83. }
  84. _, err := l.File.Write([]byte(format + "\n"))
  85. if err != nil {
  86. //reopen file
  87. l.File.Close()
  88. l.OpenFile()
  89. }
  90. }
  91. func (l *Log) SetLogLevel(level int) {
  92. l.Level = level
  93. }
  94. func (l *Log) OpenFile() error {
  95. //test if file is closed
  96. if l.File == nil {
  97. //open file
  98. file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
  99. if err != nil {
  100. return err
  101. }
  102. l.File = file
  103. }
  104. //test if file is writable
  105. _, err := l.File.Write([]byte(" "))
  106. if err != nil {
  107. //reopen file
  108. l.File.Close()
  109. file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
  110. if err != nil {
  111. return err
  112. }
  113. l.File = file
  114. }
  115. return nil
  116. }
  117. func NewLog(path string) (*Log, error) {
  118. if path == "" {
  119. path = "log"
  120. }
  121. //test if path is exist
  122. _, err := os.Stat(path)
  123. if err != nil {
  124. //create path
  125. err = os.MkdirAll(path, 0777)
  126. if err != nil {
  127. return nil, err
  128. }
  129. }
  130. // test if path is a directory
  131. fileInfo, err := os.Stat(path)
  132. if err != nil {
  133. return nil, err
  134. }
  135. if !fileInfo.IsDir() {
  136. return nil, fmt.Errorf("log file path %s is not a directory", path)
  137. }
  138. log := &Log{
  139. Level: LOG_LEVEL_DEBUG,
  140. path: path,
  141. }
  142. //open file
  143. err = log.OpenFile()
  144. if err != nil {
  145. return nil, err
  146. }
  147. return log, nil
  148. }
  149. func init() {
  150. // why logger will cause panic when call
  151. initlog()
  152. }
  153. func initlog() {
  154. var err error
  155. main_log, err = NewLog("./logs")
  156. if err != nil {
  157. panic(err)
  158. }
  159. }
  160. var main_log *Log // wapper of go_log
  161. var show_log bool = true
  162. var logger = go_log.New(os.Stdout, "", go_log.Ldate|go_log.Ltime|go_log.Lshortfile)
  163. func SetShowLog(show bool) {
  164. show_log = show
  165. }
  166. func SetLogLevel(level int) {
  167. if main_log == nil {
  168. initlog()
  169. }
  170. main_log.SetLogLevel(level)
  171. }
  172. func Debug(format string, v ...interface{}) {
  173. if main_log == nil {
  174. initlog()
  175. }
  176. main_log.Debug(format, true, v...)
  177. }
  178. func Info(format string, v ...interface{}) {
  179. if main_log == nil {
  180. initlog()
  181. }
  182. main_log.Info(format, true, v...)
  183. }
  184. func Warn(format string, v ...interface{}) {
  185. if main_log == nil {
  186. initlog()
  187. }
  188. main_log.Warn(format, true, v...)
  189. }
  190. func Error(format string, v ...interface{}) {
  191. if main_log == nil {
  192. initlog()
  193. }
  194. main_log.Error(format, true, v...)
  195. }
  196. func Panic(format string, v ...interface{}) {
  197. if main_log == nil {
  198. initlog()
  199. }
  200. main_log.Panic(format, true, v...)
  201. }
  202. func SilentDebug(format string, v ...interface{}) {
  203. if main_log == nil {
  204. initlog()
  205. }
  206. main_log.Debug(format, false, v...)
  207. }
  208. func SilentInfo(format string, v ...interface{}) {
  209. if main_log == nil {
  210. initlog()
  211. }
  212. main_log.Info(format, false, v...)
  213. }
  214. func SilentWarn(format string, v ...interface{}) {
  215. if main_log == nil {
  216. initlog()
  217. }
  218. main_log.Warn(format, false, v...)
  219. }
  220. func SilentError(format string, v ...interface{}) {
  221. if main_log == nil {
  222. initlog()
  223. }
  224. main_log.Error(format, false, v...)
  225. }
  226. func SilentPanic(format string, v ...interface{}) {
  227. if main_log == nil {
  228. initlog()
  229. }
  230. main_log.Panic(format, false, v...)
  231. }