log.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. }
  81. }
  82. _, err := l.File.Write([]byte(format + "\n"))
  83. if err != nil {
  84. //reopen file
  85. l.File.Close()
  86. l.OpenFile()
  87. }
  88. }
  89. func (l *Log) SetLogLevel(level int) {
  90. l.Level = level
  91. }
  92. func (l *Log) OpenFile() error {
  93. //test if file is closed
  94. if l.File == nil {
  95. //open file
  96. file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
  97. if err != nil {
  98. return err
  99. }
  100. l.File = file
  101. }
  102. //test if file is writable
  103. _, err := l.File.Write([]byte(" "))
  104. if err != nil {
  105. //reopen file
  106. l.File.Close()
  107. file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
  108. if err != nil {
  109. return err
  110. }
  111. l.File = file
  112. }
  113. return nil
  114. }
  115. func NewLog(path string) (*Log, error) {
  116. if path == "" {
  117. path = "log"
  118. }
  119. //test if path is exist
  120. _, err := os.Stat(path)
  121. if err != nil {
  122. //create path
  123. err = os.MkdirAll(path, 0777)
  124. if err != nil {
  125. return nil, err
  126. }
  127. }
  128. // test if path is a directory
  129. fileInfo, err := os.Stat(path)
  130. if err != nil {
  131. return nil, err
  132. }
  133. if !fileInfo.IsDir() {
  134. return nil, fmt.Errorf("log file path %s is not a directory", path)
  135. }
  136. log := &Log{
  137. Level: LOG_LEVEL_DEBUG,
  138. path: path,
  139. }
  140. //open file
  141. err = log.OpenFile()
  142. if err != nil {
  143. return nil, err
  144. }
  145. return log, nil
  146. }
  147. func init() {
  148. // why logger will cause panic when call
  149. initlog()
  150. }
  151. func initlog() {
  152. var err error
  153. main_log, err = NewLog("./logs")
  154. if err != nil {
  155. panic(err)
  156. }
  157. }
  158. var main_log *Log // wapper of go_log
  159. var show_log bool = true
  160. var logger = go_log.New(os.Stdout, "", go_log.Ldate|go_log.Ltime|go_log.Lshortfile)
  161. func SetShowLog(show bool) {
  162. show_log = show
  163. }
  164. func SetLogLevel(level int) {
  165. if main_log == nil {
  166. initlog()
  167. }
  168. main_log.SetLogLevel(level)
  169. }
  170. func Debug(format string, v ...interface{}) {
  171. if main_log == nil {
  172. initlog()
  173. }
  174. main_log.Debug(format, true, v...)
  175. }
  176. func Info(format string, v ...interface{}) {
  177. if main_log == nil {
  178. initlog()
  179. }
  180. main_log.Info(format, true, v...)
  181. }
  182. func Warn(format string, v ...interface{}) {
  183. if main_log == nil {
  184. initlog()
  185. }
  186. main_log.Warn(format, true, v...)
  187. }
  188. func Error(format string, v ...interface{}) {
  189. if main_log == nil {
  190. initlog()
  191. }
  192. main_log.Error(format, true, v...)
  193. }
  194. func Panic(format string, v ...interface{}) {
  195. if main_log == nil {
  196. initlog()
  197. }
  198. main_log.Panic(format, true, v...)
  199. }
  200. func SlientDebug(format string, v ...interface{}) {
  201. if main_log == nil {
  202. initlog()
  203. }
  204. main_log.Debug(format, false, v...)
  205. }
  206. func SlientInfo(format string, v ...interface{}) {
  207. if main_log == nil {
  208. initlog()
  209. }
  210. main_log.Info(format, false, v...)
  211. }
  212. func SlientWarn(format string, v ...interface{}) {
  213. if main_log == nil {
  214. initlog()
  215. }
  216. main_log.Warn(format, false, v...)
  217. }
  218. func SlientError(format string, v ...interface{}) {
  219. if main_log == nil {
  220. initlog()
  221. }
  222. main_log.Error(format, false, v...)
  223. }
  224. func SlientPanic(format string, v ...interface{}) {
  225. if main_log == nil {
  226. initlog()
  227. }
  228. main_log.Panic(format, false, v...)
  229. }